Forum

> > CS2D > Scripts > How to create a table
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to create a table

9 replies
To the start Previous 1 Next To the start

old How to create a table

prow
User Off Offline

Quote
Here , i made a simple table to execute commands , with messages
SORRY FOR MY BAD ENGLISH :@

Here we have the table
you can edit it and add more commands


1
2
3
4
5
6
7
8
9
10
11
funcandvar = {
[1] = function(id)
	parse("speedmod "..id.." 100")
end,
[2] = function(id)
	...
end,
[3] = function(id)
	...
end,
}

here the messages for some command:

1
funcandmsg = {"speedmoded","...","..."}

you can add more.

to execute with this


1
2
funcandvar[...](id) -- execute the commands
funcandmsg[...] -- messages

you can execute this in any hook

so i made a simple hook with math.random in some function
then:

1
2
3
4
5
6
7
8
addhook("say","tableuse")
function tableuse(id,txt)
	if (txt=="table") then
		local exec=math.random(1,#funcs)
		funcs[exec](id)
		msg(funcandmsg[exec])
	end
end

so the code would remain


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
funcandvar = {
[1] = function(id)
	parse("speedmod "..id.." 100")
end,
[2] = function(id)
	...
end,
[3] = function(id)
	...
end,
}

funcandmsg = {"speedmoded","...","..."}

addhook("say","tableuse")
function tableuse(id,txt)
	if (txt=="table") then
		local exec=math.random(1,#funcs)
		funcs[exec](id)
		msg(funcandmsg[exec])
	end
end

old Re: How to create a table

prow
User Off Offline

Quote
is for those who need to execute commands without making things too long, it would be for people just starting to program, to help

old Re: How to create a table

VADemon
User Off Offline

Quote
You have problems with making a function inside of a table?
I would do this this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
text = 15
table_func = {}

function insert_tbl_func(name_of_function)
table.insert(table_func,name_of_function)
end

function big_message(text)
print(text.."@C")
end

insert_tbl_func(big_message)

table_func[1](text)
Try out the code: http://www.lua.org/cgi-bin/demo

old Re: How to create a table

prow
User Off Offline

Quote
user VADemon has written
You have problems with making a function inside of a table?
I would do this this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
text = 15
table_func = {}

function insert_tbl_func(name_of_function)
table.insert(table_func,name_of_function)
end

function big_message(text)
print(text.."@C")
end

insert_tbl_func(big_message)

table_func[1](text)
Try out the code: http://www.lua.org/cgi-bin/demo


post your thread
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview