Forum

> > CS2D > Scripts > Use hook, null value
Forums overviewCS2D overview Scripts overviewLog in to reply

English Use hook, null value

23 replies
Page
To the start Previous 1 2 Next To the start

old Use hook, null value

HESY
User Off Offline

Quote
sup us

I have some 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:
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?

old Re: Use hook, null value

Nekomata
User Off Offline

Quote
The 3rd parameter of the 'use' hook returns 'data' so change it to this:

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

old Re: Use hook, null value

HESY
User Off Offline

Quote
@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

old Re: Use hook, null value

Nekomata
User Off Offline

Quote
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;
1
function my.use.hook(id,event,x,y)

old Re: Use hook, null value

HESY
User Off Offline

Quote
@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?

old Re: Use hook, null value

HESY
User Off Offline

Quote
@user Rainoth:
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

old Re: Use hook, null value

Rainoth
Moderator Off Offline

Quote
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?

old Re: Use hook, null value

Mami Tomoe
User Off Offline

Quote
Try this:
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 >

old Re: Use hook, null value

HESY
User Off Offline

Quote
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

1
2
3
my={}
my.hook={}
... some code here
edited 1×, last 05.04.16 08:59:28 pm

old Re: Use hook, null value

GeoB99
Moderator Off Offline

Quote
@user Mami Tomoe: 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.
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.

old Re: Use hook, null value

Rainoth
Moderator Off Offline

Quote
@user HESY: Where there's a will there's a way.
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 GeoB99'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

old Re: Use hook, null value

HESY
User Off Offline

Quote
@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

old Re: Use hook, null value

GeoB99
Moderator Off Offline

Quote
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.

old Re: Use hook, null value

Rainoth
Moderator Off Offline

Quote
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.

old Re: Use hook, null value

HESY
User Off Offline

Quote
@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)

old Re: Use hook, null value

Rainoth
Moderator Off Offline

Quote
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.

old Re: Use hook, null value

HESY
User Off Offline

Quote
@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
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

old Re: Use hook, null value

Rainoth
Moderator Off Offline

Quote
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.
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
Log in to reply Scripts overviewCS2D overviewForums overview