Forum

> > CS2D > Scripts > "use" hook, script runs well.
Forums overviewCS2D overview Scripts overviewLog in to reply

English "use" hook, script runs well.

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

old "use" hook, script runs well.

Yazir
User Off Offline

Quote
Hello, i have a problem, that i can't solve. I am making my first script for my map and i don't know how to use use hook.
Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
iskipv = 0
addhook("use","introskip")
function introskip(x,y)
	if (x == 67) then
		if (y == 68) then
				if (iskipv == 0) then
				msg("CT team pressed intro skip. "..iskipv.."@C")
				iskipv = iskipv + 1
				end
			end
		end
	end
end

It runs well. I want to make it show message, that button at xy was pressed and shows variable. Also, there's the tr._use on the map

old Re: "use" hook, script runs well.

Flacko
User Off Offline

Quote
You've skipped some parameters for the use hook cs2d lua hook use
Remember that in lua parameters are passed by order, not by name.
So if the function declaration looks like this:
1
use(id, event, data, x, y)
And you declared it like this:
1
use(x, y)
What you now have in x is the player id and in y you have the event data instead of having the x and y coordinates.

To fix your problem you just have to replace this:
1
function introskip(x,y)
with this:
1
function introskip(id, event, data, x, y)

old Re: "use" hook, script runs well.

Yazir
User Off Offline

Quote
Oh, so it works like this, thanks! I'm still learning.

@edit I have another problem. It works, but how to connect it with map, tr._if iskipv == 1, doesn't work.

@edit2 also is there any way to use in-game trigger activate a script, like trigger 123 activates function etc.
edited 3×, last 26.01.15 11:10:40 am

old Re: "use" hook, script runs well.

Yazir
User Off Offline

Quote
Thanks! That's a great function. However i have another question.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
addhook("menu","bpress")
function bpress(id, tit, but)
	if(tit == "Menu") then
		if(but == 1) then
		elseif (but == 2) then
			menu(id,"Status, Slots: ".. "128" ..",sht,kvnt,prik")
		end
	elseif (tit == "Inventory") then
		menu(id,"Inventory "..eqpage..","..ekwit[eqpage]..","..ekwit[eqpage+1]..","..ekwit[eqpage+2]..","..ekwit[eqpage+3]..","..ekwit[eqpage+4]..","..ekwit[eqpage+5]..",".."Previous page"..",Next page"..",Back to menu")
		if(but == 9) then
			msg("tu bedzie funkcja wracajaca do poczatku")
		elseif(but == 8) then
			eqpage = eqpage + 1
		elseif(but == 7) and (eqpage >= 0) then
			eqpage = eqpage - 1
		end
	end
end
The menu Inventory, doesn't show up.

Also, can you tell me how to use function to return a text, and how to call a function inside of a code like in
..ekwit[eqpage+5]..",".."Previous page"..",Next page"..",Back to menu")
To make "Previous page" be named "(Previous page)", so it wouldn't be clickable?

old Re: "use" hook, script runs well.

Bowlinghead
User Off Offline

Quote
I dont know what you want to ask.
Actually your code is wrong I thing.
The menu hook is not for creating a menu! It is used for executing an action if you press a button on a menu.
The declaration of a menu is his title name. You are not allowed to have 2 different menus with the same title name! (The game can´t seperate them then!)

Basics >


Next/Previous Page >


Code not tested!

Links:
http://www.cs2d.com/help.php?hookcat=all&hook=menu#hook
http://www.cs2d.com/help.php?luacat=all&luacmd=menu#cmd

old Re: "use" hook, script runs well.

Yazir
User Off Offline

Quote
The code isn't wrong, the only problem is that inventory won't open. Maybe end in wrong place? The menu open is in another function.

@edit
Btw. what is "action: action key 1,2 or 3" It means you can have only 3 binded keys?

old Re: "use" hook, script runs well.

Bowlinghead
User Off Offline

Quote
I need the whole code to see if there is something wrong.
At line 8 and 9 is the error I think:
1
2
elseif (tit == "Inventory") then
          menu(id,"Inventory "..eqpage..","..ekwit[eqpage]..","..ekwit[eqpage+1]..","..ekwit[eqpage+2]..","..ekwit[eqpage+3]..","..ekwit[eqpage+4]..","..ekwit[eqpage+5]..",".."Previous page"..",Next page"..",Back to menu")

You search the menu "Inventory" and in the next line you create it.
The addhooks/functions are the triggers, the events. And the menu hook only gets triggered if someone presses a button in a menu. Usually you don´t create a menu in a menu addhook.

If you give me the whole code I´ll know what your problem is.

Anyway, when you run your code and the code has erros you can check the console. Check the red line and try to fix it yourself.

Note:
Line 4 isn´t nessesary.
1
2
3
if(but == 1) then
-- No command?
elseif (but == 2) then
You can change it to this:
1
2
3
4
if(tit == "Menu") then
          if (but == 2) then
               menu(id,"Status, Slots: ".. "128" ..",sht,kvnt,prik")
          end


EDIT:
Serveraction keys are programmable keys. Default they are F2, F3 and F4. That is the only way to bind keys server sided.
With small tricks you can use something like "G" (alias the drop key). Watch this. But it´s ugly. Try to avoid it.

old Re: "use" hook, script runs well.

Yazir
User Off Offline

Quote
I've put it to work, to show all the menus, however cmds won't execute :@.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
eqpage = 0
function ekwi(id,key) -- hooked to serveraction
if (key == 3) then
	menu(id,"Menu,Inventory,Status,stuff,even more stuff")
	end
end

function bpress(id, tit, but)
	if(tit == "Menu") then
		if (but == 1) then
			menu(id,"Inventory "..eqpage..","..ekwit[eqpage]..","..ekwit[eqpage+1]..","..ekwit[eqpage+2]..","..ekwit[eqpage+3]..","..ekwit[eqpage+4]..","..ekwit[eqpage+5]..",".."Previous page"..",Next page"..",Back to menu")
			if(but == 7) and (eqpage >= 0) then -- cmds here don't work
				eqpage = eqpage - 1
			elseif(but == 8) then -- cmds here don't work
				eqpage = eqpage + 1 -- cmds here don't work
			elseif(but == 9) then
				msg2(0,"tu bedzie funkcja wracajaca do poczatku")  -- cmds here don't work
			end
			elseif (but == 2) then
				menu(id,"Status, Slots: ".. "128" ..",shit,kvnt,prik")
		end
	end
end


@edit, fixed it a little, still does the same thing
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
eqpage = 0
function ekwi(id,key) -- hooked to serveraction
if (key == 3) then
	msg2(0,"test")
	menu(id,"Menu,Inventory,Status,stuff,even more stuff")
	end
end

function bpress(id, tit, but)
	if(tit == "Menu") then
		if (but == 1) then
			msg2(0,"test")
			menu(id,"Inventory,".." "..eqpage..","..ekwit[eqpage]..","..ekwit[eqpage+1]..","..ekwit[eqpage+2]..","..ekwit[eqpage+3]..","..ekwit[eqpage+4]..","..ekwit[eqpage+5]..",".."Previous page"..",Next page"..",Back to menu")
		elseif(tit == "Inventory") then
			if(but == 7) then -- cmds here don't work
				eqpage = eqpage - 1
				msg2(0,"test")
			elseif(but == 8) then -- cmds here don't work
				eqpage = eqpage + 1 -- cmds here don't work
				msg2(0,"test")
			elseif(but == 9) then
				msg2(0,"test")  -- cmds here don't work
			end
		elseif (but == 2) then
				menu(id,"Status, Slots: ".. "128" ..",shit,kvnt,prik")
				msg2(0,"test")
		end
	end
end

@edit2 YEAAAHH BABYY! I have made it! :):):)smileyface:). Here is the code if anyone wondered.
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
eqpage = 0
function ekwi(id,key) -- hooked to serveraction
if (key == 3) then
	msg2(0,"open menu")
	menu(id,"Menu,Inventory,Status,stuff,even more stuff")
	end
end

