Use hook, null value
23 replies05.04.16 12:54:50 pm
sup
I have some code:
and he not work.
because X = 0 and Y = tilex wtf
okay, corrected it on:
omg! its work!
but all is not well, sometimes the action is performed for another player.
where is the mistake?

I have some code:
Code:
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
addhook("use", "my.hook.use")
function my.hook.use(id,event,x,y)
if event==100 then
if player(id,"tilex")==x and player(id,"tiley")==y then
msg('[DEBUG] [Use] player: '..player(id,"name")..' event: '..event..' x: '..x..' y: '..y)
end
end
end
function my.hook.use(id,event,x,y)
if event==100 then
if player(id,"tilex")==x and player(id,"tiley")==y then
msg('[DEBUG] [Use] player: '..player(id,"name")..' event: '..event..' x: '..x..' y: '..y)
end
end
end
and he not work.
because X = 0 and Y = tilex wtf

okay, corrected it on:
Code:
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
addhook("use", "my.hook.use")
function my.hook.use(id,event,x,y)
if event==100 then
if player(id,"tilex")==y then
msg('[DEBUG] [Use] player: '..player(id,"name")..' event: '..event..' x: '..x..' y: '..y)
end
end
end
function my.hook.use(id,event,x,y)
if event==100 then
if player(id,"tilex")==y then
msg('[DEBUG] [Use] player: '..player(id,"name")..' event: '..event..' x: '..x..' y: '..y)
end
end
end
omg! its work!
but all is not well, sometimes the action is performed for another player.
where is the mistake?
The 3rd parameter of the 'use' hook returns 'data' so change it to this:
There was no additional data being returned so the value was 0. And you put 'x' as the variable for the data parameter. Hence you got X = 0 and Y = X's coordinates
Read: http://www.cs2d.com/help.php?hookcat=all&hook=use#hook
Code:
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
addhook("use", "my.hook.use")
function my.hook.use(id,event,data,x,y)
if event==100 then
if player(id,"tilex")==x and player(id,"tiley")==y then
msg('[DEBUG] [Use] player: '..player(id,"name")..' event: '..event..' x: '..x..' y: '..y)
end
end
end
function my.hook.use(id,event,data,x,y)
if event==100 then
if player(id,"tilex")==x and player(id,"tiley")==y then
msg('[DEBUG] [Use] player: '..player(id,"name")..' event: '..event..' x: '..x..' y: '..y)
end
end
end
There was no additional data being returned so the value was 0. And you put 'x' as the variable for the data parameter. Hence you got X = 0 and Y = X's coordinates
Read: http://www.cs2d.com/help.php?hookcat=all&hook=use#hook
@
Nekomata:
I thought it is not necessary, so do not take into account
Thank you

Quote:
The 3rd parameter of the 'use' hook returns 'data'
I thought it is not necessary, so do not take into account
Thank you
It's not necessary for this event ID (100) but the parameters in a function are used in order.
If the 'data' parameter was called at the end then you could've gotten away with your code;
If the 'data' parameter was called at the end then you could've gotten away with your code;
Code:
1
function my.use.hook(id,event,x,y)
@
Nekomata: friend, I took into account their mistakes, but unfortunately with "use" is sometimes (I do not understand for what reason), the action is triggered for another player...
Why is this happening?

Why is this happening?
@
Rainoth:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
addhook("trigger", "my.hook.trigger")
addhook("use", "my.hook.use")
function my.hook.use(id,event,data,x,y)
if event==100 then
if player(id,"tilex")==x and player(id,"tiley")==y then
if trigger == "debug" then
msg2(id,'[DEBUG] [Use] player: '..player(id,"name")..' event: '..event..' x: '..x..' y: '..y)
end
-- if trigger ...
-- ...
-- ... etc.
end
end
end
addhook("use", "my.hook.use")
function my.hook.use(id,event,data,x,y)
if event==100 then
if player(id,"tilex")==x and player(id,"tiley")==y then
if trigger == "debug" then
msg2(id,'[DEBUG] [Use] player: '..player(id,"name")..' event: '..event..' x: '..x..' y: '..y)
end
-- if trigger ...
-- ...
-- ... etc.
end
end
end
1. x and y are "use" tiles. Meaning that it returns where the player was standing when he clicked E (by default). It makes no sense to check if player is standing on them since he does. Always. It's needless computation.
2. my.hook.trigger is another function and "trigger" is not a global variable (unless you set it so in a function you didn't provide), thus the variable "trigger" shouldn't work in that context. It shouldn't even be there in the first place.
Finally, what do you mean by action? Do you mean that msg2 is called for someone else?
2. my.hook.trigger is another function and "trigger" is not a global variable (unless you set it so in a function you didn't provide), thus the variable "trigger" shouldn't work in that context. It shouldn't even be there in the first place.
Finally, what do you mean by action? Do you mean that msg2 is called for someone else?
Try this:
I've changed the function name (since I had errors when it had dots in it) and remade the msg2
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
addhook("use","myhookuse")
function myhookuse(id,event,data,x,y)
if event==100 then
if player(id,"tilex")==x and player(id,"tiley")==y then
if trigger == "debug" then
msg2(id,"[DEBUG] [Use] player: "..player(id,"name").." event: "..event.." x: "..x.." y: "..y)
end
-- if trigger ...
-- ...
-- ... etc.
end
end
end
function myhookuse(id,event,data,x,y)
if event==100 then
if player(id,"tilex")==x and player(id,"tiley")==y then
if trigger == "debug" then
msg2(id,"[DEBUG] [Use] player: "..player(id,"name").." event: "..event.." x: "..x.." y: "..y)
end
-- if trigger ...
-- ...
-- ... etc.
end
end
end
I've changed the function name (since I had errors when it had dots in it) and remade the msg2
Is death truly a bad thing? If it is, then why do we all get to experience it? And... Why does it take so long? - What is the meaning of death?

1. x and y are "use" tiles. Meaning that it returns where the player was standing when he clicked E (by default). It makes no sense to check if player is standing on them since he does. Always. It's needless computation.
2. my.hook.trigger is another function and "trigger" is not a global variable (unless you set it so in a function you didn't provide), thus the variable "trigger" shouldn't work in that context. It shouldn't even be there in the first place.
Finally, what do you mean by action? Do you mean that msg2 is called for someone else?
2. my.hook.trigger is another function and "trigger" is not a global variable (unless you set it so in a function you didn't provide), thus the variable "trigger" shouldn't work in that context. It shouldn't even be there in the first place.
Finally, what do you mean by action? Do you mean that msg2 is called for someone else?
msg2 for example
I think I made unnecessary actions:
The essence of this script is that on the map there is a button, when clicked, the script should work...
Quote:
I've changed the function name (since I had errors when it had dots in it) and remade the msg2
Code:
1
2
3
2
3
my={}
my.hook={}
... some code here
my.hook={}
... some code here
edited 1×, last 05.04.16 08:59:28 pm
@
Mami Tomoe: I guess the 5 line will return an error as a nil value due to "trigger".
@
HESY: Just use
entity in case if you attempt to trigger a specific entity with "debug" name.
Note the Use.x and Use.y are indexed within Use table and it's required to fill those with actual X and Y path coordination.

@


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Use = {} -- Keep this table as it is
Use.x = -- X coordination here
Use.y = -- Y coordination here
function useHook(id, event, data, x, y)
if event == 100 then
if x == Use.x and y == Use.y then
if entity(x, y, "name") == "debug" then
msg2(id,'[DEBUG] [Use] player: ' .. player(id, 'name') .. ' event: ' .. event .. ' x: ' .. x .. ' y: ' .. y)
end
end
end
end
addhook("use", "useHook")
Use.x = -- X coordination here
Use.y = -- Y coordination here
function useHook(id, event, data, x, y)
if event == 100 then
if x == Use.x and y == Use.y then
if entity(x, y, "name") == "debug" then
msg2(id,'[DEBUG] [Use] player: ' .. player(id, 'name') .. ' event: ' .. event .. ' x: ' .. x .. ' y: ' .. y)
end
end
end
end
addhook("use", "useHook")
Note the Use.x and Use.y are indexed within Use table and it's required to fill those with actual X and Y path coordination.
@
Fraizeraust: on map 257x257 I have ~50+ triggers
this does not fit, I'll go crazy

this does not fit, I'll go crazy
@
HESY: Where there's a will there's a way.
For coders out there, feel free to correct my function definition if I did it wrong because I've never written it that way and wrote it from a vague memory of seeing it multiple times.
@
HESY: How the code works (I can comment it out if you wish but not everyone appreciates that so ask if you need it).
The script will supposedly:
Take all entities and find all "Trigger_Use" entities (type 93 according to cs2d.com, you can change into typename if you wish)
Store them into a table with all values you might need (it actually stores values like position despite not really needing it. Sorry, I just wrote it just in case I would need it while writing to access the table...)
Later in the code you can copy paste the function definition and create your own functions for each trigger easily (no needless IFs needed like in
Fraizeraust's example). Make sure to change the "YourTriggerName" into whatever name you fill on your trigger. This is case sensitive I think so make sure it's exact.
When player clicks any button, the script will search for a trigger with that name in the table and if it has a function, it will call it.
Hope it works and helps you out.
Cheers.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
_triggers = {}
for k,v in pairs(entitylist()) do
if entity(v.x,v.y,"type") == 93 then
_triggers[entity(v.x,v.y,"name")] = {x = v.x,y = v.y, _function = nil}
end
end
_triggers["YourTriggerName"]._function =
function (id)
msg2(id,"This is a test function")
end
addhook("use","_use")
function _use(id,event,data,x,y)
if event == 100 then
for k,v in pairs(_triggers) do
if entity(x,y,"name") == k then
if _triggers[k]._function then
_triggers[k]._function(id)
end
end
end
end
end
for k,v in pairs(entitylist()) do
if entity(v.x,v.y,"type") == 93 then
_triggers[entity(v.x,v.y,"name")] = {x = v.x,y = v.y, _function = nil}
end
end
_triggers["YourTriggerName"]._function =
function (id)
msg2(id,"This is a test function")
end
addhook("use","_use")
function _use(id,event,data,x,y)
if event == 100 then
for k,v in pairs(_triggers) do
if entity(x,y,"name") == k then
if _triggers[k]._function then
_triggers[k]._function(id)
end
end
end
end
end
For coders out there, feel free to correct my function definition if I did it wrong because I've never written it that way and wrote it from a vague memory of seeing it multiple times.
@

The script will supposedly:
Take all entities and find all "Trigger_Use" entities (type 93 according to cs2d.com, you can change into typename if you wish)
Store them into a table with all values you might need (it actually stores values like position despite not really needing it. Sorry, I just wrote it just in case I would need it while writing to access the table...)
Later in the code you can copy paste the function definition and create your own functions for each trigger easily (no needless IFs needed like in

When player clicks any button, the script will search for a trigger with that name in the table and if it has a function, it will call it.
Hope it works and helps you out.
Cheers.
edited 1×, last 05.04.16 11:52:27 pm
@
Rainoth: thank you so much for such a detailed answer. I will be try.
-----
Help!
Use Hook:
Trigger_use settings: Name "", Trigger "debug"
What's wrong?

-----
Help!
Use Hook:
Quote:
LUA ERROR: attempt to call a nil value
-> in Lua hook 'trigger', params: 'debug', false
-> in Lua hook 'trigger', params: 'debug', false
Trigger_use settings: Name "", Trigger "debug"
What's wrong?
edited 1×, last 06.04.16 08:21:28 pm
Are you sure that you're using the actual working
Rainoth's code and not something that has
trigger hook in it? Seeing that the error tells it is caused by this hook and its param. I took a try to see if this script works for testing purposes.
Modified de_dust2 a bit by adding Trigger_Use, added a name calling it debug, changed YourTriggerName to debug inside the square bracket in 8 line so Lua will not recognize a nil value and I do not encounter any issue whatsoever. Here's the recent logs to see the last actions of this script:
Edit: Hmm... Looks like you filled up the trigger field with debug instead of the name in the map editor. That must be done the opposite otherwise it's quite obvious it will not recognize the trigger because it has no name therefore a nil value.


Modified de_dust2 a bit by adding Trigger_Use, added a name calling it debug, changed YourTriggerName to debug inside the square bracket in 8 line so Lua will not recognize a nil value and I do not encounter any issue whatsoever. Here's the recent logs to see the last actions of this script:

The script should work as it is, without any additional hooks. It doesn't make sense that you have "trigger" hook there since I didn't define anything with "trigger" hook. Only use the code I posted above.
@
Rainoth: Using your code stumbled upon a bug:
The infinite button in use (by the way, I changed the "name" to the "trigger", because for the need to redo the entire map)
This bug is carried out starting with the second player who came to the server and etc. But the player 1 of this there is no bugs
(Sorry for so many questions and error, but I am powerless and I need help)

The infinite button in use (by the way, I changed the "name" to the "trigger", because for the need to redo the entire map)
This bug is carried out starting with the second player who came to the server and etc. But the player 1 of this there is no bugs
(Sorry for so many questions and error, but I am powerless and I need help)
I'm afraid I couldn't understand properly what your problem is. Do you mean that the button can be pressed as many times as you wish (thus starting the function attached to it as many times as you click it)?
If you want the button to work multiple times, you set it so:
Name: YourNameHere
Trigger:
If you want the button to work only once, you set it so:
Name: YourNameHere
Trigger: YourNameHere
If it's not that, please try to explain better so we can understand it.
If you want the button to work multiple times, you set it so:
Name: YourNameHere
Trigger:
If you want the button to work only once, you set it so:
Name: YourNameHere
Trigger: YourNameHere
If it's not that, please try to explain better so we can understand it.
@
Rainoth: I apologize for my mistakes, I mean Delay (120000ms) does not work on the button.
For example, I have 10 triggers called 'debug'
If use
That's all off button..
Need to check for the delay, but after reading cs2d.com I have not found how to do it

Quote:
If you want the button to work only once, you set it so:
For example, I have 10 triggers called 'debug'
If use
Code:
1
2
2
Name: debug
Trigger: debug
Trigger: debug
That's all off button..
Need to check for the delay, but after reading cs2d.com I have not found how to do it
So you want to disable buttons for a while and then re-enable them?
Sure you can do it with map entities but you can add a small code too.
Not sure if it will work. Been a while since I programmed a lot with lua for CS2D. Feel free to fix the third timer parameter passed ("trigger "..k) if it doesn't work.
Sure you can do it with map entities but you can add a small code too.
Code:
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
_triggers = {}
for k,v in pairs(entitylist()) do
if entity(v.x,v.y,"type") == 93 then
_triggers[entity(v.x,v.y,"name")] = {x = v.x,y = v.y, _function = nil}
end
end
_triggers["YourTriggerName"]._function =
function (id)
msg2(id,"This is a test function")
end
addhook("use","_use")
function _use(id,event,data,x,y)
if event == 100 then
for k,v in pairs(_triggers) do
if entity(x,y,"name") == k then
if _triggers[k]._function then
_triggers[k]._function(id)
timer(120000,"parse","trigger "..k)
end
end
end
end
end
for k,v in pairs(entitylist()) do
if entity(v.x,v.y,"type") == 93 then
_triggers[entity(v.x,v.y,"name")] = {x = v.x,y = v.y, _function = nil}
end
end
_triggers["YourTriggerName"]._function =
function (id)
msg2(id,"This is a test function")
end
addhook("use","_use")
function _use(id,event,data,x,y)
if event == 100 then
for k,v in pairs(_triggers) do
if entity(x,y,"name") == k then
if _triggers[k]._function then
_triggers[k]._function(id)
timer(120000,"parse","trigger "..k)
end
end
end
end
end
Not sure if it will work. Been a while since I programmed a lot with lua for CS2D. Feel free to fix the third timer parameter passed ("trigger "..k) if it doesn't work.