Forum

> > CS2D > Scripts > Menu Slot
Forums overviewCS2D overview Scripts overviewLog in to reply

English Menu Slot

4 replies
To the start Previous 1 Next To the start

old Menu Slot

Precel97
User Off Offline

Quote
Hi ! Can someone tell me how to add slot to menu if its possible?
I mean : For Example There empty menu and when is say !addslot then slot is added to menu.
Help plz

old Re: Menu Slot

Cons
User Off Offline

Quote
Sorry i dindnt understand you question, but if is what i am thinking about, its this:: (Just Put a , for every slot you want)
1
2
3
4
5
6
addhook("serveraction" , "sa")
function sa(id,action)
	if action==1 then 
	menu(id,"Menu Name!!,Name of slot¹,Name of slot²")
	end
end

Sorry, i hope is this you are findinig

old Re: Menu Slot

Szkieletor
User Off Offline

Quote
He means a script, that will work like this:

if chat message=!addslot " " then
     Add slot to menu

This may be requiring live parsing of lua, there are scripts for it somewhere in File Archive.

old Re: Menu Slot

Lee
Moderator Off Offline

Quote
it's not too hard to do

1
2
3
4
5
6
7
8
9
10
11
12
13
my_menu = "Title" -- global declaration
-- assuming that you have totable/string.split

-- Menu builder
addhook("say", "menu_cmds")
function menu_cmds(id, txt)
	txt = txt:split()
	if txt[1] == "!addslot" then
		my_menu = my_menu .. "," .. txt:sub(#"!addslot")
	elseif txt[1] == "!showmenu" then
		menu(id, my_menu)
	end
end

Now the tricky part is to get the items back again.

1
2
3
4
5
6
7
8
9
10
addhook("menu", "menu_handler")
function menu_handler(id, title, sel)
	if title ~= "Title" then return end
	-- Get the table of the selections, remember that they are delimited by commas
	local m = my_menu:split(",")
	-- m = {"Title", ...} <- plus 1 offset to selection
	local selection = m[sel+1]
	-- selection contains the text of the selection
	... <- your own code here
end

You can also do some validation checks in the first phase by making sure that no commas are in the builder:

1
2
3
4
--Change
		my_menu = my_menu .. "," .. txt:sub(#"!addslot")
-- To
		my_menu = my_menu .. "," .. txt:sub(#"!addslot"):split(",")[1]

Example:
Adding maps to a votemap script

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
30
31
32
33
34
35
36
37
38
TITLE = "Votemap"
my_menu = title.."@b" -- or whatever the large one is
-- assuming that you have totable/string.split

maps = {}

-- Menu builder
addhook("say", "menu_cmds")
function menu_cmds(id, txt)
	txt = txt:split()
	if txt[1] == "!addmap" then
		my_menu = my_menu .. "," .. txt:sub(#"!addslot")
	elseif txt[1] == "!votemap" then
		menu(id, my_menu)
	elseif txt[1] == "!tally" then
		local max = -1
		local target_map = ""
		for map,votes in pairs(maps) do
			if votes>max then
				max = votes
				target_map = map
			end
		end
		msg("Changing map to "..target_map)
		parse("map "..target_map)
		-- You can also make your script more secure
		-- parse("map "..target_map:split(";")[1])
	end
end

addhook("menu", "menu_handler")
function menu_handler(id, title, sel)
	if title ~= TITLE then return end
	local m = my_menu:split(",")
	local map = m[sel+1]
	if not maps[map] then maps[map] = 0 end
	maps[map] = maps[map] + 1
end

The string.split/totable function is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function string.split(t, b)
	local cmd = {}
	local match = "[^%s]+"
	if b then
		match = "%w+"
	end
	if type(b) == "string" then match = "[^"..b.."]+" end
	if not t then return invalid() end
	for word in string.gmatch(t, match) do
		table.insert(cmd, word)
	end
	return cmd
end

totable = string.split

EDIT:
Use at your own risk, I wrote the entire thing in this textarea so there will probably be a few bugs. Foremost of all, don't shadow the maps function with another identifier.

old Re: Menu Slot

Precel97
User Off Offline

Quote
my_menu = "Title" -- global declaration
-- assuming that you have totable/string.split

-- Menu builder
addhook("say", "menu_cmds")
function menu_cmds(id, txt)
txt = txt:split()
if txt[1] == "!addslot" then
my_menu = my_menu .. "," .. txt:sub(#"!addslot")
elseif txt[1] == "!showmenu" then
menu(id, my_menu)
end
end
not work

attempt to call a method sub (a nil value)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview