Forum

> > CS2D > Scripts > usebutton hook
Forums overviewCS2D overview Scripts overviewLog in to reply

English usebutton hook

9 replies
To the start Previous 1 Next To the start

old usebutton hook

massiveguy
User Off Offline

Quote
i want to make lua which if one player press button then
all players will get image over them.

i have code but this is working for only one player who pressed button.
please help me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function initArray(m) 
local array = {} 
for i = 1, m do 
array[i]=0 
end 
return array 
end 

glowing=initArray(32) 


addhook("usebutton","asdfg")
function asdfg(id,x,y)
 if entity(x,y,"name") == 'glower' then
glowing[id]=image("gfx/glower.png",1,1,200+id,id) 
end
end

old Re: usebutton hook

VaiN
User Off Offline

Quote
really?

Just put it in a loop:
1
2
3
4
5
for i = 1,32 do
	if player(i,"exists") and player(i,"health")>0 then
		glowing[i]=image("gfx/glower.png",1,1,200+i,i)
	end
end

I didn't check any of your code for errors, but if you are wanting to apply somthing to all players you should loop through them. There are multiple ways of doing it, this is just one example.

old Re: usebutton hook

VaiN
User Off Offline

Quote
user MikuAuahDark has written
@user VaiN: why not use cs2d lua cmd player(0,"tableliving") instead?

because it may be more confusing for someone who didn't know to use a loop anyway.

but here you go:
1
2
3
4
local tbl = player(0,"tableliving")
for id = 1,#tbl do
     glowing[ tbl[id] ]=image("gfx/glower.png",1,1,200+tbl[id],tbl[id])
end
This is typically better as it is less computationally heavy, but either way will work fine.

old Re: usebutton hook

ReVoltage
User Off Offline

Quote
user VaiN has written
1
2
3
4
local tbl = player(0,"tableliving")
for id = 1,#tbl do
     glowing[ tbl[id] ]=image("gfx/glower.png",1,1,200+tbl[id],tbl[id])
end

Uhh, maybe what user MikuAuahDark mean is this
1
for _,id in pairs(player(0,"tableliving")) do
So it will be like this
1
2
3
for _,i in pairs(player(0,"tableliving")) do
glowing[i]=image("gfx/glower.png",1,1,200+i,i)
end
Problem?

Correct me if I wrong..

old Re: usebutton hook

KimKat
GAME BANNED Off Offline

Quote
user ReVoltage is quite more right due to the code accuracy. Instead of making player range 0-12 glow. user ReVoltage makes it select only those players which are alive and makes them glow. Both work, but they differ in terms of coding. As #tbl will only calculate how many indices there are in a table. It doesn't select individual players although it creates a range for players instead.

old Re: usebutton hook

VaiN
User Off Offline

Quote
and this is why I usually don't post on these forums, because I prefer to show people the basics when they don't know them, instead of showing them the shortcuts.

and you are wrong about #tbl. It does return the correct count and loops through all ID's returned. Though I should have used tbl[i] instead of tbl[id] as that was misleading.

it returns a table with values AND keys.
if you use:
1
2
3
for k,id in ipairs(player(0,"tableliving")) do
	print(k, id)
end
you will see that all living players are listed and every index is an ordered pair. as such #tbl will return the correct count of entries and allow you to loop through all of them.

more detailed explanation:
say there is ID 5 and 8 alive in the server
local tbl = player(0,"tableliving")
will return this:
tbl[1] = 5
tbl[2] = 8
#tbl will give you 2
so this works fine:
1
2
3
for i=1,#tbl do
	print(tbl[i])
end
and it will print:
1
2
5
8
because the ID is the value not the index.

when you use for _,id in pairs() .. you are only capturing the value and not the key, that's why you may have overlooked this. That is the most compact way to achieve what he was after though, admittedly.

i still stand by my original example as it's more flexible and shows the basics of a 'loop' as was the original intent. and with that he'll know how to tackle any other such obstacles in his scripting adventures.

and on that note i'll stop posting on this forum from now on, and get back to my own scripting, since i seem to waste less time that way.
edited 3×, last 16.12.13 07:00:04 am

old Re: usebutton hook

Apache uwu
User Off Offline

Quote
I'll like to point out that not only is user VaiN's technique is 100% correct, but it's also faster.

for > ipairs > pairs
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview