Difference between revisions of "Lua Tutorials"
From WolfWiki
m (u->you;whit->whith and other de-FF-ication) |
m (whipped out the spelling & grammer stick but surely missed a few ones) |
||
Line 18: | Line 18: | ||
: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. | :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 | + | :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]. |
==Tutorials== | ==Tutorials== | ||
− | :'''NOTE''': Lines written in '''Bold''' are the actual lines we | + | :'''NOTE''': Lines written in '''Bold''' are the actual lines we are going to write in our script. |
===Hello World=== | ===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. | :1. First we need a new file so, create a new file and name it HelloWorld.lua. | ||
− | ::Now we need | + | ::Now we need an [[ETPro]] [http://www.lua.org/ 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. | + | :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 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. | + | :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. | + | :4. At line ''3'' place '''end --close et_InitGame()''' |
− | :: | + | ::Now if you did it right you should have something like [http://home.zonnet.nl/westhof99/lua/HelloWorld.lua this]: |
− | <font color="blue">function</font> et_InitGame(levelTime,randomSeed,restart) <font color="green">--called at beginning of a gameround</font> | + | <font color="blue">function</font> et_InitGame(levelTime,randomSeed,restart) <font color="green">-- called at beginning of a gameround</font> |
− | et.G_Print(<font color="darkblue">"Hello World!!!\n"</font>) <font color="green">--printout our text to the console</font> | + | et.G_Print(<font color="darkblue">"Hello World!!!\n"</font>) <font color="green">-- printout our text to the console</font> |
<font color="blue">end</font> <font color="green">--close et_InitGame()</font> | <font color="blue">end</font> <font color="green">--close et_InitGame()</font> | ||
− | :: | + | ::Now save and load your script and you will see Hello World!!! in the server console. |
<hr> | <hr> | ||
===Hello Spam=== | ===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'''. | :1. First we need a new file so, create a new file and name it '''HelloSpam.lua'''. | ||
− | ::Now we need | + | ::Now we need an [[ETPro]] [http://www.lua.org/ 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. | + | :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. | + | ::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. | + | :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. | + | :4. At line ''3'' place '''end --close et_RunFrame()''' |
− | :: | + | ::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 you will see Hello Spam!!! in the server console. |
<hr> | <hr> | ||
===Hello Client=== | ===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. | :1. First we need a new file so, create a new file and name it HelloClient.lua. | ||
− | ::Now we need a [[ETPro]] [http://www.lua.org/ lua] function that will | + | ::Now we need a [[ETPro]] [http://www.lua.org/ 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. | + | :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 | + | ::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 client slot to send to | + | ::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 | + | ::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. | + | :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. | + | :4. At line ''3'' place '''end --close et_InitGame()''' |
− | :: | + | ::Now if you did it right you should have something like [http://home.zonnet.nl/westhof99/lua/HelloClient.lua this]: |
− | <font color="blue">function</font> et_InitGame(levelTime,randomSeed,restart) <font color="green">--called at beginning of a gameround</font> | + | <font color="blue">function</font> et_InitGame(levelTime,randomSeed,restart) <font color="green">-- called at beginning of a gameround</font> |
− | et.trap_SendServerCommand(-<font color="blue">1</font>,<font color="darkblue">"print \"Hello Client\n\""</font>) <font color="green">--printout our text to the console</font> | + | et.trap_SendServerCommand(-<font color="blue">1</font>,<font color="darkblue">"print \"Hello Client\n\""</font>) <font color="green">-- printout our text to the console</font> |
− | <font color="blue">end</font> <font color="green">--close et_InitGame()</font> | + | <font color="blue">end</font> <font color="green">-- close et_InitGame()</font> |
− | :: | + | ::Now save and load your script and you will see Hello World!!! in the client's console at the begin of a round. |
<hr> | <hr> | ||
[[Category:ETPro]] | [[Category:ETPro]] | ||
[[Category:ETPro:Mods]] | [[Category:ETPro:Mods]] | ||
[[Category:ETPro:LUA]] | [[Category:ETPro:LUA]] |
Revision as of 09:00, 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.
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 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.
- 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.