Lua Tutorials
From WolfWiki
This page is here to help you understand ETPro Lua functions.
Before you start on this tutorial I assume you know basic Lua scripting. If not then I suggest you take a look at the online edition of the book Programming in Lua or the lua-users wiki. You can also take a look at code made by someone else, a list of ETPro mods that can be found here, or our Lua Sample Code.
Contents
Getting started
Loading a Script
- Scripts are loaded by their file name out of the /etpro sub directory of your Enemy Territory installation, so don't forget to save them in the right folder.
- if you want to load HelloWorld.lua you would have to set the server cvar lua_modules to "HelloWorld.lua".
- If you want to load more mods you can add them to the same variables separated by a space like "HelloWorld.lua lol.lua pingpong.lua"
- NOTE: if you modify this cvar all currently loaded lua modules will be unloaded.
- After editing your script you save it and then reload it in ETPro to make your chances take effect, this can simply be done by reset_match server command.
Development Environment
- While scripting you will see your code gets hard to read, its much easier to read with syntax highlighting. This can be done by scripting inside an lua development environment.
- These can be found under the Development Environments heading at this page.
Tutorials
- NOTE: Lines written in Bold are the actual lines we are going to write in our scripts.
Hello World
- Here we will show a simple hello world example for the server console.
- Just follow the steps and read the comments.
- 1. First we need a new file so, create a new file and name it HelloWorld.lua.
- Now we need an ETPro lua function that will be called automatically by ETPro when we want it. Therefore we will use et_InitGame(levelTime,randomSeed,restart). That function is called once every round at the beginning. It's a good function to do some initial settings. We do not have to worry about those parameters (levelTime,randomSeed,restart) in this example.
- 2. Inside our file at line 1 we will write function et_InitGame(levelTime,randomSeed,restart) -- called at beginning of a gameround
- Now we got a part of our script that is called at the beginning of a round so now we can add our code to it. We are going to print "hello world" in the server console. To do this we will use et.G_Print("message"). That function will simply print the "message" in the server console.
- 3. At line 2 we will write et.G_Print("Hello World!!!\n") -- printout our text to the console
- Lastly we need to end out function at line 1 with a simple end
- 4. At line 3 place end --close et_InitGame()
- Now if you did it right you should have something like this:
function et_InitGame(levelTime,randomSeed,restart) -- called at beginning of a gameround et.G_Print("Hello World!!!\n") -- printout our text to the console end --close et_InitGame()
- Now save and load your script and you will see Hello World!!! in the server console.
Hello Spam
- Here we will show a simple Spammy hello world example for the server console.
- Just follow the steps and read the comments.
- 1. First we need a new file so, create a new file and name it HelloSpam.lua.
- Now we need an ETPro lua function that will be called automatically by ETPro many times to make it spammy. Therefore we will use et_RunFrame(levelTime). That function is called every server frame. It is a good function for stuff you need to constantly check but you should not put to much code in here. We do not have to worry about those parameter (levelTime) in this example.
- 2. Inside our file at line 1 we will write function et_RunFrame(levelTime) --called every serverframe
- Now we got a part of our script that is called at the beginning of a round so, now we can add our code to it. We are going to print "hello world" in the server console. To do this we will use et.G_Print("message"); that function will simply print the "message" in the server console
- 3. Now at line 2 we will write et.G_Print("Hello Spam!!!\n") -- printout our text to the console
- Lastly we need to end our function at line 1 with a simple at line 3 end
- 4. At line 3 place end --close et_RunFrame()
- Now if you did it right you should have something like this:
function et_RunFrame(levelTime) --called every serverframe et.G_Print("Hello Spam!!!\n") -- printout our text to the console end --close et_RunFrame()
- Now save and load your script and you will see Hello Spam!!! in the server console.
Hello Client
- Here we will show a simple hello world example for the client console.
- Just follow the steps and read the comments.
- 1. First we need a new file so, create a new file and name it HelloClient.lua.
- 2. Inside our file at line 1 we will write function et_InitGame(levelTime,randomSeed,restart) --called at beginning of a gameround
- Now we need a function to print a message to the client his console. We will use et.trap_SendServerCommand(-1,"print \"message\n\"") to achieve this. That function will simply print the "message" in the client's console.
- The first parameter is the client slot to send to. When this is -1 ETPro will send the message to all clients. The 2nd parameter looks confusing and like the original author's spelling, it is. First you have the main command witch is "print ..". Then, at the place of the dots, comes the data to send with the print. In our case this is "message\n"".
- The \n will send a newline command to the console so the next message written to the console is on the next line. The \" are just quotes but since they are already inside a string you need to prefix them with a \ (commonly known as an escape character).
- 3. So just write this at line 2 et.trap_SendServerCommand(-1,"print \"Hello Client\n\"") -- printout our text to the console
- Last we need to end our function at line 1 with a simple end
- 4. At line 3 place end --close et_InitGame()
- Now if you did it right you should have something like this:
function et_InitGame(levelTime,randomSeed,restart) -- called at beginning of a gameround et.trap_SendServerCommand(-1,"print \"Hello Client\n\"") -- printout our text to the console end -- close et_InitGame()
- Now save and load your script and you will see Hello World!!! in the client's console at the begin of a round.
Server Command lol
- Here we will show how to make a new server command that, will simpely print back hihi.
- Just follow the steps and read the comments.
- 1. First we need a new file so, create a new file and name it ServerCmdLol.lua.
- 2. Inside our file at line 1 we will write function et_ConsoleCommand()--called when someone types a command in conosle or uses sa todo a server command
- Now the first thing we need to know is wut command was entered.
- Commands there are entered will be split up by spaces so, cmd1 hello all will be split up in "cmd1", "hello" and "all".
- The only ecception is that cmd2 "hello all" will be split up in "cmd2" and "hello all" becase, "hello all" is entered as 1 string
- To get a part of the command you should use et.trap_Argv(partnumber)
- partnumber 0 is the actual command and, partnumber 1 is the first parameter of the command
- so in our example above we would get:
- et.trap_Argv(0) = "cmd1"
- et.trap_Argv(1) = "hello"
- et.trap_Argv(2) = "all"
- 3. so at line 2 we will write local cmd = et.trap_Argv(0) --load out the first parameter of the test typed in console (the command itself)
- The commands are send on like they way they where entered so if you enter cMd3 you will get cMd3.
- But that makes them harder to detect there for we will convert the string to lower case using a normal lua function.
- 4. to make it lower case we will write cmd = string.lower(cmd) --convert it to lower case so its easyer to match in our if statements at line 3
- Ok, now we can use a simpel if statement to check if the command is lol.
- 5. So now at line 4 write if cmd == "lol" then --run our code if the command was "dumpuserid"
- If thats true then we will print "hihi".
- 6. So at line 5 write et.G_Print("hihi\n") --print out "hihi"
- 7. So at line 6 we will write return 1 --return 1 to make etpro ignore the command so u wont get an 'invalid command' warning
- Thats all we need todo for this command so we can start closing our function
- 8. At line 7 we end our if statement of line 4 so we will write end --close the if at line 4
- Now we get to the code that gets executed when the command was not lol.
- We got nothing todo here so we will return 0 to make ETPro habdle it.
- 9. make etpro handle all other commands so write return 0 --if the command wasnt 'lol' then we will get here and return 0 to make etpro handle it at line 8
- Now we are almost done just end our function.
- 10. So at line 9 we will write end --close function et_ConsoleCommand() at line 1
- Now if you did it right you should have something like this:
function et_ConsoleCommand() --called when someone types a cokmmand in conosle or uses sa todo a server command local cmd = et.trap_Argv(0) --load out the first parameter of the test typed in console (the command itself) cmd = string.lower(cmd) --convert it to lower case so its easyer to match in our if statements if cmd == "lol" then --run our code if the command was 'dumpuserid' et.G_Print("hihi\n") --print out 'hihi' return 1 --return 1 to make etpro ignore the command so u wont get an 'invalid command' warning end --close the if at line 7 return 0 --if the command wasnt 'lol' then we will get here and return 0 to make etpro handle it end -- close et_ConsoleCommand()
- Now save and load your script and you will be able to use lol as a server command.