Forum

> > CS2D > Scripts > How to make some command
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to make some command

6 replies
To the start Previous 1 Next To the start

old Re: How to make some command

Rainoth
Moderator Off Offline

Quote
1
2
3
4
5
6
addhook("say","say_cmd")
function say_cmd(id,t)
	if t:sub(1,4) == "!cmd" then
		msg2(id,"You called a command")
	end
end

Not necessarily for say hook, can be in console too. That's a simple way when you don't want to make complicated commands and just a few of them.

old Re: How to make some command

GeoB99
Moderator Off Offline

Quote
Tip: Never use string.sub() in case if you implement many commands for your script. It makes things slower in terms of time. Pattern matching is way more powerful and it's not required to put patterns on every side of a command.

Unless if you just implement few things such as 3 commands, you're freely to use string.sub(). Here you can find further information and instructions about it below:

> thread cs2d [Tip] Stop using string.sub() for commands

old Re: How to make some command

Dousea
User Off Offline

Quote
I agree with user GeoB99. This is the wrap of everything user VaiN said.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
local function split(s, d)
	local w = {}
	
	s:gsub(("[^%s]+"):format(d or " "), function(v)
		w[#w + 1] = v
	end)
	
	return w
end

function sayhook(i, t)
	local c = t:match("^([!|@][%w]+)[%s]?")
	local d = t:match("[%s](.*)")
	
	if (c and d) then
		-- This is the place where you add commands
		if (c == "kill") then
			-- This is not error-safe due to no checkup for the arguments' type (d)
			parse(("killplayer %d"):format(tonumber((split(d))[1]))")
		end
	end
end

addhook("say", "sayhook")
edited 1×, last 03.04.16 12:50:02 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview