*Introduction:
Info: This script is stable and propably doesn't have any bugs.
Hi everyone, i've recently thinking about writing command-handler, but there was one little problem..
Command-handler would be useful in both sites (client and server), so i write it a piece of code,
which you can load on both sides. You don't have to load it in both sides, you can load this script freely.
Script works perfectly, but i'm thinking on better way, to identify script-side.
License
Download
*List of functions:
void addCommand(string or array cmd, void callback)
void removeCommand(string or array cmd, [[optional: void callback ]])
Including script in xml:
Info: I'm recommending you, to place script in default lib folder.
<script src="lib/cmd-handler.nut" type="client" />
<script src="lib/cmd-handler.nut" type="server" />
*Examples:
*Adding command(s):
*Removing command(s):
Info: This script is stable and propably doesn't have any bugs.
Hi everyone, i've recently thinking about writing command-handler, but there was one little problem..
Command-handler would be useful in both sites (client and server), so i write it a piece of code,
which you can load on both sides. You don't have to load it in both sides, you can load this script freely.
Script works perfectly, but i'm thinking on better way, to identify script-side.
License
Download
*List of functions:
void addCommand(string or array cmd, void callback)
void removeCommand(string or array cmd, [[optional: void callback ]])
Including script in xml:
Info: I'm recommending you, to place script in default lib folder.
<script src="lib/cmd-handler.nut" type="client" />
<script src="lib/cmd-handler.nut" type="server" />
*Examples:
*Adding command(s):
Squirrel Script
- // -----[Client-Side]-----
-
- addCommand("quit",function(arg) // our function must have it's own argument. argument "arg" is command parameters
- {
- exitGame()
- })
-
- addCommand(["q","quit","exit"],function(arg) // we have registered one function in three diffrent commands
- {
- exitGame()
- })
-
- // -----[Server-Side]-----
-
- addCommand("ban",function(pid,arg) // our function must have two arguments. First is the player id that typed the command, second is command parameters.
- {
- if (arg != "")
- {
- arg = sscanf("dds",arg) || sscanf("dd",arg) || sscanf("d",arg) // we can add some "optional" command parameters, this is the example of it.
-
- if (type(arg) == "array")
- {
- if (2 in arg) // checking if arg[2] value exists
- {
- sendMessageToAll(255,0, 0,"Id"+pid+" banned id "+arg[0]+" for "+arg[1]+"minutes. Reason: "+arg[2])
- ban(pid,arg[0],arg[1])
- }
- else if (1 in arg) // checking if arg[1] value exists
- {
- sendMessageToAll(255,0, 0,"Id "+pid+" banned id "+arg[0]+" for "+arg[1]+"minutes.")
- ban(pid,arg[0],arg[1],"none")
- }
- else
- {
- sendMessageToAll(255,0, 0,"Id "+pid+" banned id "+arg[0]+".")
- ban(pid,-1,"none")
- }
- }
- else
- {
- sendMessageToPlayer(pid,255,0, 0,"Wrong syntax, <pid> or <minutes> argument must be integer!")
- }
- }
- else
- {
- sendMessageToPlayer(pid,255,0, 0,"Wrong syntax, type: /ban (pid) <minutes> <reason>") // command parameters in () are required, but parameters <> are optional.
- }
- })
*Removing command(s):
Squirrel Script
- // -----[Client-Side]-----
-
- function tp1() // creating some function
- {
- setPlayerPosition(heroId,0,0,0)
- }
-
- addCommand("test",tp1) // we are hooking tp1 function into "test" command
-
- addCommand("test",function(arg) // we can add more than one function into our command
- {
- setPlayerAngle(heroId,0)
- })
-
- removeCommand("test",tp1) // this line removes only tp1 function from "test" command (function tp1 still exists)
-
- // -----[Server-Side]-----
-
- addCommand(["print","type"],function(pid,arg)
- {
- print(pid+" typed: "+arg)
- }
-
- addCommand(["print","type"],function(pid,arg)
- {
- print(pid+" name is "+getPlayerName(pid))
- }
-
- removeCommand(["print","type"]) // this line removes all hooks from commands "print" and "type", we can remove more than one command