Lua Tutorials

From WolfWiki
Revision as of 20:25, 26 April 2006 by WeblionX (Talk | contribs) (-_-)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


This page isn't finished but its supposed to make you understand ETPro lua functions.

Before you start on this tutorial its assumed you do 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 someone else his code, a list of ETPro mods can be found here.

Getting started

Loading a Script

Scripts are loaded by their file name out of the /etpro subdirectory 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 u moddify 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 whit syntax highlighting. this can de done be scripting inside an lua development environment.
At this page under development environments you will find a list of [development environments.

Tutorials

NOTE: Lines written in Bold are the actual lines we gonna write to our script.

Hello World

here we will show an 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 a ETPro lua function that will automagicly called by ETPro when we want it. Therefor we will use et_InitGame(levelTime,randomSeed,restart) that function is called ones every round at the beginning. its a good function to do some initial settings. We don't 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 therefor 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 World!!!\n") --printout our text to the console
and as last we need to end out function at line 1 whit 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 an simpel Spammy hello world example for the server console
just follow the steps abnd read the comments.
1. First we need a new file so, create a new file and name it HelloSpam.lua.
Now we need a ETPro lua function that will automagicly called by ETPro many times to make it spammy. Therefor we will use et_RunFrame(levelTime) that function is called every server frame. its a good function for stuff u need to constandly check but u should not put to much code in here. We dont 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 beggining of a round so, now we can add our code to it. we are gonig to print "hello world" in the server console therefor we will use et.G_Print("message") that function will simpely 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
and as last we need to end our function at line 1 whit a simpel at line 3 end
4. at line 3 place end --close et_RunFrame()
now if u did it right u 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 u 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.
Now we need a ETPro lua function that will automagicly called by ETPro when we want it. Therefor we will use et_InitGame(levelTime,randomSeed,restart) as shown in the Hello World 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 need a function to print a message to the client his console therefor we will use et.trap_SendServerCommand(-1,"print \"message\n\"") that function will simply print the "message" in the client console console.
The first parameter is client slot to send to if is -1 it will send to all clients. the 2nd parameters looks confusing and it is. First u got the main command witch is "print .." then, at the place of the dots comes the data to send whit the print in our case its "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 " but since they are already inside a string u need to prefix them whit a \
3. so just write this at line 2 et.trap_SendServerCommand(-1,"print \"Hello Client\n\"") --printout our text to the console
and as last 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.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 console at the begin of a round.