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: all (72)

Lua Command parse

Categories

Parameters

  • "commands"
  • [Stop-At-Semicolon] (optional)

Info

Executes ordinary non-Lua CS2D commands.
Everything behind the first semicolon (;) will be ignored. This is a security measurement in case you're using user input (like chat messages) with this command. You can change this behavior by setting Stop-At-Semicolon to 0.

A list of non-Lua CS2D commands is available here!

Note: In many cases you can simply include the wrapper to use ordinary non-Lua CS2D commands like Lua functions. Take a look at sys/lua/wrapper.lua for more information! Your resulting Lua code will be much easier to read if you use the wrapper!

Attention: Many people make mistakes when using the parse command. It needs a string as parameter. This can be difficult if you want to insert Lua variables into this string or if you are working with CS2D commands that have strings as parameters. Take a look at the examples!

Attention: Be very careful when using this command with user input. Users could use quotes or semicolons to change the way the script is parsed. This can allow them to execute their own commands or to manipulate parameters. Never set Stop-At-Semicolon to 0 if you are parsing a string which contains user input!

Sample 1: Change the server name using Lua (note the usage of single quotes and double quotes to encapsule the 2 different strings)
parse('sv_name "Test Server"')

Sample 2: Using an integer variable with parse (note that two dots are used to concatenate the variable with the string. The two dots are a Lua operator)
banid=1
parse('banusgn '..banid)

Sample 3: Using a string variable with parse (note that additional double quotes are required for this case because the player name is a string parameter)
username="Player"
parse('banname "'..username..'"')

Sample 3 might be a bit confusing at the first look but we know that two dots concatenate 2 strings in Lua and we have 3 strings here:
- 'banname "'
- username (which has the value Player)
- and '"'
so the resulting string is simply 'banname "Player"' (without the single quotes)
It is NOT 'banname ""Player""' because the double quotes in "Player" are NOT part of the variable value! They are just used to tell Lua where the string starts and ends. This is a working CS2D command. You could enter it in the console (without the single quotes of course).

Sample 4: Using multiple variables with parse
player=1
xpos=5
ypos=3
parse('setpos '..player..' '..xpos..' '..ypos)
The parameter in the code of sample 4 will be connected to the string 'setpos 1 5 3' - which works (note that parameters in CS2D console commands are separated with spaces and NOT with commas like it's done in Lua!)

Take a look at the file sys/lua/wrapper.lua in your CS2D folder for more examples on how to use parse!

If you have problems using parse with complicated concatenated values you should use print to show the resulting string. This way you can see if the value is correct and works in the CS2D console! To do so just replace 'parse' with 'print'!