Jump to content

[TUT] Basic Lua


AJXB

Recommended Posts

This tutorial needs an open minded person, a clever one, someone uses common sense.

What is Lua?

Lua is a powerful, fast, lightweight, embeddable scripting language.

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

Where does Lua come from?

Lua is designed, implemented, and maintained by a team at PUC-Rio, the Pontifical Catholic University of Rio de Janeiro in Brazil. Lua was born and raised in Tecgraf, the Computer Graphics Technology Group of PUC-Rio, and is now housed at Lablua. Both Tecgraf and Lablua are laboratories of the Department of Computer Science of PUC-Rio.

Notes about Coding in MTA:SA:

1. Before you start, you will need a lua editor, you can choose one of the following:

Unofficial MTA script editor

Notepad++

2. Letters state is very important, you can't write outputchatbox, it should be

outputChatBox 

.

3. Colors mixed with other colors will form a different color, example: 255 red and 0 green and 0 blue will return plain RED, if we add 100 green, the result will be a color between red and green combined.

4. Each function will need an end, so you must add

end 

at the end of the function's body, example: after

outputChatBox  

and any code you want to use, put end to close the function.

Lua regarding coding in Multi Theft Auto:San Andreas framework:

Coding using Lua inside MTA:SA frame is different of the original Lua, as MTA:SA has its own custom functions, no other game have them, nor original Lua.

Lua coding in MTA:SA must contain 2 essential parts, the Meta.xml file, the server side, or the client side, or BOTH, Each side is represented as a file, we'll call it a script.

Something about each part:

Meta.xml: This is the identification part of each resource, in other words, it will tell the MTA:SA Server that this resource contains these scripts.

Format:

<info author="AuthorName" type="Type[script,misc,gamemode,map]" version="Resource's version" name="Resource's name" description="Resource's Description" /> 
  
<script src="PathToFirstScript.lua" type="Server or Client (Side)"/> 
<script src="PathToSecondScript.lua" type="Server or Client (Side)"/> 
  
</meta> 

How to make a Meta.xml file?

Just copy the code above, and save it as meta.xml file.

Example: A resource that contains 2 sides, Client and Server sides, the Client side script is called "Client.lua", the Server side script is called "Server.lua", it will have this meta.xml code:

<meta> 
  
<info author="Aboud" type="script" version="1.0" name="Example resource" description="Example resource"  /> 
  
<script src="Server.lua" type="Server"/> 
<script src="Client.lua" type="Client"/> 
  
</meta> 

There, we've finished the meta.xml basic part, this file is not hard, it does have many sides though.

Server.lua: This is one of the parts that we can't make a format for, its dynamic, it will be formatted as you desire, as the purpose of the resource, All i can say about this, is that is connected to the server, in every way, unlike the other side that we will see later on.

Format: N/A

How to make a Server.lua file?

Copy the code below, and save as Server.lua.

Example: I'll make an example about three resources, the first one will Output a message to the chatbox, the second one will kill the player (You), the third one will warp you to a position (LS-LV-SF), anywhere.

First example: After we've made the Server.lua, an empty file with the .lua extension.

First step we will do is to make a new function, a function is a.. meh, a function :P something the script will do, when ever i trigger it.

Function syntax:

function  FunctionName (Arguments..,...,...) 

Its a simple syntax, the arguments thingy, you will learn later on.

Next step we will need to do is to use

outputChatBox 

code, this code will output a message to the main chat box.

outputChatBox syntax:

outputChatBox ("The Text",[thePlayer,Color:R,Color:G,Color:B,ColorCoded]) 

Let me explain,

-"The Text" is the text you want to output to the chat box, for example: "This is a test"

-thePlayer is the element you want the text to appear to, example,

getRootElement () 

(ALL elements inside the server - Players)

-Color:R: is a number between 0-255 that will define the red color the text will contain, 0 for NONE and 255 for absolute RED, Check note #3.

