Jump to content

[TUT] Further introduction into scripting


.:HyPeX:.

Recommended Posts

WELCOME

Hey guys, HyPeX here, with a tutorial to help new scripters.

First of all, this tutorial assumes you have read and understood the introduction to scripting: https://wiki.multitheftauto.com/wiki/Sc ... troduction

So, with any furthermore lets go in:

Note: My first point in this tutorial is to give tips and data to users and scripters wich are not implicit on the wiki, but they're in lua tutorials, and some other info that there isnt anywhere.

[*]Our start point will be functions. Normally, you understand functions very well, but not how they work nor many aspects of them.

First of all a defined function is also a variable. Why it is a variable? becouse we're defining something, in this case a function.

Take a look at this code:

function MyFunctionName()  
--DoStuff 
end 

What we can see here? we defined "MyFunctionName" as a function, wich will do everything specified here. So continuing this we can actually call functions!

Most of the cases you would just end up making events and using them, wich is actually a good way to go (You can still use exports, read here), but if we're inside the same resource, we do not need to use events!

Look now at this code:

function MyFunctionName() 
--DoStuff 
end 
  
MyFunctionName() 

Whats interesting here? we can see that at the end of the script, we added MyFunctionName(). What this does? it will try to call "MyFunctionName", wich is defined by us as a function before, with no arguments ( Thats becouse brackets are empty, and the function hasnt any use to them either).

Note that all MTA's functions are predefined, and we can overwrite them, creating a function "setElementData" will overwrite the predefined "setElementData" one.

Now, Arguments:

We can call a function with arguments! What does this mean? we can give extra "local" variables to our function by passing it to them, see this:

function MyFunctionName(Argument1, Argument2) 
--DoStuff 
end 
  
MyFunctionName("Cally", "Dolly") 

What we can see here? we defined that variables that the function will recognize (That doesnt mean that they will always be aviable, nor the same) if they're passed to it. (If we pass argument to a function, but the function cant handle them (ie not specified) then its useless, and nothing will happen.)

So, inside the our function, Argument1 will be "Cally", and Argument2 will be "Dolly". Also, arguments must always be separated by a comma.

How functions work inside the same resource:

Most surely, you havent yet noticed this, but you can call a function wich is in another script file, in the same resource.

If we have 2 files:

File 1:

function MyFunctionName() 
--DoStuff 
end 

File2:

MyFunctionName() 

So, File2 will call our function from it, and actually creating another function will overwrite the already defined "outside" function with the local file defined one.

Now a big part in functions: Returns

Basically, calling a function like we did before has some adventages wich events do not. (Exports have them too)

This adventage is mainly one: returns.

Why returns? here comes our explanation about them

What return actually does? a return (when called) will halt the current function run with no other consecuences if nothing is specified:

function MyFunctionName() 
return 
--DoStuff 
end 
  
MyFunctionName() 

So the function will be stopped before DoStuff comes to be ever readed. This is usefull to stop a function if we want to and how we want to.

But there's even more. We can actually specify what the return will do:

function MyFunctionName() 
return "Halted" 
--DoStuff 
end 
  
MyFunctionName() 

So, when the function is cut off, it will return string "Halted", how can we actually get that? by specifiying an end:

function MyFunctionName() 
return "Halted" 
--DoStuff 
end 
  
myReturn = MyFunctionName() 

So in the end, myReturn will actually be "Halted". We can also return functions:

function MyFunctionName() 
return MySecondFunction() 
--DoStuff 
end 
  
function MySecondFunction() 
return "MySecondHalt" 
end 
  
myReturn = MyFunctionName() 

So myReturn will be "MySecondHalt".

Last and not least important: how functions are called.

You should know that any plain thingy is run upon a resource start, but functions are not plain, they are definitions (aka variables) so they wont run upon resourse start, you need to call them, as readed abdove.

I will keep editing and adding more things to this topic. Next to come is tables.

PD: if someone wants to add this to the wiki, do so, i get quite some headaches editing the wiki.

EDIT: After some inactivity, i wont continue this tutorial, sorry. Please head up to Made's topic here to continue your learning!

Edited by Guest
Link to comment
  • 2 weeks later...
Awesome, I've learnt and understood finally, now I can't wait for the tables tutorial! Thank you!

Thanks!, i'm doing the tables one, but it is getting somehow ridiculously hard to explain and long, i'm covering every small aspect of them from my knowledge, trying to not leave anything. (I do each part on notepad and then take it to forum where i organize it and colour it)

Link to comment
  • 4 months later...
  • 3 weeks later...
  • Recently Browsing   0 members

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