Maaan, lua is weird
Code: Select all
function gadget:UnitDamaged(
unitID, unitDefID, unitTeam,
damage, paralyzer, weaponID,
attackerID, attackerDefID, attackerTeam
)
for t in pairs(myTeam) do
if (unitTeam == myTeam[t]) then
local goto_x, goto_y, goto_z = Spring.GetUnitPosition (unitID)
machTargetArea(myTeam[t], goto_x, goto_z)
end
end
end
Lets break it down for you.
Code: Select all
function gadget:UnitDamaged(
unitID, unitDefID, unitTeam,
damage, paralyzer, weaponID,
attackerID, attackerDefID, attackerTeam
)
This is the function deceleration. It is telling Lua that this function exists and that it takes a list of arguments. You can think of it kind of like opening a fast food restaurant window. The name of your restaurant is 'gadget:UnitDamaged' and you have a menu board which defines the things you need before you will take an order. We call it an argument list. (Sometimes its called a parameter list).
Code: Select all
unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam
is your argument list. Each argument
must be specified before you will start processing the order.
In this example Lua would walk up to your fast food window and specify all of the items on the menu, arguments to your function, tell you to start working and wait until you are finished.
Where Lua got these arguments really doesn't matter. You don't care where they came from, or how Lua obtained them. You only care what they are. So... what exactly are they?
unitID - The ID number of the unit that got attacked. Kind of like dogtags or a social security number.
unitDefID - This is an ID number that specifies the definition of the unit. More like a resume for the unit.
unitTeam - Which team the unit belongs to.
damage - How much damage was done to the unit.
paralyzer - (I don't really know)
weaponID - The ID number for the weapon that attacked the unit with unitID. Like the serial number on a gun.
attackerID - The ID number of the unit that did the attacking.
attackerDefID - The ID number of the definition for the unit.
attackerTeam - The team the attacker belongs to.
Now. Like I said before, we don't care how Lua got this information, we just need to assume its true. Now that we know all of the information we need to start, we can begin processing the order.
This is a for loop. It is a basic looping structure. It will do everything between the matching 'do' and 'end' keywords some number of times. How many times? We don't know, and frankly we don't care. What we do care about is what each part of this 'for' statement
is.
t - The t in this for statement is a variable. It is going to store the data that comes out of whatever collection we are looking at.
in - A keyword that tells us which collection to look in.
pairs(myTeam) - pairs() is a function and myTeam is a global variable somewhere. The variable myTeam is a collection. A collection is a group of things. You can think about it as being a great big bag that we can throw things in. pairs() is a special function that allows us to look at the things in our bag in sets of two items, a key and a value.
When we look at the code "for t in pairs(myTeam) do" we need to understand that t is going to become the
key for every pair of items in our bag. It is going to help us understand what happens next.
The if statement is fairly basic. Lets take a look at the interesting part
Here we have that same global variable myTeam again. Remember, it is a collection ( a bag full of things). 't' is our
key we got from our for statement.
myTeam[t] is a statement that says: "Hey bag of stuff. give me the thing that matches to this key 't'" and magically out pops the thing that is associated with the key.
Why are we doing this? Well, we know that somewhere in that big fat sack of things is my team identifier. It tells me what my team is. Eventually when I yell at the sack long enough, and with enough keys 't', it is going to give me back my team identifier. I just have to find it first.
The 'if' statement itself checks everything that comes out of the bag against the unitTeam argument that Lua gave us. If they ever match I know that the unit Lua is telling me about is actually
my unit. This is because the unit's team idenfier matches
my team identifer.
The next two lines
local goto_x, goto_y, goto_z = Spring.GetUnitPosition (unitID)
machTargetArea(myTeam[t], goto_x, goto_z)
Ask the spring engine to give my the x,y and z coordinate of
my unit that just got attacked. (GetUnitPosition) And then send all of my other units to that location (machTargetArea).
The end statements simply tell the if, for and function to end.
In this case we don't return anything so after we are finished processing we simply walk back up to the order window and tell lua that we are finished. Lua then proceeds on to its next task.