Forum

> > CS2D > Scripts > How can I shorten this?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How can I shorten this?

9 replies
To the start Previous 1 Next To the start

old How can I shorten this?

mozilla1
User Off Offline

Quote
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
function command_entry(id,txt)
	p = {}
	for word in string.gmatch(txt, "%S+") do
		table.insert(p,word)
	end 
	
		if p[1] == "@kill"         then kill(id,p[2])
	elseif p[1] == "@kick"         then kick(id,p[2])
	elseif p[1] == "@spawn"        then spawn(id,p[2])
	elseif p[1] == "@setcolor"     then setcolor(id,p[2])
	elseif p[1] == "@setcustomlvl" then customlevel(id,p[2])
	elseif p[1] == "@equip"        then equip(id,p[2],p[3])
	elseif p[1] == "@help"         then	help(id)
	elseif p[1] == "@fake"         then fake(id,p[2])
	elseif p[1] == "@faketo"       then faketo(id,p[2],p[3])
	elseif p[1] == "@speedmod"     then speedmod(id,p[2],p[3])
	elseif p[1] == "@listusers"    then listusers(id)
	elseif p[1] == "@check"        then check(id,p[2])
	elseif p[1] == "@adduser"      then adduser(id,p[2],p[3])
	elseif p[1] == "@makespecall" or p[1] == "@specall" then makespecall(id)
	elseif p[1] == "@makespec"    or p[1] == "@spec"    then makespec(id,p[2])
	elseif p[1] == "@maket"       or p[1] == "@tr"      then maket(id,p[2]) 
	elseif p[1] == "@makect"      or p[1] == "@ct"      then makect(id,p[2])
	elseif p[1] == "@restart"     or p[1] == "@rr"      then restart(id)
	else msg("©"..colorlist[id].."["..customlvl[id].."] "..player(id,"name")..": "..txt)	
	end
end

I want to load every function according to the first parameter. But I guess this form is inappropiate to handle all things. Just wanna know if there is a better way to do this.


Thanks.

old Re: How can I shorten this?

VADemon
User Off Offline

Quote
A better and a faster way:
(just an example)
1
2
3
4
local execute_function = {
"@kill" = function () kill(id,p[2]) end,
"@kick" = function() kick(id,p[2]) end
}
And so on...

1
2
3
4
5
if execute_function[p[1]] then
execute_function[p[1]](id,p[2],p[3])
else
--your awesome @#!* here ;)
end

...aaaand you don't know a lot about Lua (variables part)

old Re: How can I shorten this?

archmage
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function command_entry(id,txt)
     p = {}
     for word in string.gmatch(txt, "%S+") do
          table.insert(p,word)
     end 

	if ( #p < 1 ) then return false end

	-- store the command and remove it from the table
	local cmd = p[1]
	table.remove(p, 1)
	
	-- attempt to call a function named cmd
	local status, err = pcall(_G.[cmd], unpack(p))

	if ( not status ) then
		print(err)
	end
end

old Re: How can I shorten this?

Flacko
User Off Offline

Quote
I'd like to point out that user archmage's solution could be improved.

Put all your functions such as kill() kick() and spawn() in a table, for example
1
2
3
4
5
6
7
8
9
10
commands = 
{
	kill = function(id,etc)
		--stuff
	end,
	kick = function(id,etc)
		--stuff
	end,
	--and so on
}
Otherwise, there's a big risk in letting the user call functions from the global environment.

1
p = {}
bad.
1
local p = {}
good.

1
2
local cmd = p[1]
table.remove(p, 1)
The "@" symbol is still at the beginning of the command.
1
local cmd = table.remove(p,1):sub(2, -1)
The "@" is gone.

1
_G.[cmd]
If you already put the commands in the commands table, change to
1
commands[cmd]

old Re: How can I shorten this?

archmage
User Off Offline

Quote
user Flacko has written
Otherwise, there's a big risk in letting the user call functions from the global environment.

Can't believe I didn't think about that. Thanks Flacko.

old Re: How can I shorten this?

omg
User Off Offline

Quote
best idea would probably be to leave the @ where it is. it wont cause that much inefficiency, especially since its already in the table

old Re: How can I shorten this?

DannyDeth
User Off Offline

Quote
user VADemon has written
A better and a faster way:
(just an example)

Lua uses a kind of linked list for tables, therefore accessing tables is quite slow. The massive if statement is actually faster, since you are doing the exact same thing but adding a table reference.

old Re: How can I shorten this?

mozilla1
User Off Offline

Quote
I finished according to what i've seen here.
Just ignore the "print", I used this to debug.


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
function command_entry(id,txt)
	local p = {}
	for word in string.gmatch(txt, "%S+") do
		table.insert(p,word)
	end
-- Verifica se o comando não tiver parametros	
	if (#p < 1) then
		parse("sv_msg2 "..id.." ©255000000Erro, parâmetro não definido. (Teste)")
		return
	end
	
-- Armazena o comando e depois remove da tabela	
	local cmd = p[1]:sub(2,-1)
	table.remove(p,1)

	for v in ipairs(p) do
		print(v.." Type: "..type(v))
	end
	
-- Tentativa de chamar uma função chamada cmd	
    local status, err = pcall(commands[cmd],id,unpack(p))
    if (not status) then
		print(err)
    end
end


Actually, which one is faster? I'm quite trivied now since I saw the last reply here.

old Re: How can I shorten this?

VADemon
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
runs = 1000000

function benchmark()
local run=runs
local time_start=os.clock()
--Insert code here:

for i=1,run do

--your awesome shit as function here ;)

end

--End of code
local time_end=os.clock()
print("Started: "..time_start..", ended: "..time_end..", difference: "..time_end-time_start)

end
Try it out, locally in your Lua.


But I still don't understand the 3rd part of your script (pcall? printing error? wtf?!)

@user DannyDeth:
A small example maybe? I don't understand the last sentence.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview