Forum

> > CS2D > Scripts > Restart round or end ound = remove hats
Forums overviewCS2D overview Scripts overviewLog in to reply

English Restart round or end ound = remove hats

15 replies
To the start Previous 1 Next To the start

old Restart round or end ound = remove hats

cs2d_is_a_Gem
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
addhook("serveraction", "skinsmod")
function skinsmod(id, g)
if g==1 then
menu(id, "Hats Menu, Sonic | 2D, Goku | 2D, Devil | 2D, Soldado red | 2D, Capitan america | 2D, soldado blue | 2D, Soldado roxo | 2D,, Next|>>>")
return 1
end
end

		addhook("menu", "menus")
		function menus(id, title, but)

if (title=="Hats Menu") then
if(but==1) then
	freeimage(id)
	id=image("gfx/hat/s0nic.png",1,1,200+id)
end
if(but==2) then
	freeimage(id)
	id=image("gfx/hat/goku_hat.png",1,1,200+id)
end
if(but==3) then
	freeimage(id)
	id=image("gfx/hat/devil.png",1,1,200+id)
end
if(but==4) then
	freeimage(id)
	id=image("gfx/hat/soldadored.png",1,1,200+id)		
end
if(but==5) then 
	freeimage(id)
	id=image("gfx/hat/captain america hat by spike.png",1,1,200+id)
end
if(but==6) then
	freeimage(id)
	id=image("gfx/hat/soldadoazul.png",1,1,200+id)
end
if(but==7) then
	freeimage(id)
	id=image("gfx/hat/soldadoroxo.png",1,1,200+id)
end


need help
edited 1×, last 18.10.16 06:20:57 am

old Re: Restart round or end ound = remove hats

Rainoth
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
hats = {}
addhook("serveraction", "skinsmod")
function skinsmod(id, g)
	if g==1 then
		menu(id, "Hats Menu, Sonic | 2D, Goku | 2D, Devil | 2D, Soldado red | 2D, Capitan america | 2D, soldado blue | 2D, Soldado roxo | 2D,, Next|>>>")
		-- serveraction doesn't return anything //return 1
	end
end

addhook("startround","_sr")
function _sr()
	for _,id in pairs (player(0,"table")) do
		if hats[id] then
			freeimage(hats[id])
			hats[id] = nil
		end
	end
end

addhook("menu", "menus")
function menus(id, title, but)
	if (title=="Hats Menu") then
		if hats[id] then 
			freeimage(hats[id])
			hats[id] = nil
		end
		if(but==1) then
			hats[id]=image("gfx/hat/s0nic.png",1,1,200+id)
		elseif(but==2) then
			hats[id]=image("gfx/hat/goku_hat.png",1,1,200+id)
		elseif(but==3) then
			hats[id]=image("gfx/hat/devil.png",1,1,200+id)
		elseif(but==4) then
			hats[id]=image("gfx/hat/soldadored.png",1,1,200+id)          
		elseif(but==5) then 
			hats[id]=image("gfx/hat/captain america hat by spike.png",1,1,200+id)
		elseif(but==6) then
			hats[id]=image("gfx/hat/soldadoazul.png",1,1,200+id)
		elseif(but==7) then
			hats[id]=image("gfx/hat/soldadoroxo.png",1,1,200+id)
		end
	end
end

old Re: Restart round or end ound = remove hats

Bowlinghead
User Off Offline

Quote
cs2d lua hook endround
cs2d lua cmd freeimage
1
2
3
4
addhook("endround","roundend")
function roundend()
  freeimage(id)
end
But you saved the image in "id" which is no global variable so you cant freeimage it.

You need to create an array like this:
1
2
3
4
5
6
7
8
9
10
11
myhats = {}

addhook("menu", "menus")
function menus(id, title, but)
	if (title=="Hats Menu") then
		freeimage(myhats[id])
		myhats[id] = image("gfx/hat/s0nic.png",1,1,200+id)
	elseif
		-- ...
	end
end

old Re: Restart round or end ound = remove hats

Yates
Reviewer Off Offline

Quote
@user Bowlinghead: When using cs2d lua cmd freeimage make sure to set the value of the variable to
nil
afterwards like user Rainoth has done. user Cure Pikachu just stated the images are already removed at the beginning of a round, the value must still be set to
nil
in this case as well.

This is to stop weird errors occurring due to two (or more) variables being given the same value and you are using that value to manipulate the images which leads to multiple or the wrong image being manipulated.

old Re: Restart round or end ound = remove hats

cs2d_is_a_Gem
User Off Offline

Quote
no work
very hard configure my script
(my scripter level is 0,0,0,0,0,0,1)

I will study FOR use your help,
THX ALL FOR HELPING<3
Very friendly ALL
look server incomplete 24/7


> » EliteARG « Awp_Grey 190.114.255.156:36964
> new code
More >




thx all
zorry me english bad

old Re: Restart round or end ound = remove hats

Bowlinghead
User Off Offline

Quote
1. cs2d lua hook menu look at the parameter list. Id is the player id.
Your code:
1
2
3
4
5
6
7
8
9
10
11
addhook("menu", "menus")
function menus(id, title, but) -- player id
	if (title=="Hats Menu") then
		if(but==1) then
			freeimage(id) --> you use it as image id
			id=image("gfx/hat/s0nic.png",1,1,200+id) -- here
		end
	...
	end
	...
end
So you have to have RAM that is stored outside of that function to exist after the function has ended like this array:
1
hats = {}
In line 1 from @user Rainoth s code. Usually you store the data with integer indicis like:
1
hats[3] = image(...)
And since every player has his own id (from 1-32) you can just write
1
hats[id] = image(...)
2. But note that you need to clear the data like @user Yates said.

old Re: Restart round or end ound = remove hats

Rainoth
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
hats = {}
hats.hooks = {"serveraction","menu","startround"}
hats.items = {
	["Sonic"] = {view = "2D", path = "s0nic.png"}, -- don't forget the commas
	["Goku"] = {view = "2D", path = "goku_hat.png"}
}
hats.count = #hats.items
hats.counter = 0
hats.menus = {}
hats.player = {}

for k,_ in pairs (hats.items) do
	hats.counter = hats.counter + 1
	hats.items[k].ID = hats.counter
end

for _,hook in pairs (hats.hooks) do
	addhook(hook,"h_"..hook)
end 

if hats.count <= 9 then
	hats.menus[1] = "Hats Menu"
	for k,v in pairs (hats.items) do
		hats.menus[1] = hats.menus[1] .. "," .. k .. "|" .. v.view
	end
elseif hats.count > 9 then
	local index = #hats.menus+1
	local counter = 0
	hats.menus[index] = "Hats Menu ["..index.."]"
	for k,v in pairs (hats.items) do
		hats.menus[index] = hats.menus[index] .. "," .. k .. "|" .. v.view
		counter = counter + 1
		if counter == 7 then
			hats.menus[index] = hats.menus[index] .. ",Previous Page, Next Page"
			counter = 0
			index = index + 1
		end
	end
	index = nil
	counter = nil
end

function h_serveraction(id, b)	
	if b == 1 then
		menu(hats.menus[1])
	end
end

function findHat(ID)
	for k,v in pairs (hats.items) do
		if v.ID == ID then
			return k
		end
	end
end

function removeHat(id)
	if hats.player[id] then
		freeimage(hats.player[id])
		hats.player[id] = nil
	end
end

function h_menu(id, t, b)
	if t == "Hats Menu" then
		removeHat(id)
		hats.player[id] = image("gfx/hat/"..hats.items[findHat(b)].path,1,1,200+id)
	elseif t:find("Hats Menu") then
		local page = t:sub(12,13)
		if tonumber(t:sub(13,14)) then -- in case of more than single digit page amount
			page = page .. t:sub(13,14)
		end
		page = tonumber(page)
		local ID = page*7+b
		if b <= 7 then
			removeHat(id)
			hats.player[id] = image("gfx/hat/"..hats.items[findHat(ID)].path,1,1,200+id)
		elseif b == 8 then
			menu(id,hats.menus[page+1])
		elseif b == 9 then
			menu(id,hats.menus[page-1])
		end
		ID = nil
		page = nil
	end
end

function h_startround()
	for _,id in pairs (player(0,"tableliving")) do
		removeHat(id)
	end
end

hats.count = nil
hats.counter = nil
hats.hooks = nil

Try this. Wanted to get my mind off of things. Untested.
Add more hats by checking how I added the second one (it's at the start of code).

old Re: Restart round or end ound = remove hats

Rainoth
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
hats = {}
hats.hooks = {"serveraction","menu","startround"}
hats.items = {
	["Angel"] = {view = "2D", path = "angel.png"}, -- don't forget the commas
	["Devil"] = {view = "2D", path = "devil.png"},
	["Gandalf"] = {view = "2D", path = "gandalf_hat.png"},
	["Graduation"] = {view = "2D", path = "graduation_hat.png"},
	["Party"] = {view = "2D", path = "party_hat.png"},
	["Pirate"] = {view = "2D", path = "pirate_hat.png"},
	["Pumpkin"] = {view = "2D", path = "pumpkin_head.png"},
	["Santa"] = {view = "2D", path = "santa_hat.png"},
	["Snowman"] = {view = "2D", path = "snowman.png"},
	["Spear"] = {view = "2D", path = "spear.png"}
}
--hats.count = #hats.items
hats.counter = 0
hats.menus = {}
hats.player = {}

for k,_ in pairs (hats.items) do
	hats.counter = hats.counter + 1
	hats.items[k].ID = hats.counter
end
hats.count = hats.counter

for _,hook in pairs (hats.hooks) do
	addhook(hook,"h_"..hook)
end 

if hats.count <= 9 then
	hats.menus[1] = "Hats Menu"
	for k,v in pairs (hats.items) do
			hats.menus[1] = hats.menus[1] .. "," .. k .. "|" .. v.view
	end
elseif hats.count > 9 then
	local index = 1
	local counter = 0
	hats.menus[index] = "Hats Menu ["..index.."]"
	for k,v in pairs (hats.items) do
		hats.menus[index] = hats.menus[index] .. "," .. k .. "|" .. v.view
		counter = counter + 1
		if counter == 7 then
			if index == 1 then
				hats.menus[index] = hats.menus[index] .. ",(Previous Page),Next Page"
			else
				hats.menus[index] = hats.menus[index] .. ",Previous Page,Next Page"
			end
				counter = 0
				index = index + 1
				hats.menus[index] = "Hats Menu ["..index.."]"
		end
	end
	index = nil
	counter = nil
end

function h_serveraction(id, b)     
	if b == 1 then
		menu(id,hats.menus[1])
	end
end

function findHat(ID)
	for k,v in pairs (hats.items) do
		if v.ID == ID then
			return k
		end
	end
end

function removeHat(id)
	if hats.player[id] then
		freeimage(hats.player[id])
		hats.player[id] = nil
	end
end

function h_menu(id, t, b)
	if t == "Hats Menu" then
		removeHat(id)
		hats.player[id] = image("gfx/hat/"..hats.items[findHat(b)].path,1,1,200+id)
	elseif t:find("Hats Menu") then
		local page = t:sub(12,12)
		if tonumber(t:sub(12,13)) then -- in case of more than single digit page amount
			page = page .. t:sub(12,13)
		end
		page = tonumber(page)
		local ID = page*7+b-7
		if b <= 7 then
			removeHat(id)
			hats.player[id] = image("gfx/hat/"..hats.items[findHat(ID)].path,1,1,200+id)
		elseif b == 8 then
			menu(id,hats.menus[page-1])
		elseif b == 9 then
			menu(id,hats.menus[page+1])
		end
		ID = nil
		page = nil
	end
end

function h_startround()
	for _,id in pairs (player(0,"tableliving")) do
		removeHat(id)
	end
end

hats.count = nil
hats.counter = nil
hats.hooks = nil

Tested. Worked for me. I found some dumb mistakes like bad hat ID calculation, nil menu when you've got multiple pages and press "Previous page" and so on.

I have a question for everyone else, though.
If you check my previous code, I have hats.items table with two tables, then the next line is hats.count = #hats.items
I expected it to be 2 but it was 0, as if the table had no items (but when you check how I did it now, it works). Any explanation why?

old Re: Restart round or end ound = remove hats

Masea
Super User Off Offline

Quote
user Rainoth has written
I have a question for everyone else, though.
If you check my previous code, I have hats.items table with two tables, then the next line is hats.count = #hats.items
I expected it to be 2 but it was 0, as if the table had no items (but when you check how I did it now, it works). Any explanation why?
It supposed to be 10 in very deed.

old Re: Restart round or end ound = remove hats

Cure Pikachu
User Off Offline

Quote
user Rainoth has written
I have a question for everyone else, though.
If you check my previous code, I have hats.items table with two tables, then the next line is hats.count = #hats.items
I expected it to be 2 but it was 0, as if the table had no items (but when you check how I did it now, it works). Any explanation why?

Here's your answer:
https://forums.coronalabs.com/topic/26777-resolved-length-of-a-array/ has written
The
#
operator will not work with the key-value style of tables, it only works with numeric indexed arrays. However it also stops counting when it hits a nil value:
t[1] = "Hello World"
t[2] = "How are you?"
t[3] = nil
t[4] = "Top of the mornin' to ya!"

#t
is 2 not 4. If you use
#tablename
and the count is less than you expect, you likely have a nil in the table.
edited 2×, last 22.02.17 04:26:02 am
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview