Written especially for some of you

Difficulty: Intermediate
Tried to make it as simple as possible, but you should be able to handle the expression editor easily to understand - knowing basic things like fastloops, Val(), if-else, what a condition is, etc. I'll assume you're used to figuring out other extensions too.


Why use Lua?
First, there's always formulas. Take this simple formula that calculates distance, for example:
D=Sqrt((X1-X2)^2 + (Y1-Y2)^2)

Let's assume you're storing them in counters, in MMF2, that's:
Set Alterable Value D to Sqr((value(X1)-value(X2)) pow 2+(value(Y1)-value(Y2)) pow 2)

In Lua, it's:
D=math.sqrt((X1-X2)^2 + (Y1-Y2)^2)

Less than half the length

Also, there's...
- Externalizing code (allowing mods, and it's much faster to type than to click)
- Writing long lines of text outside the expression editor.
- Much easier to debug and read code.


Coding in Lua
Sure, you could code it from within MMF2, that's what I did at first. But it's hard to debug and harder to sort out your code. Use a proper tool to write it.

The most obvious would be the one from the official Lua site: http://www.lua.org

The less obvious is downloading Notepad++ which also helps with other things, like writing this article. Site:
http://notepad-plus.sourceforge.net/uk/site.htm


Where do I start?
Lua is a scripting language in itself, so you should learn it properly. The best tutorial out there is this: http://lua-users.org/wiki/TutorialDirectory

Go from top to bottom, it teaches you some cool basic things. I'm not going to cover most things that are detailed on the site, but there are plenty of things you should learn from there, like math, arrays, strings. It makes things much easier, trust me. I'd suggest you read at least all of the Introductory Topics before even trying to use it in MMF2. I'll assume you have.


Telling Lua to do things
For most of this tutorial, I'll be touching mainly on how to convert MMF2 into Lua. The most basic thing in Lua is a function. Put everything in functions. Let's say, for example, you want Lua to calculate damage. Damage will be [attack - defense] just to make things easy.

Here's what I plan to do:
1. Create the function in Lua.
2. Get MMF2 to tell Lua everything it needs to calculate damage.
3. Actually calculate damage.
4. Lua tells MMF2 the damage results.

Setting it up
First, we set it up. Create the Lua object. Double click it, then your code goes in Script. But like I said, it's easier to write the code in Notepad++, then copy it into there.

Let's make the function (it's from function to end):

function damage(tonumber(attack), tonumber(defense))

dam = attack - defense
if dam<0 then
dam = 0
end
return dam
end


Here's what it means:
function damage(stuff) sets up a function called damage which we call later.
(attack, defense) means we make new variables, the first is called attack, second is defense. Tonumber() does exactly what Val() does in MMF2.
dam = (stuff) does exactly what it says. It sets dam to the attack minus defense. It's kinda like creating a counter just by saying dam.
if..condition..then..action: That's a simple if, then thing. So, if damage is less than 0, it sets your damage to 0. Hah, another reason why I love Lua - it lets you put a condition and action in the same line.
return dam sends the dam value to MMF2
end closes the function.

Oh, and another note, you can mix strings and numbers in Lua. It will automatically convert a string to a number if it thinks it has to. But you really shouldn't, not with arrays and tables, especially. Because array[1] means that you're taking the first value in the array. array[1] means that you're taking an array called 1. So, converting them to strings and numbers saves you some trouble later on.


Sending data from MMF2
Ok, I'm going to assume that you put them in counters. For this example.. let's say you're taking value(attack) and value(defense) from different counters with those names.

Go to Lua object, under action. Lua Functions > Call Function
This is to call up that function we made earlier.

Your function name is damage. Because that's what we called it earlier.
Your parameter list is Str$(value(attack)) + | + Str$(value(defense)). What this does is that it takes the numbers in the counters and sends them into the values of the (attack, defense) we wanted earlier. The | means that you're separating the different values.
Return count is 1 because we only had one variable to return, dam.

Done!

Calculating damage
As soon as you call it, it calculates the damage before you could blink. Done!

Getting damage results
Lua has already calculated it, and it's sent it back as a return.
In the expression editor, go to Retrieve Data from an object > (the Lua object) > Lua Functions > Get Value Return. Put that number wherever you need the damage.

Done!

And we did it all in only one event!


Calling things from MMF2 into Lua and making MMF do things
Let's say you want to get data from MMF2 and put it into Lua. The opposite thing. It's a bit messier, because that's how MMF is. For this example, we want to 'ask' MMF for um.. armor. Oh, and let's play a sound every time we calculate damage.

function damage(tonumber(attack), tonumber(defense))

armor = DoCall(GetArmor)
dam = attack - defense*armor
if dam<0 then
dam = 0
end
volume = dam*2
DoCall(Sound, volume)
return dam
end


DoCall(GetArmor)
The first one is simple.
Make a new event with condition (Lua object) > On Function
Set function name to GetArmor.
Then set action to (Lua object) > MMF Functions > Set Return
Put whatever number you want as armor there! (remember to convert it to string)

Now whenever Lua calls GetArmor, you send the value of the armor to it!

DoCall(Sound)
Here, we're going to play a sound where the volume is affected by the damage. It can be whatever, a scream, armor clink.

Make a new On Function condition, with Sound.
Play the sound you want.
In the expression editor, go to Retrieve Data from an object > (the Lua object) > Lua Functions > Get Value Return. Put it in whatever place that determines volume

And there we are, volume following the amount of damage!


Cooler things
Lua also allows you to do things with arrays, tables, and data storage. That's for another time, though, once you're used to basic things like this. Arrays are just like MMF arrays. Tables are nicer... they're easier to read with Lua and don't use memory when you don't use them. You can also call them by names - they're as easy to use as INI. Oh.. and it lets you store data!