MG/Lua

Lua is the scripting language used in MegaGlest's scenarios. It allows scenarios to expand and do things not normally possible in regular games of Glest. This page lists all the Lua commands available in MegaGlest. For information on how to create scenarios and working with Lua in general, see Lua.

showMessage(text, header)
Displays a message box on the screen, that will have to be dismissed by the player.

Parameters:

text - a string identifying the text from a language file.

header - a string identifying the title bar text from a language file.

addConsoleText(text)
Displays a message in the game console messages area. Will remain until the user-specified message timeout has elapsed.

Parameters:

text - a string identifying the text from a language file.

addConsoleLangText(const char *fmt, ...)
Available in rev 2813. Displays a message in the game console messages area. Will remain until the message timeout has elapsed. Works like displayFormattedText besides the fact that addConsoleLangText uses the language-file for resolving the format of the text.

Parameters:

text - a string identifying the text from a language file.

setDisplayText(text)
Displays a message at the top of the screen. Will remain until dismissed with clearDisplayText or setDisplayText is called again.

Parameters:

text - a string identifying the text from a language file.

displayFormattedText (const char *fmt, ...)
Available in rev 2767. Works like setDisplayText but allows formatting of text.

Parameters:

fmt - a string identifying the format of text (like in printf).

... - variable parameter list to pass values to fmt (like in printf).

displayFormattedLangText (const char *fmt, ...)
Available in rev 2767. Works like displayFormattedText but uses the language-file for resolving the format of the text.

Parameters:

fmt - a string identifying the format of text (like in printf).

... - variable parameter list to pass values to fmt (like in printf).

clearDisplayText
Clears the message from the top of the screen previously set with setDisplayText.

createUnit(unitID, faction, pos)
Creates a unit. If pos is occupied, a nearby cell will be chosen. The game attempts to keep a 2 cell 'border' between the new unit and other units or objects (eg, trees). If a position can not be found, the game crashes.

Parameters:

unit - name of a unit in the loaded factions.

faction - the index of the faction this unit will belong to.

pos - an array of two elements, specifying the co-ordinates {x,y}.

destroyUnit(unitID)
Destroys a unit.

Parameters:

unitID - the ID of the unit to destroy.

morphToUnit(unitID, morphName, ignoreRequirements)
Morphs a unit into another unit of type morphName. If ignoreRequirements is set to 1 then the morph is executed even if the unit does not satisfy the morph requirements.

Parameters:

unitID - the ID of a unit.

morphName - the name of the morph to execute.

ignoreRequirements - a 0 or 1 value indicating whether to respect morph requirements (0) or not (1)

moveToUnit ( unitID, destUnitID )
Move unitID to the location of destUnitID.

Parameters:

unitID - the ID of the unit to move (starting location).

destUnitID - the ID of the unit to move to (ending location of unitID). This would essentially make unitID 'follow' destUnitID until it arrives at the location of destUnitID.

giveAttackCommand(unitID, unitToAttackID)
Instruct a unit to carry out a command of type 'attack' on a specific unit.

Parameters:

unitID - the ID of a unit that has an attack command. unitToAttackID - the ID of a unit that should be attacked.

giveAttackStoppedCommand(unitID, valueName, ignoreRequirements)
Executes the attack stopped command for a unit specified by 'valueName'. If ignoreRequirements is set to 1 then the command is executed even if the unit does not satisfy the command requirements.

Parameters:

unitID - the ID of a unit.

valueName - the name of the attack stopped command to execute.

ignoreRequirements - a 0 or 1 value indicating whether to respect command requirements (0) or not (1).

giveResource(resource, faction, amount)
Give an amount of a specified resource to a player, negative values are valid and have the obvious effect.

Parameters:

resource - a corresponding resource in the techtree.

faction - the index of the faction to give the resource.

amount - the amount of resource the faction will receive.

givePositionCommand(unitID, command, pos)
Instruct a unit to carry out a command of type 'attack' or 'move'. The unit being given the command must have such a command available.

Parameters:

unitID - the ID of a unit that has an attack and/or move command.

command - the type of command to carry out ('attack' or 'move')

pos - an array of two elements, specifying the co-ordinates {x,y}.

giveProductionCommand(unitID, produced)
Instruct a unit to carry out a command of type 'produce'. The unit being given the command must have such a command available.

Parameters:

unitID - the ID of a unit that is to produce the new unit.

produced - the name of the unit to produce.

giveUpgradeCommand(unitID, upgrade)
Instruct a unit to carry out a command of type 'upgrade'. The unit being given the command must have such a command available.

Parameters:

unitID - the ID of the unit to perform the upgrade.

upgrade - the name of the upgrade to perform.

giveKills(unit, amount)
Available in rev 2823. Gives a specified unit a certain number of kills (which can be used to level them up).

Parameters:

unit - The ID of the unit to give the kills to.

amount - The number of kills given.

setCameraPosition(pos)
Move the camera to the coordinates of pos.

Parameters:

pos - an array of two elements, specifying the co-ordinates {x,y}.

togglePauseGame(pauseStatus)
Toggle the pause state of the existing game.

Parameters:

pauseStatus - 0 indicates normal game play, 1 indicates a paused game

setPlayerAsWinner(factionID)
Sets the player with index faction ID as the winner. Would typically be followed by endGame.

Parameters:

faction - the index of the faction.

endGame
Ends the scenario, bringing up the "you win" screen and asking the player if they want to leave the game. Usually would be called after setPlayerAsWinner</tt> when victory conditions have been met.

loadScenario(scenario, keepFactions)
Loads another scenario from inside an existing scenario. The new scenario must have the same techtree and factions, but map, tileset can change.

Parameters:

scenario - the name of the scenario to be loaded.

keepFactions - 1 to call the next scenario with exisitng factions or 0 to call the next scenario with only newly defined factions in the new scenario

enableAi(faction)
Enables a factions AI. All of the faction's units will move with this command.

Parameters:

faction - the index of the faction.

disableAi(faction)
Disables a factions AI. None of the faction's units will move with this command.

Parameters:

faction - the index of the faction.

getAiEnabled(faction)
Checks the enabled status of a factions AI. Returns 0 if the AI is disabled, and 1 if enabled.

Parameters:

faction - the index of the faction.

enableConsume(faction)
Enables a factions requirement to consume resources like food. All of the faction's units will consume food type resources with this command.

Parameters:

faction - the index of the faction.

disableConsume(faction)
Disbles a factions requirement to consume resources like food. All of the faction's units will not consume food type resources with this command.

Parameters:

faction - the index of the faction.

getConsumeEnabled(faction)
Checks the consume status of a factions AI. Returns 0 if disabled, and 1 if enabled.

Parameters:

faction - the index of the faction.

playStaticSound(soundFile)
Play a static sound file specified by 'soundFile'.

Parameters:

soundFile - the filename of the sound file to play.

playStreamingSound(soundFile)
Play a streaming sound file (background music) specified by 'soundFile'.

Parameters:

soundFile - the filename of the sound file to play.

stopStreamingSound(soundFile)
Stop playing a streaming sound file (background music) specified by 'soundFile'.

Parameters:

soundFile - the filename of the sound file to play.

stopAllSound
Stop playing all sounds

registerCellTriggerEventForUnitToUnit(sourceUnitID, destUnitID)
Register a cell 'trigger' event. Any time sourceUnit comes next to destUnit the eventID returned by this function will be triggered inside an event called '<cellTriggerEvent>'.

Parameters:

sourceUnitID - the ID of a unit moves next to destUnit.

destUnitID - the ID of a unit that source moves next to.

Example:

registerCellTriggerEventForUnitToLocation(sourceUnitID, pos)
Register a cell 'trigger' event. Any time sourceUnit comes into the cell co-ordinates specified by pos, the event called '<cellTriggerEvent>' is triggered using the eventID returned by this function.

Parameters:

sourceUnitID - the ID of a unit moves next to destUnit.

pos - the x and y cell coordinates that will trigger the event when sourceUnitID enters the cell

registerCellTriggerEventForFactionToUnit(sourceFactionID, destUnitID)
Register a cell 'trigger' event. Any time a unit from sourceFaction comes next to destUnit the eventID returned by this function will be triggered inside an event called '<cellTriggerEvent>'.

Parameters:

sourceFactionID - the ID of a faction who has at least one unit that moves next to destUnit.

destUnitID - the ID of a unit that source moves next to.

registerCellTriggerEventForFactionToLocation(sourceFactionID, pos)
Register a cell 'trigger' event. Any time a unit from sourceFaction comes into the cell co-ordinates specified by pos, the event called '<cellTriggerEvent>' is triggered using the eventID returned by this function.

Parameters:

sourceFactionID - the ID of a faction who has at least one unit that moves next to destUnit.

pos - the x and y cell co-ordinates that will trigger the event when a unit from sourceFactionID enters the cell.

getCellTriggerEventCount(eventID)
Returns the number of times the event specified by eventID has been triggered.

Parameters:

eventID - the ID of a previously registered event

unregisterCellTriggerEvent(eventID)
Unregister the event specified by eventID.

Parameters:

eventID - the ID of a previously registered event.

startTimerEvent
Registers a timer and returns its unqiue ID, which can later be compared within a '<timerTriggerEvent>' event.

Example:

resetTimerEvent(eventID)
Reset the timer specified by eventID. This function also returns the number of seconds elapsed since the timer started.

Parameters:

eventID - the ID of a previously registered timer.

stopTimerEvent(eventID)
Stop the timer specified by eventID. This function also returns the number of seconds elapsed since the timer started.

Parameters:

