Commands/Lua

Reference for console commands, Lua hooks and Lua commands in CS2D.

CS2D Command CS2D Console Commands

Lua Hook Lua Hooks

Lua Command Lua Commands

Category: basic (14)

Lua Command addhook

Categories

Parameters

  • "hook"
  • "func"
  • [prio] (optional)

Info

Attaches the Lua function "func" to the hook "hook". CS2D will always call that Lua function when the event related to that hook occurs.
You can add an unlimited amount of different functions to each hook - but bear in mind that this might affect the performance in a negative way. Especially for hooks which are called frequently.

Click here for a list of all available hooks

Hook Parameters
CS2D passes parameters to most hooked Lua functions. These parameters contain information about the event that occurred and triggered the hook. If you want to use these parameters you have to define them in the function you add to the hook. Otherwise they won't be available in your script. You only have to add the parameters you actually use but they have to be in the same order as defined in the hook. So if you only need the last one you also have to define all other ones.

Hook Return Value
Some of the hook-functions can also have a return value. This return value influences the way CS2D reacts. Returning nothing is equal to returning 0, "" or nil.
When returning nothing CS2D will react in the normal way - as if no Lua script was executed.
In many cases you can skip the action in CS2D by returning 1 (return 1) in a hooked Lua function.
Read the description of the hook you want to use to learn more about possible return values.

Hook Priority (Optional)
Priority is only relevant if there is more than one function attached to the same hook.
  • Default priority is 0
  • higher numbers = higher priority
  • lower numbers (negative is allowed too) = lower priority
The attached function with the HIGHEST priority is the function which will be executed LAST. CS2D will take the return value of this last function (if there is any) only. All other return values will be ignored. This is because CS2D can only react in ONE way at a time and therefore it can only process ONE return value even though there are multiple hooked functions.
In most cases you will just omit the priority parameter (it is [optional]).

Example: Show a custom message if a player says something. Return 1 suppresses the normal chat output.
addhook("say","mySayFunc")
function mySayFunc(playerId,chatMessage)
   msg(player(playerId,"name").." just said "..chatMessage)
   return 1
end


Attention: The "hook" and the "func" parameters must be strings! Do not forget the " or ' around them!
Please only use the plain hook and function names. Do not add any function parameters or anything else in the addhook-call.

RIGHT:
addhook("leave","myleavehook")


WRONG:
addhook("leave(id,reason)","myleavehook(id,reason)")

WRONG:
addhook(leave,myleavehook)


Attention: "func" can be either a simple function in the Lua main table (aka a global function) e.g. "helloWorld" or a function nested in a table e.g. "foo.bar" or even multiple tables e.g. "this.is.nested". Table accessors like [] are NOT supported here. So instead of "foo['bar']" you have to write "foo.bar".