Forum

> > CS2D > Scripts > How to make a Command processor system?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to make a Command processor system?

9 replies
To the start Previous 1 Next To the start

old How to make a Command processor system?

Louie
User Off Offline

Quote
Ok so i'm confused about this,that's why i need help to do this.So lets say i have a command table:
1
2
3
4
commands = {
{"!kick",kick_Command,rank = 2}
{"!mute",mute_Command,rank = 1}
}
Then when a player types a command then it would match the text that the player typed to the table, so if what the player typed in is in the commands table then it would check their rank, and if their rank is sufficient then it would call in the command(kick_Command or mute_Command), how do i do those things? its seems pretty hard for me

old Re: How to make a Command processor system?

Rainoth
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- say there's a table rank = {}
-- say you load it from files and set everyone's rank to be different for example on team selection you check if there's a file with player usgn and then assign him rank[id] = what's read in the file if it exists

commands = {
{"!kick",DEFINE YOUR FUNCTION HERE,rank = 2}
{"!mute",AND HERE,rank = 1}
}

-- then you check player rank like so

for k,v in pairs (commands) do
	if txt == commands[v][1] then
		if rank[id] == commands[v][3] then
			commands[v][2]() -- not sure about this part
		end
	end
end
Well, apart from function calling, it should pretty much work.

If you want, I could send you an old script of mine that I used on my server, it might be a bit buggy cuz I just loved to play around and make tons of crap on it, but it does have a similar system to what you're looking for.

old Re: How to make a Command processor system?

Louie
User Off Offline

Quote
@user Rainoth: Ok so i made this:
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
39
40
41
42
43
44
owner = {140639}
rank = {}

for id = 1,32 do
rank[id] = 0
end

commands = {
{"!kick",kickcmd,rank = 2};
{"!slap",slapcmd,rank = 1};
}

addhook("join","setrank")
function setrank(id)
   for _, usgn in ipairs(owner) do
     if player(id,"usgn") == usgn then
        rank[id] = 3
     end
   end
end

function kickcmd(id,iid,rsn)
   if not rsn then rsn = "No reason specified"
   msg(player(id,"name").." kicked "..player(iid,"name").." for: "..rsn)
   parse("kick "..iid.." "..rsn)
   end
end

function slapcmd(id,iid)
   parse("slap "..iid)
end

addhook("say","_say")
function _say(id,t)
   for k, v in pairs(commands) do
       if t == commands[v][1] then
          if rank[id] >= commands[v][3] then
             commands[v][2]()
          end
         return 1
       end
       msg2(id,"Error,invalid command!")
   end
end
but i always get this error:
1
LUA ERROR: sys/lua/test.lua:36: attempt to index field '?' (a nil value)

old Re: How to make a Command processor system?

Rainoth
Moderator Off Offline

Quote
Try changing to this
1
2
3
4
5
commands = 
{
{"!kick",kickcmd,rank = 2},
{"!slap",slapcmd,rank = 1}
}

Also, even if that part works, the way you call functions won't work. Your functions require arguments but in function call there's no arguments.

old Re: How to make a Command processor system?

eledah
User Off Offline

Quote
And remember that you have to use
1
v[1]
to get "!kick", "!mute", whatever instead of
1
commands[v][1]


Also, you need to use string.sub() in order to extract the player's ID. Something like this.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
addhook("say","_say")
function _say(id,t)
	local array = text_extract(t)
	for k, v in pairs(commands) do
		if array[1] == v[1] then
			if rank[id] >= v[3] then
				v[2](array[2], array[3])
			end
			return 1
		end
	end
	msg2(id,"Error,invalid command!")
end

function text_extract(text)
	--[[
		!kick 4 Used hacks
		 |
		 v
		!kick
		4
		Used hacks
	]]
end

old Re: How to make a Command processor system?

_Yank
User Off Offline

Quote
And also, in case you didn't know, you cant use a number to access a value, in an array, that has been defined with a string.

tab = {"Hello", "Mercyless", "World", author = "Me"}
(You can't access the author value with tab[4] but with tab.author)

Concluding,
1
commands[v].rank ~= commands[v][3]

old Re: How to make a Command processor system?

Louie
User Off Offline

Quote
@user eledah:
Ok so i tried this:
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
39
40
41
42
43
44
45
46
47
48
49
owner = {140639}
rank = {}

for id = 1,32 do
rank[id] = 0
end

commands = {
{"!kick",kickcmd,rank = 2};
{"!slap",slapcmd,rank = 1};
}

addhook("join","setrank")
function setrank(id)
   for _, usgn in ipairs(owner) do
     if player(id,"usgn") == usgn then
        rank[id] = 3
     end
   end
end

function kickcmd(id,iid,rsn)
   if not rsn then rsn = "No reason specified"
   msg(player(id,"name").." kicked "..player(iid,"name").." for: "..rsn)
   parse("kick "..iid.." "..rsn)
   end
end

function slapcmd(id,iid)
   parse("slap "..iid)
end

addhook("say","_say")
function _say(id,t)
   local array = extract(t)
   for k, v in pairs(commands) do
       if array[1] == v[1] then
          if rank[id] >= command[v].rank then
             v[2](array[2],array[3])
          end
         return 1
       end
       msg2(id,"Error,invalid command!")
   end
end
       
function extract(txt)
   string.sub(txt)
end
but i get a new error:
1
LUA ERROR: sys/lua/test.lua:48: bad argument #2 to 'sub'(number expected,got no value)
i think i i didn't properly used the string.sub()
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview