The 3rd Age

Battle for the Galaxy

Battle for the Galaxy

Battle for the Galaxy seeks to change the land of Middle-earth into the vast Star Wars Galaxy.

Button for The 3rd AgeButton for The Dwarf HoldsButton for The Elven AllianceButton for Helm's Deep Last HopeButton for GothmogtheOrcButton for BFME+Button for The Four AgesButton for HDR HeadquartersButton for Middle Earth CenterButton for Project Perfect Mod

Become an affiliate!

   

Quick Lists

Top Rated Popular New Updated Last Comments Users

Register and log in to move these advertisements down

Advanced LUA: Helper Functions

Tutorial for Battle for Middle-earth BFME

Avatar of Nertea

Nertea

Category: Code
Level: Expert
Created: Wednesday November 18, 2009 - 0:43
Updated: Wednesday November 18, 2009 - 1:56
Views: 12761
Summary: Learn about how to use lua helper functions in BFME1

Rating

Staff says

4.7

Members say

5.0

Average

4.7/5.0

7 votes

Page 1 2 3 4
This page deals with storing unchanging data in a function... things like subobject lists.

Unfortunately, you can't declare global unchanging variables in scripts.lua. This means that you can't just store all the subobject lists (for the two helper functions on the previous pages) for a unit at the top of the file where they can be easily accessed. This normally isn't a problem, because such lists aren't used more than once in most cases - we just stick them in the top of the function as before. However, what if you have a very complex bit of lua where you repeatedly want to access the same list in different spaces? Say 2 OnCreate scripts that are only slightly different? Or, you anticipate changing the model's subobjects in the future. If you could change one master list instead of all of them, it would be much easier.

This next function can sit at the top of the scripts.lua and just contains data. It can be used any time you need the data it contains. Unlike the other two functions I've written in the article, it actually outputs a value (or in this case, several values - a list).

              
Code

-- This function retrieves data for use in a subobject script
-- --------------------------------------------------
-- # Inputs: category: a table containing the type of subobject to retrieve
-- #    
-- # Outputs: objects: a table containing the subobject names
function RetrieveOrcObjects(category)
    -- Initialize table and keys because BFME's lua is a mess
    local objects = {}
    local a = "helmets"
    local b = "shields"
    local c = "weapons1h"
    local d = "weapons2h"
    -- Assign values to each key
    objects[a] = {"HELM1","HELM2","HELM3","HELM4","HELM5","HELM6","HELM7"}
    objects[b] = {"SHIELD1","SHIELD2","SHIELD3","SHIELD4","SHIELD5","SHIELD6","SHIELD7","SHIELD8","SHIELD9"}
    objects[c] = {"1HW1","1HW2","1HW3","1HW4","1HW5","1HW6","1HW7","1HW8","1HW9","1HW10","1HW11","1HW12","1HW13"}
    objects[d] = {"2HW1","2HW2","2HW3","2HW4"}
    
    -- Return wanted values
    return objects[category]
end


I can use this in my Orc code too...

              
Code

-- Orc Fighter randomization
function OnMordorOrcACreated(self)

    local helmetList = RetrieveOrcObjects("helmets")
    local shieldList = RetrieveOrcObjects("shields")
    local weaponList = RetrieveOrcObjects("weapons")

    HideAllSubObjects(self,shieldList)
    HideAllSubObjects(self,helmetList)
    HideAllSubObjects(self,weaponList)

    RandomizeSubobjects(self,helmetList)
    RandomizeSubobjects(self,shieldList)
    RandomizeSubobjects(self,weaponList)
end

Note how the parameter of "helmets" corresponds to the "helmets" section in the function. That lets it fetch the right subobject list. You would need to write a function of this type for every unit, something like:
              
Code

function RetrieveOrcObjects(category)
end

              
Code

function RetrieveUrukSwordsmanObjects(category)
end

              
Code

function RetrieveRohanPeasantObjects(category)
end

              
Code

function RetrieveDwarfWarriorObjects(category)
end

With the appropriate lists of subobjects and categories written in.

This concludes the tutorial. I hope someone finds it useful - though it may be difficult to grasp the concepts of functions without some basic programming experience, and difficult to write them without more specific lua programming basics.

Comments

Display order: Newest first

Nertea (Team Chamber Member) - Tuesday December 22, 2009 - 10:25

lua tables aren't really pure arrays because of their interesting key/record system. I'm not really a programmer, but my experience with actual programming languages has shown that lua tables don't behave the same way, so I guess it's ok for them to call them something else. Kinda like python's lists really.

they're defined like that because EA broke something somewhere - I couldn't get it to work using the normal way of declaring a table key/record.

Bart (Administrator) - Friday December 18, 2009 - 5:47

Nice tutorial. I wonder why LUA calls arrays tables though.
And why do you have to do this:
a = "foo"; list[a] = "bar"
instead of just:
list["foo"] = "bar"

Nertea (Team Chamber Member) - Saturday November 21, 2009 - 13:42

Glad it's a bit helpful. I do want to know if anybody uses this, and with what degree of success, partially for interest's sake and partially to see if it's worth writing up stuff about other lua script tricks that I've developed/am developing.

Rob38 (Team Chamber Member) - Thursday November 19, 2009 - 18:29

Interesting. I also use lots of random stuff for my models so this can be very helpful.

Sulherokhh (Team Chamber Member) - Wednesday November 18, 2009 - 6:39

Yippee! I love tuts like that, easily accessible and immediately useful.
Info #3 is a vital debugging tool. :)

Go to top

 

"One site to rule them all, one site to find them,
one site to host them all, and on the network bind them."

 
4:04:06