eventID - the ID of a previously registered timer.

timerEventSecondsElapsed(eventID)
This function returns the number of seconds elapsed since the timer started.

Parameters:

eventID - the ID of a previously registered timer.

triggeredCellEventId
This function returns the eventID that is currently triggering a '<cellTriggerEvent>'.

triggeredTimerEventId
This function returns the eventID that is currently triggering a '<timerTriggerEvent>'.

startEfficientTimerEvent(triggerSecondsElapsed)
This function creates a timer that will only trigger the timer event after triggerSecondsElapsed</tt> seconds, then remove the timer.

unitPosition(unitID)
Returns the current location of a given unit.

Parameters:

unitID - the unitID of the unit whose position you wish to know.

Example:

unitFaction(unitID)
Returns the faction index of the unit with unitID.

Parameters:

unitID - The ID of the unit who's faction you wish you check.

resourceAmount(resource, faction)
Returns the amount of a given resource is possessed by a faction.

Parameters:

resource - The name of the resource you wish you check.

faction - The index of the faction to check.

lastCreatedUnitName
Returns the unit name of the last created unit.

lastCreatedUnit
Returns the unit ID of the last created unit.

lastDeadUnitName
Returns the name of the last unit to die.

lastDeadUnit
Returns the ID of the last unit to die.

lastDeadUnitCauseOfDeath
Returns the following depending on how the last unit died:
 * None = 0
 * Attacked = 1
 * Attack boost = 2
 * Starved resource = 3
 * Negative regeneration = 4

lastDeadUnitKillerName
Returns the name of the killer of the last dead unit. Returns empty if the cause of death was not from an attacker (attacked).

lastDeadUnitKiller
Returns the ID of the killer of the last dead unit. Returns -1 if the cause of death was not from an attacker (attacked).

lastAttackedUnit
Returns the ID of the last unit which was attacked.

lastAttackedUnitName
Returns the name of the last unit which was attacked.

lastAttackingUnit
Returns the ID of the last unit to attack.

lastAttackingUnitName
Returns the name of the last unit to attack.

getSystemMacroValue(key)
Returns the value of the SystemMacro in C++ (e.g. $SCENARIO_PATH, $APPLICATIONPATH, $GAMEVERSION). At the moment it is primary used to get the path to the loaded scenario.

Parameters:

key -  The name of the SystemMacro you want to get. Usually starts with "$". (e.g. $SCENARIO_PATH, $APPLICATIONPATH, $GAMEVERSION)

Example (will load the file filename.lua in the folder of your scenario):

scenarioDir
Does the same as getSystemMacroValue("$SCENARIO_PATH"). Exist for GAE compatibility.

getPlayerName(faction)
Returns the name of the player of a given faction.

playerName(faction)
Does the same as getPlayerName(faction). Exist for GAE compatibility.

Parameters:

faction - The index of the faction you wish to check.

unitCount(faction)
Returns the number of units a given faction has.

Parameters:

faction - The index of the faction you wish to check.

unitCountOfType(faction, name)
Returns the number of units of a specific name a given faction has.

Parameters:

faction - The index of the faction you wish to check. name - The name of the specific unit you wish to check.

gameWon
Returns true if the human player won the game, and false if they did not.

isGameOver
Returns true if the game is over (either by the default game over criteria being met or by calling endGame</tt>). Returns false otherwise.

startLocation(faction)
Returns the start location for a given faction as a set of coordinates.

Parameters:

faction - the index of the faction.

Example: This would create a unit 5 cells east of startLocation 0.

setRandomGenInit(seed)
Generates a random number based on the supplied seed.

getRandomGen(minVal, maxVal)
Used with setRandomGenInit</tt> to get a random number inbetween minVal</tt> and maxVal</tt>.

getWorldFrameCount
Retrieves the world frame count, which can be used in seeding random numbers.

getUnitsForFaction(factionIndex, commandTypeName, field)
Returns an array (table) of units in the specified faction which:
 * If only the commandTypeName is specified, the table will be a list of all units which have that command (such as attack</tt>).
 * If the commandTypeName is empty and only the field is specified (0</tt> = land, 1</tt> = air), the table will list all units which are in that specific field.
 * If both commandTypeName and the field are specified, the table will list all units which have the specified command can can use it on the specified field (such as being able to attack air units).

getUnitCurrentField(unitId)
For the specified unit, returns 0</tt> if they are land and 1</tt> if they are in the air field.

networkShowMessageForFaction(text, header, factionIndex)
Works the same was as showMessage</tt>, but only displays the message to the specific faction. Good for network scenarios where it may be beneficial to display different messages to each player.

networkShowMessageForTeam(text, header, teamIndex)
Works the same was as networkShowMessageForFaction</tt> above, but displays to all players in the specified team.

XML events
These are XML tags used to execute Lua code on its specified event. Variables can be used across different events.