The 3rd Age

The Four Ages mod

The Four Ages mod

Fight for the freedom of Beleriand with the eldar and the elves or enslave them with Angband.

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


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


Staff says


Members say




7 votes

Page 1 2 3 4
Table of Contents:

Page 1: Introduction
Page 2: A Hide Subobject Function
Page 3: A Randomize Subobject Function
Page 4: Storing things for easy access


First I'll note: this stuff will likely apply to BFME2 as well, however it's untested in that environment (lua did change some, so it might not work). This tutorial is for the BFME user who already understands the basic use of lua, and wants to learn a bit more about ways of streamlining workflow. Actually writing these helper functions does require some proper scripting experience, so beware! In some ways this is more of a resource than a tutorial - anyone can use these script functions if they like.
They deal with ways to automate tedious repetitive code writing or copy-pasting in this domain - in particular the primary use of lua code in BFME - randomizing weapons/accessories on a model.

First, a little about the functions defined in scripts.lua. If you don't know where/what this file is, you should probably take a look at some other tutorials on lua in BFME first.

  • Each function is self-contained (cannot access data from other functions)
  • Functions in this list can use (call) other functions
  • If there is a syntax error in any of these functions, ALL the functions will stop working

These are important in 3 ways: the first because it's good to know, the third because it's important to know when you've made a syntax error, and the second because it's the focus of this tutorial. I'm going to talk about how you can use other functions in the file in a 'primary' function - say, a hypothetical thing like               
function OnMordorOrcACreated(self)
which randomizes the weapons, shields and helmets of an Orc Warrior.

On to the article!


Display order: Newest first

Nertea (Team Chamber Member) - Tuesday December 22, 2009 - 9: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 - 4: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 - 12: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 - 17: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 - 5: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."