Skip to content

Instantly share code, notes, and snippets.

@D4edalus
Created March 8, 2017 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save D4edalus/a038fb89056a9fdc37ccfbd4e031b5de to your computer and use it in GitHub Desktop.
Save D4edalus/a038fb89056a9fdc37ccfbd4e031b5de to your computer and use it in GitHub Desktop.
/*
Called by the server when a registered function is executed
*/
CodeCallback_ScriptCommand(command, arguments)
{
executeRegisteredCommand(command, arguments);
}
/*
enter "$testcmd asdf 123 fdas" into chat to execute
*/
testcmd(args)
{
iprintln("Called Testcmd: ");
if(isDefined(args))
{
for(i = 0; i < args.size; i++)
iprintln("Arguemnt " + i + " = " + args[i]);
}
}
/*
registers a new script command by storing it into an array with alias and function pointer
*/
registerScriptCommand(command, fun)
{
addscriptcommand(command, 1); // registers the function to be called by: CodeCallback_ScriptCommand
if(!isDefined(level.registeredCommands))
level.registeredCommands = [];
level.registeredCommands[command] = fun;
}
/*
looks up the command alias, and executes the function iff it exists
*/
executeRegisteredCommand(command, args)
{
cmd = level.registeredCommands[command];
if(isDefined(cmd))
{
argsarr = strtok(args, " ");
[[cmd]](argsarr);
}
else
{
// command does not exist
}
}
/*
already existing function, just add the register part to it
*/
CodeCallback_StartGameType()
{
registerScriptCommand("testcmd", ::testcmd);
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment