English Use hook, null value

23 replies
Goto Page
To the start Previous 1 2 Next To the start
05.04.16 12:54:50 pm
Up
HESY
User
Offline Off
sup us

I have some code:
Code:
1
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

and he not work.
because X = 0 and Y = tilex wtf

okay, corrected it on:
Code:
1
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

omg! its work!
but all is not well, sometimes the action is performed for another player.

where is the mistake?
05.04.16 01:24:12 pm
Up
Nekomata
User
Offline Off
The 3rd parameter of the 'use' hook returns 'data' so change it to this:

Code:
1
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


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
05.04.16 01:46:31 pm
Up
HESY
User
Offline Off
@user Nekomata:
Quote:
The 3rd parameter of the 'use' hook returns 'data'

I thought it is not necessary, so do not take into account
Thank you
05.04.16 04:40:11 pm
Up
Nekomata
User
Offline Off
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;
Code:
1
function my.use.hook(id,event,x,y)
05.04.16 07:36:22 pm
Up
HESY
User
Offline Off
@user 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?
05.04.16 08:01:17 pm
Up
Rainoth
Moderator
Offline Off
Post current code.
05.04.16 08:30:06 pm
Up
HESY
User
Offline Off
@user Rainoth:
Code:
1
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
05.04.16 08:42:55 pm
Up
Rainoth
Moderator
Offline Off
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?
05.04.16 08:51:09 pm
Up
Goo
User
Offline Off
Try this:
Code:
1
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


I've changed the function name (since I had errors when it had dots in it) and remade the msg2
More >
05.04.16 08:57:08 pm
Up
HESY
User
Offline Off
user Rainoth has written:
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?


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
my={}
my.hook={}
... some code here
edited 1×, last 05.04.16 08:59:28 pm
05.04.16 08:58:31 pm
Up
Fraizeraust
Moderator
Offline Off
@user Goo: I guess the 5 line will return an error as a nil value due to "trigger".

@user HESY: Just use cs2d lua cmd entity in case if you attempt to trigger a specific entity with "debug" name.
Code:
1
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")

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.
05.04.16 09:05:42 pm
Up
HESY
User
Offline Off
@user Fraizeraust: on map 257x257 I have ~50+ triggers
this does not fit, I'll go crazy
05.04.16 11:30:18 pm
Up
Rainoth
Moderator
Offline Off
@user HESY: Where there's a will there's a way.
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
_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 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.

@user 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 user 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.
edited 1×, last 05.04.16 11:52:27 pm
06.04.16 12:04:23 am
Up
HESY
User
Offline Off
@user Rainoth: thank you so much for such a detailed answer. I will be try.

-----

Help!
Use Hook:
Quote:
LUA ERROR: attempt to call a nil value
-> 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
06.04.16 08:42:18 pm
Up
Fraizeraust
Moderator
Offline Off
Are you sure that you're using the actual working user Rainoth's code and not something that has cs2d lua hook 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:
Log >


• 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.
06.04.16 11:07:19 pm
Up
Rainoth
Moderator
Offline Off
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.
07.04.16 09:14:32 pm
Up
HESY
User
Offline Off
@user 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)
08.04.16 01:18:57 am
Up
Rainoth
Moderator
Offline Off
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.
08.04.16 11:15:16 am
Up
HESY
User
Offline Off
@user Rainoth: I apologize for my mistakes, I mean Delay (120000ms) does not work on the button.

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
Name: 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
08.04.16 01:43:23 pm
Up
Rainoth
Moderator
Offline Off
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.
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
_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


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.
To the start Previous 1 2 Next To the start