Difference between revisions of "Lua Tutorials"

From WolfWiki
Jump to: navigation, search
m (whipped out the spelling & grammer stick but surely missed a few ones)
m (whipped out the spelling & grammer stick but surely missed a few ones)
Line 3: Line 3:
 
This page isn't finished but its supposed to make you understand [[ETPro]] [http://www.lua.org/ lua] functions.
 
This page isn't finished but its supposed to make you understand [[ETPro]] [http://www.lua.org/ lua] functions.
  
Before you start on this tutorial its assumed you do know basic [http://www.lua.org/ lua] scripting ,if not then I suggest you take a look at the online edition of the book [http://www.lua.org/pil/ Programming in Lua] or the [http://lua-users.org/wiki/ lua-users wiki].
+
Before you start on this tutorial it's assumed you do know basic [http://www.lua.org/ lua] scripting. If not then I suggest you take a look at the online edition of the book [http://www.lua.org/pil/ Programming in Lua] or the [http://lua-users.org/wiki/ lua-users wiki].
 
You can also take a look at someone else his code, a list of [[ETPro]] mods can be found [[:Category:ETPro:Mods|here]].
 
You can also take a look at someone else his code, a list of [[ETPro]] mods can be found [[:Category:ETPro:Mods|here]].
  

Revision as of 09:01, 27 April 2006


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

Before you start on this tutorial it's 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 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 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 are going to write in our script.

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
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 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
And lastly we need to end our function at line 1 whit 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.
Now we need a ETPro lua function that will be called automatically by ETPro when we want it. To do this 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. 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.