-Color:G: is a number between 0-255 that will define the green color the text will contain, 0 for NONE and 255 for absolute GREEN, Check note #3.

-Color:B: is a number between 0-255 that will define the blue color the text will contain, 0 for NONE and 255 for absolute BLUE, Check note #3.

-ColorCoded: if you are using color codes in the text, then put

true 

if not, then put

false 

, it can be only these two options, true or false, simple words, simple form, example of color codes, #FFFFFF For white.

Now, we know what is meta.xml, server.lua, a function, and

outputChatBox 

.

Next step is very important, you MUST add an end at the end of the function, Check note #4.

We now have the function, last thing we need is to trigger the function, how? let me show you.

Each function you add, must be triggered at some point, else, why add it?

there are many codes to trigger a function, some Server sided, others client sided, some even BOTH.

we'll use

addCommandHandler 

addCommandHandler: this is a code that will trigger a function when a command is put, example, when i execute the command /kill, it will trigger a function that will kill me, as a player.

addCommandHandler syntax:

addCommandHandler ("TheCommand",theFunction) 

Example: let's say i want to trigger a function called (TheFunction), using the command "trigger".

the code will be:

addCommandHandler ("trigger",TheFunction) 

Our final code will be:

The File is: meta.xml

<meta> 
  
<info author="Aboud" type="script" version="1.0" name="Example resource" description="Example resource"  /> 
  
<script src="Server.lua" type="Server"/> 
  
</meta> 

There won't be a Client side, as we are not using it, yet.

The File is: Server.lua

function TheFunction () 
outputChatBox ("The Text",getRootElement(),255,0,255,true) 
end 
addCommandHandler ("trigger",TheFunction) 

All done, now you can start the server, start the resource, and do /trigger.

The second resource:

We will need a new function, but this function will be a little different, we will use an argument.

Example: function theFunction (thePlayer)

Let's start, we will create a new meta.xml, and a new server.lua, i won't repeat above steps.

A new function: function theFunction (thePlayer)

now, a new code line, that will kill the element (thePlayer) is:

killPed 

killPed syntax:

killPed ( thePlayer, theKiller) 

Example:

killPed (thePlayer,thePlayer) 

this code will kill the player (You) or anyone who triggers the function, and the killer will be the same player.

Now, nothing is really complicated, same above steps, the result will be, same Meta.xml file, and

Server.lua:

function theFunction (thePlayer) 
killPed (thePlayer,thePlayer) 
end 
addCommandHandler ("trigger",theFunction) 
  

Now we save, and go in game, it should work fine.

we reach the third resource, and the last of this tutorial, written by Aboud =]

The third one will warp you to a position.

What is warp?

Changing your position into another, example, warp to LV, will set your position to some point in LV you define, the point will be defined by 3 points, numbers, coordinates, X,Y,Z

X = The position according to "X"

Y = The position according to "Y"

Z = The height above the point 0 (Sea level)

Let's start, we will use the same method as before, but with a different code, we'll use

setElementPosition 

This code will set the element's position, any element, as long as the script can define it.

setElementPosition syntax:

setElementPosition (theElement,x,y,z) 

Example:

setElementPosition (theElement,10,10,2) 

Let's use it inside our Server.lua script, i expect you to do it yourself, it's not that hard.

Result will be, Same meta.xml,

Server.lua:

function theFunction (thePlayer) 
setElementPosition (thePlayer,10,10,2) 
end 
addCommandHandler ("trigger",theFunction) 

In short, Lua is easy, if you got time + if you are willing to know, REALLY willing, not anyone can learn this.

Lua is a huge world, of words you put together to make a working script, you will need time to get it done, this is only the start.

This tutorial is for anyone who is willing to learn, for any further questions, you can send me a personal message, here.

Regards,

Aboud.

P.S: This took me 2 hours to write, it takes 20 seconds to write a thank you! :)

Edited by Guest
Link to comment
  • 7 months later...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...