Difference between revisions of "Lua Tutorials"

From WolfWiki
Jump to: navigation, search
m (spelling)
m (u->you;whit->whith and other de-FF-ication)
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 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].
 
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]].
  
Line 11: Line 11:
 
:if you want to load HelloWorld.lua you would have to set the server cvar '''lua_modules''' to ''"HelloWorld.lua"''.<br>
 
:if you want to load HelloWorld.lua you would have to set the server cvar '''lua_modules''' to ''"HelloWorld.lua"''.<br>
 
: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"''
 
: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 modify this cvar all currently loaded [http://www.lua.org/ lua] modules will be unloaded.
+
:'''NOTE''': if you modify this cvar all currently loaded [http://www.lua.org/ 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.
 
: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===
 
===Development Environment===
:While scripting you will see your code gets hard to read, its much easier to read whit syntax highlighting. this can be done be scripting inside an [http://www.lua.org/ lua] 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 [http://www.lua.org/ lua] development environment.
  
 
:At [http://lua-users.org/wiki/LuaAddons this page] under ''[http://lua-users.org/wiki/LuaAddons development environments]'' you will find a list of [[http://lua-users.org/wiki/LuaAddons development environments].
 
:At [http://lua-users.org/wiki/LuaAddons this page] under ''[http://lua-users.org/wiki/LuaAddons development environments]'' you will find a list of [[http://lua-users.org/wiki/LuaAddons development environments].
Line 32: Line 32:
 
:2. inside our file at line ''1'' we will write '''function et_InitGame(levelTime,randomSeed,restart) --called at beginning of a gameround'''
 
: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
+
::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'''
 
:3. now at line ''2'' we will write '''    et.G_Print("Hello World!!!\n") --printout our text to the console'''
Line 54: Line 54:
 
:1. First we need a new file so, create a new file and name it '''HelloSpam.lua'''.
 
:1. First we need a new file so, create a new file and name it '''HelloSpam.lua'''.
  
::Now we need a [[ETPro]] [http://www.lua.org/ 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 constantly check but u should not put to much code in here. We don't have to worry about those parameter (levelTime) in this example.
+
::Now we need a [[ETPro]] [http://www.lua.org/ 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. It's a good function for stuff you need to constantly check but you should not put to much code in here. We don't 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'''
 
:2. inside our file at line ''1'' we will write '''function et_RunFrame(levelTime) --called every serverframe'''
Line 66: Line 66:
 
:4. at line ''3'' place '''end --close et_RunFrame()'''
 
:4. at line ''3'' place '''end --close et_RunFrame()'''
  
::now if u did it right u should have something like [http://home.zonnet.nl/westhof99/lua/HelloSpam.lua this]:
+
::now if you did it right you should have something like [http://home.zonnet.nl/westhof99/lua/HelloSpam.lua this]:
 
  <font color="blue">function</font> et_RunFrame(levelTime) <font color="green">--called every serverframe</font>
 
  <font color="blue">function</font> et_RunFrame(levelTime) <font color="green">--called every serverframe</font>
 
     et.G_Print(<font color="darkblue">"Hello Spam!!!\n"</font>) <font color="green">--printout our text to the console</font>
 
     et.G_Print(<font color="darkblue">"Hello Spam!!!\n"</font>) <font color="green">--printout our text to the console</font>
 
  <font color="blue">end</font> <font color="green">--close et_RunFrame()</font>
 
  <font color="blue">end</font> <font color="green">--close et_RunFrame()</font>
::now save and load your script and u will see Hello Spam!!! in the server console.
+
::now save and load your script and you will see Hello Spam!!! in the server console.
 
<hr>
 
<hr>
 
===Hello Client===
 
===Hello Client===
Line 84: Line 84:
 
::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.
 
::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 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 \
+
::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 you need to prefix them with a \
  
 
:3. so just write this at line ''2'' '''    et.trap_SendServerCommand(-1,"print \"Hello Client\n\"") --printout our text to the console'''
 
:3. so just write this at line ''2'' '''    et.trap_SendServerCommand(-1,"print \"Hello Client\n\"") --printout our text to the console'''

Revision as of 02:49, 27 April 2006


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 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 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 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 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. It's a good function for stuff you need to constantly check but you should not put to much code in here. We don't 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 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 Spam!!!\n") --printout our text to the console
and as last 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 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 you need to prefix them with 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.