function bpress(id, tit, but)
	if(tit == "Menu") then
		if (but == 1) then
			msg2(0,"open inventory")
			menu(id,"Inventory".." "..eqpage..","..ekwit[eqpage]..","..ekwit[eqpage+1]..","..ekwit[eqpage+2]..","..ekwit[eqpage+3]..","..ekwit[eqpage+4]..","..ekwit[eqpage+5]..",".."Previous page"..",Next page"..",Back to menu")
		end
	elseif(tit == "Inventory".." "..eqpage) then
			if(but == 7) then -- cmds here don't work
				eqpage = eqpage - 1
				msg2(0,"button 7")
			elseif(but == 8) then -- cmds here don't work
				eqpage = eqpage + 1 -- cmds here don't work
				msg2(0,"button 8")
			elseif(but == 9) then
				msg2(0,"button 9")  -- cmds here don't work
			end
		elseif (but == 2) then
				menu(id,"Status, Slots: ".. "128" ..",shit,kvnt,prik")
				msg2(0,"open status")
	end
end



@edit4 I have made it, it works and all the stuff, but i need to shorten
1
menu(id,"Inventory".." "..tostring(math.floor(eqpage/6)+1)..","..ekwit[eqpage]..","..ekwit[eqpage+1]..","..ekwit[eqpage+2]..","..ekwit[eqpage+3]..","..ekwit[eqpage+4]..","..ekwit[eqpage+5]..",".."Previous page"..",Next page"..",Back to menu")
this cmd, becouse i have to copy it multiple times, and also change it sometimes, and that's pain in the ass. What can i do to write it once, then link it like variable?
edited 4×, last 27.01.15 04:16:57 pm

old Re: "use" hook, script runs well.

Bowlinghead
User Off Offline

Quote
With a function.
I already explained it a little bit.
You can create functions by yourself.

Just write this at the top of your script:
1
2
3
function my_menu(id)
	menu(id,"Inventory".." "..tostring(math.floor(eqpage/6)+1)..","..ekwit[eqpage]..","..ekwit[eqpage+1]..","..ekwit[eqpage+2]..","..ekwit[eqpage+3]..","..ekwit[eqpage+4]..","..ekwit[eqpage+5]..",".."Previous page"..",Next page"..",Back to menu")
end

If you want to open the menu, you just have to write this:
1
my_menu(id)

Edit:
You can use functions for everything. If you have the same code that gets executed multiple times then use a function.
I´m happy that your code is working

Edit 2:
What do you need to change? You can use parameters for this

old Re: "use" hook, script runs well.

Yazir
User Off Offline

Quote
Yes! Thanks! You're the best! Those things you have wrote are very helpfull for me . I'll put your name in the credits anfter i complete my mod if you don't mind.

old Re: "use" hook, script runs well.

Bowlinghead
User Off Offline

Quote
Arrays are like... multiple variables with the same name...
I can´t explain them good though.
You are also able to put arrays into arrays. Do you mean this with 2d arrays?

old Re: "use" hook, script runs well.

Yazir
User Off Offline

Quote
I know what arrays are, i know what arrays do, but i don't know how to use them in mine script.
2d arrays mean
2darray = { {x},{y} }
Two 1d arrays in one array make 2d array, like x/y position.

I have to put inventory array, and some other variables for each player by id (32 max).

@edit
To say it in another words, in 2d array you can have more than 1 variable per array[number(idk how it's named)]

@edit2
After thinking about it for 3 hours, i guess it doesn't need 2d array. Or it does. I don't know. Array in array messes everything up. It's too hard to handle it by me in any way. I need help .
edited 2×, last 27.01.15 10:10:09 pm

old Re: "use" hook, script runs well.

Bowlinghead
User Off Offline

Quote
Do you mean sth like this?

Code tested! It works!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- Creates an array
function initArray()
	local array = {}
	for i = 1, 32 do -- count from 1 to 32 (For every player ID)
		array[i]={x=0,y=0} -- Every value is "{0,0}" (x and y are the names)
	end
	return array
end
position = initArray()

addhook("movetile","save_move_data")
function save_move_data(id,x,y)
	position[id][x] = x -- set new x
	position[id][y] = y -- set new y
	msg("Player "..player(id,"name").." changed his position!")
	msg(player(id,"name").."s new position is: "..position[id][x].." | "..position[id][y])
end
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview