Editing MG/Lua

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
 
[[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]].
 
[[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]].
 
Notice: You can’t use the <-operator in within the XML-events. You can use an external script instead or invert the logic.
 
  
 
==Text==
 
==Text==
Line 17: Line 15:
 
'''text''' - a string identifying the'' text'' from a language file.
 
'''text''' - a string identifying the'' text'' from a language file.
  
=== addConsoleLangText(text)===
+
=== addConsoleLangText(const char *fmt, ...)===
Displays a message in the game console messages area. Will remain until the message timeout has elapsed. Works like <tt>displayFormattedText()</tt> besides the fact that <tt>addConsoleLangText()</tt> uses the language-file for resolving the format of the text.
+
Available in rev 2813. Displays a message in the game console messages area. Will remain until the message timeout has elapsed. Works like <tt>displayFormattedText()</tt> besides the fact that <tt>addConsoleLangText()</tt> uses the language-file for resolving the format of the text.
  
 
Parameters:<br />
 
Parameters:<br />
Line 29: Line 27:
 
'''text''' - a string identifying the ''text'' from a language file.
 
'''text''' - a string identifying the ''text'' from a language file.
  
===displayFormattedText (fmt, ...)===
+
===displayFormattedText (const char *fmt, ...)===
Works like <tt>setDisplayText()</tt> but allows formatting of text.
+
Available in rev 2767. Works like <tt>setDisplayText()</tt> but allows formatting of text.
  
 
Parameters:<br />
 
Parameters:<br />
Line 36: Line 34:
 
'''...''' - variable parameter list to pass values to fmt ''(like in printf)''.
 
'''...''' - variable parameter list to pass values to fmt ''(like in printf)''.
  
===displayFormattedLangText (fmt, ...)===
+
===displayFormattedLangText (const char *fmt, ...)===
Works like <tt>displayFormattedText()</tt> but uses the language-file for resolving the format of the text.
+
Available in rev 2767. Works like <tt>displayFormattedText()</tt> but uses the language-file for resolving the format of the text.
  
 
Parameters:<br />
 
Parameters:<br />
Line 43: Line 41:
 
'''...''' - variable parameter list to pass values to fmt ''(like in printf)''.
 
'''...''' - variable parameter list to pass values to fmt ''(like in printf)''.
  
=== clearDisplayText() ===
+
=== clearDisplayText () ===
 
Clears the message from the top of the screen previously set with <tt>setDisplayText()</tt>.
 
Clears the message from the top of the screen previously set with <tt>setDisplayText()</tt>.
  
Line 50: Line 48:
 
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.
 
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:<br />'''unit''' - name of a unit in the loaded factions.<br />'''faction''' - the index of the faction this unit will belong to.<br />'''pos''' - an array of two elements, specifying the co-ordinates {x,y}.
+
Parameters:<br />
=== createUnitNoSpacing(unitID, faction, pos) ===
+
'''unit''' - name of a unit in the loaded factions.<br />
Same as above, but position is exact.
+
'''faction''' - the index of the faction this unit will belong to.<br />
 +
'''pos''' - an array of two elements, specifying the co-ordinates {x,y}.
  
 
+
=== destroyUnit(unitID) ===
===destroyUnit(unitID)===
 
 
Destroys a unit.
 
Destroys a unit.
  
Line 69: Line 67:
 
'''ignoreRequirements''' - a 0 or 1 value indicating whether to respect morph requirements (0) or not (1)
 
'''ignoreRequirements''' - a 0 or 1 value indicating whether to respect morph requirements (0) or not (1)
  
===setUnitPosition(unitId, pos)===
+
=== moveToUnit ( unitID, destUnitID ) ===
"Teleports" unit to the given position (places the unit there instantly, no move animation).
 
 
 
Parameters:<br />
 
'''unitID''' - the ID of the unit to move.<br />
 
'''pos''' - the x and y coordinates of the location you wish to move the unit to (note the exact position may not be the supplied position, as the unit may be slightly staggered, particularly if a unit already exists in the given location).
 
 
 
=== moveToUnit(unitID, destUnitID) ===
 
 
Move unitID to the location of destUnitID.
 
Move unitID to the location of destUnitID.
  
Line 82: Line 73:
 
'''unitID''' - the ID of the unit to move (starting location).<br />
 
'''unitID''' - the ID of the unit to move (starting location).<br />
 
'''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.
 
'''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.
 
===giveStopCommand(unitID)===
 
Gives a stop command to a specific unit.
 
 
Parameters:<br />
 
'''unitID''' - the ID of a unit that has a stop command.
 
  
 
===giveAttackCommand(unitID, unitToAttackID)===
 
===giveAttackCommand(unitID, unitToAttackID)===
Line 134: Line 119:
 
'''upgrade '''- the name of the upgrade to perform.
 
'''upgrade '''- the name of the upgrade to perform.
  
===giveKills(unitID, amount)===
+
===giveKills(unit, amount)===
Gives a specified unit a certain number of kills (which can be used to level them up).
+
Available in rev 2823. Gives a specified unit a certain number of kills (which can be used to level them up).
  
 
Parameters:<br />
 
Parameters:<br />
'''unitID''' - The ID of the unit to give the kills to.<br />
+
'''unit''' - The ID of the unit to give the kills to.<br />
 
'''amount''' - The number of kills given.
 
'''amount''' - The number of kills given.
 
===getUnitProperty(unitId,typeId)===
 
Returns the value of the type of the unit.
 
The type can be a number between 1 and 5.
 
1=hp
 
2=ep
 
3=level
 
4=field
 
5=skill
 
 
getUnitProperty is dedicated for hp,ep and field. For skill and level see getUnitPropertyName(unitId,typeId).
 
 
===getUnitPropertyName(unitId,typeId)===
 
Returns the name of the type of the unit.
 
The type can be a number between 1 and 5.
 
1=hp
 
2=ep
 
3=level
 
4=field
 
5=skill
 
 
getUnitPropertyName is dedicated for skill and level. For hp,ep and field see getUnitProperty(unitId,typeId).
 
 
===selectUnit(unitID)===
 
Selects the specified unit.
 
 
Parameters:<br />
 
'''unitID''' - The ID of the unit to select.
 
 
===unselectUnit(unitID)===
 
Unselects the specified unit (provided it's already selected).
 
 
Parameters:<br />
 
'''unitID''' - The ID of the unit to unselect.
 
 
===addUnitToGroupSelection(unitID, groupIndex)===
 
Adds the unit to a group, assigned a user defined group index.
 
 
Parameters:<br />
 
'''unitID''' - The ID of the unit to select.<br />
 
'''groupIndex''' - A number to identify the group.
 
 
===removeUnitFromGroupSelection(unitID, groupIndex)===
 
Removes the unit from a group of the defined group index.
 
 
Parameters:<br />
 
'''unitID''' - The ID of the unit to select.<br />
 
'''groupIndex''' - The previously assigned index of the group.
 
 
===recallGroupSelection(groupIndex)===
 
Reselects a previously defined group by its group index.
 
 
Parameters:<br />
 
'''groupIndex''' - The previously assigned index of the group.
 
 
===highlightUnit(unitID, radius, thickness, colour)===
 
Places a coloured circle around a unit.
 
 
Parameters:<br />
 
'''unitID''' - unit ID of the target unit.<br />
 
'''radius''' - the radius, in Glest tiles, of the circle.<br />
 
'''thickness''' - thickness of the circle.<br />
 
'''colour''' - the colour of the circle.
 
 
===unhighlightUnit(unitID)===
 
Removes a previously placed highlight from a unit.
 
 
Parameters:<br />
 
'''unitID''' - unit ID of the target unit.
 
  
 
==Game==
 
==Game==
Line 216: Line 132:
 
Parameters:<br />
 
Parameters:<br />
 
'''pos''' - an array of two elements, specifying the co-ordinates {x,y}.
 
'''pos''' - an array of two elements, specifying the co-ordinates {x,y}.
 
=== shakeCamera(intensity, duration) ===
 
Causes the camera to shake.
 
 
Parameters:<br />
 
'''intensity''' - Intensity of the shaking effect<br />
 
'''duration''' - Duration of the shaking effect in update frames (40 equals one second)
 
 
=== shakeCameraOnUnit(intensity, duration, unitId) ===
 
Same as above, but the shake intensity is affected by the distance between camera and unit.
 
Useful if the camera should only shake, when the unit is in sight.
 
 
Parameters:<br />
 
'''intensity''' - Intensity of the shaking effect<br />
 
'''duration''' - Duration of the shaking effect in update frames (40 equals one second)<br />
 
'''unitId''' - Unit ID of the unit, which should influence the shake intensity
 
  
 
===togglePauseGame(pauseStatus)===
 
===togglePauseGame(pauseStatus)===
Line 247: Line 147:
 
=== endGame() ===
 
=== 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 <tt>setPlayerAsWinner()</tt> when victory conditions have been met.
 
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 <tt>setPlayerAsWinner()</tt> when victory conditions have been met.
 +
  
 
=== loadScenario(scenario, keepFactions) ===
 
=== 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.
+
Loads another scenario from inside an existing scenario. The new scenario must have the same techtree and factions, but map, tileset can change.  
  
 
Parameters:<br />
 
Parameters:<br />
 
'''scenario''' - the name of the scenario to be loaded.<br />
 
'''scenario''' - the name of the scenario to be loaded.<br />
'''keepFactions''' - True to call the next scenario with existing factions or false to call the next scenario with only newly defined factions in the new scenario
+
'''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
 
 
===setAttackWarningsEnabled(enabled)===
 
Enables or disables the attack warning system for the player.
 
 
 
Parameters:<br />
 
'''enabled''' - A boolean true or false value, with true enabling the attack warning system and false disabling it.
 
 
 
===getAttackWarningsEnabled()===
 
Returns the current status of the attack warnings system (true if enabled, false if disabled).
 
 
 
===getIsDayTime()===
 
Returns if it is day ingame. ( 1 is day and 0 is night )
 
 
 
===getIsNightTime()===
 
Returns if it is night ingame. ( 1 is night and 0 is day )
 
 
 
===getTimeOfDay()===
 
Returns the ingame time of the day.
 
 
 
=== storeSaveGameDate(key, value) ===
 
Stores ''value'' at ''key'' in the savegame.<br />
 
'''''Only available in the onSave-tag!'''''
 
 
 
Parameters:<br />
 
'''key''' - a key string<br />
 
'''value''' - a string that is stored
 
 
 
=== loadSaveGameDate(key) ===
 
Returns the ''value'' stored at ''key'' in the savegame.<br />
 
'''''Only available in the onLoad-tag!'''''
 
 
 
Parameters:<br />
 
'''key''' - a key string
 
[[Category:MG]]
 
[[Category:Scenarios]]
 
[[Category:Lua]]
 
  
 
==AI==
 
==AI==
Line 388: Line 253:
  
 
===registerCellTriggerEventForFactionToUnit(sourceFactionID, destUnitID)===
 
===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 '&lt;cellTriggerEvent>'.
+
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:<br />
 
Parameters:<br />
Line 400: Line 265:
 
'''sourceFactionID''' - the ID of a faction who has at least one unit that moves next to destUnit.<br />
 
'''sourceFactionID''' - the ID of a faction who has at least one unit that moves next to destUnit.<br />
 
'''pos''' - the x and y cell co-ordinates that will trigger the event when a unit from sourceFactionID enters the cell.
 
'''pos''' - the x and y cell co-ordinates that will trigger the event when a unit from sourceFactionID enters the cell.
 
===registerCellAreaTriggerEvent(pos)===
 
Registers a bounding rectangle as a trigger event. This will be triggered by ANY unit moving in or out of the area. To know which unit, use <tt>triggeredEventAreaEntryUnitId()</tt>.
 
 
Parameters:<br />
 
'''pos''' - the x and y coordinate of the top left point then the bottom right point (eg, {10,10,5,5} would create a box from points (10,10) to (5,5)).
 
 
 
===registerDayNightEvent()===
 
Enables the Day/Night Event which will be triggered when night or day starts.
 
 
Example:
 
<syntaxhighlight lang="lua">
 
<startup>
 
registerDayNightEvent()
 
</startup>
 
<dayNightTriggerEvent> 
 
if getIsDayTime() == 1 then
 
setDisplayText('It is day time, a safer time to roam!')
 
    else
 
setDisplayText('**It is night time, WATCH OUT!**')
 
    end
 
</dayNightTriggerEvent>
 
</syntaxhighlight>
 
 
===unregisterDayNightEvent()===
 
Disables the Day/Night Event.
 
 
===triggeredEventAreaEntryUnitId()===
 
Returns the unit ID of the unit who entered a registered cell area trigger event.
 
 
===triggeredEventAreaExitUnitId()===
 
Returns the unit ID of the unit who exited a registered cell area trigger event.
 
 
===lastUnitTriggerEventUnit()===
 
Returns the ID of the Unit which triggered the UnitTriggerEvent.
 
 
===lastUnitTriggerEventType()===
 
Returns the ID of property which changed.
 
1=hp
 
2=ep
 
3=level
 
4=field
 
5=skill
 
  
 
===getCellTriggerEventCount(eventID)===
 
===getCellTriggerEventCount(eventID)===
Line 450: Line 271:
 
Parameters:<br />
 
Parameters:<br />
 
'''eventID''' - the ID of a previously registered event
 
'''eventID''' - the ID of a previously registered event
 
===triggeredCellEventUnitId()===
 
Can be called in a '&lt;cellTriggerEvent>' to return the unit ID of the unit whom triggered the event.
 
  
 
===unregisterCellTriggerEvent(eventID)===
 
===unregisterCellTriggerEvent(eventID)===
Line 459: Line 277:
 
Parameters:<br />
 
Parameters:<br />
 
'''eventID''' - the ID of a previously registered event.
 
'''eventID''' - the ID of a previously registered event.
 
===registerUnitTriggerEvent(unitId)===
 
Registers a unit trigger event for a unit which will be triggered if one of his properties changes.
 
 
Example:
 
<syntaxhighlight lang="lua">
 
<startup>
 
    createUnit('battle_machine', 1, startLocation(1))
 
 
    player2_battlemachine= lastCreatedUnit()
 
    registerUnitTriggerEvent(player2_battlemachine)
 
</startup>
 
<unitTriggerEvent>
 
    if player2_battlemachine == lastUnitTriggerEventUnit() then
 
                       
 
        if lastUnitTriggerEventType() == 1 then -- hp changed
 
            print('Unit hp is now: ' .. getUnitProperty(lastUnitTriggerEventUnit(),lastUnitTriggerEventType()))
 
        elseif lastUnitTriggerEventType() == 2 then -- ep changed
 
            print('Unit ep is now: ' .. getUnitProperty(lastUnitTriggerEventUnit(),lastUnitTriggerEventType()))
 
        elseif lastUnitTriggerEventType() == 3 then -- level changed
 
            print('Unit level is now: ' .. getUnitPropertyName(lastUnitTriggerEventUnit(),lastUnitTriggerEventType()))
 
        elseif lastUnitTriggerEventType() == 4 then -- field changed
 
            print('Unit field is now: ' .. getUnitProperty(lastUnitTriggerEventUnit(),lastUnitTriggerEventType()))
 
        elseif lastUnitTriggerEventType() == 5 then -- skill changed
 
            print('Unit skill is now: ' .. getUnitPropertyName(lastUnitTriggerEventUnit(),lastUnitTriggerEventType()))
 
        end
 
    end
 
</unitTriggerEvent>
 
</syntaxhighlight>
 
 
===unregisterUnitTriggerEvent(unitId)===
 
Unregisters the unit trigger event for a unit.
 
  
 
===startTimerEvent()===
 
===startTimerEvent()===
Line 554: Line 340:
  
 
Parameters:<br />
 
Parameters:<br />
'''unitID''' - The ID of the unit whose faction you wish you check.
+
'''unitID''' - The ID of the unit who's faction you wish you check.
  
 
=== resourceAmount(resource, faction) ===
 
=== resourceAmount(resource, faction) ===
Line 562: Line 348:
 
'''resource''' - The name of the resource you wish you check.<br />
 
'''resource''' - The name of the resource you wish you check.<br />
 
'''faction''' - The index of the faction to check.
 
'''faction''' - The index of the faction to check.
 
===unitName(unitId)===
 
Returns the name of the unit whose unit ID was passed into the function.
 
 
Parameters:<br />
 
'''unitID''' - the ID of the unit whose name you wish to know.
 
  
 
=== lastCreatedUnitName() ===
 
=== lastCreatedUnitName() ===
Line 575: Line 355:
 
Returns the unit ID of the last created unit.
 
Returns the unit ID of the last created unit.
  
=== lastDeadUnitName() ===
+
=== lastDeadUnitName () ===
 
Returns the name of the last unit to die.
 
Returns the name of the last unit to die.
  
=== lastDeadUnit()===
+
=== lastDeadUnit ()===
 
Returns the ID of the last unit to die.
 
Returns the ID of the last unit to die.
  
Line 641: Line 421:
  
 
Parameters:<br />
 
Parameters:<br />
'''faction''' - The index of the faction you wish to check.<br />
+
'''faction''' - The index of the faction you wish to check.
 
'''name''' - The name of the specific unit you wish to check.
 
'''name''' - The name of the specific unit you wish to check.
  
Line 672: Line 452:
  
 
===getUnitsForFaction(factionIndex, commandTypeName, field)===
 
===getUnitsForFaction(factionIndex, commandTypeName, field)===
Returns an array (table) of units in the specified faction which lists all units which have the specified command and can use it on the specified field (such as being able to attack air units).<br />
+
Returns an array (table) of units in the specified faction which:
<tt>0</tt> for land and <tt>1</tt> for air field.
+
*If only the commandTypeName is specified, the table will be a list of all units which have that command (such as <tt>attack</tt>).
[[Category:MG]]
+
*If the commandTypeName is empty and only the field is specified (<tt>0</tt> = land, <tt>1</tt> = air), the table will list all units which are in that specific field.
[[Category:Scenarios]]
+
*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).
[[Category:Lua]]
 
  
 
===getUnitCurrentField(unitId)===
 
===getUnitCurrentField(unitId)===
Line 687: Line 466:
 
===networkShowMessageForTeam(text, header, teamIndex)===
 
===networkShowMessageForTeam(text, header, teamIndex)===
 
Works the same was as <tt>networkShowMessageForFaction()</tt> above, but displays to all players in the specified team.
 
Works the same was as <tt>networkShowMessageForFaction()</tt> above, but displays to all players in the specified team.
 
===showMarker(flashCount, factionIndex, note, textureFile, pos)===
 
Places a marker on the map at a given location.
 
 
Parameters:<br />
 
'''flashCount''' -<br />
 
'''factionIndex''' - index of the faction who "placed" the marker.<br />
 
'''note''' - note on the marker.<br />
 
'''textureFile''' - path of texture file to use for marker.<br />
 
'''pos''' - the x and y cell coordinates of the marker.
 
 
===networkSetCameraPositionForFaction(factionIndex, pos)===
 
Set the camera position for a specific faction.
 
 
Parameters:<br />
 
'''factionIndex''' - index of the faction whose camera you wish to move.<br />
 
'''pos''' - x and y coordinates for the camera's position.
 
 
===networkSetCameraPositionForTeam(teamNumber, pos)===
 
Set the camera position for all factions on a team.
 
 
Parameters:<br />
 
'''teamNumber''' - team number whose camera you wish to move.<br />
 
'''pos''' - x and y coordinates for the camera's position.
 
  
 
== XML events ==
 
== XML events ==
 
These are XML tags used to execute Lua code on its specified event. Variables can be used across different events.
 
These are XML tags used to execute Lua code on its specified event. Variables can be used across different events.
  
<syntaxhighlight lang="xml"><global>
+
<syntaxhighlight lang="xml"><startup>
 
<!--
 
<!--
Code here will be executed whenever the scenario is started.
+
Code here will be executed once on startup.
 
-->
 
-->
</global>
 
<onSave>
 
<!--
 
Code here will be executed when the game is about to save.
 
-->
 
</onSave>
 
<onLoad>
 
<!--
 
Code here will be executed when the savegame got loaded.
 
-->
 
</onLoad>
 
 
<startup>
 
<startup>
<!--
 
Code here will be executed once on startup.
 
-->
 
</startup>
 
 
<unitCreatedOfType type="unitname">
 
<unitCreatedOfType type="unitname">
 
<!--
 
<!--
Line 749: Line 489:
 
<!--
 
<!--
 
Code here will be executed every time a unit gets attacked.  
 
Code here will be executed every time a unit gets attacked.  
 +
Works with SVN rev 2823.
 
-->
 
-->
 
</unitAttacked>
 
</unitAttacked>
Line 754: Line 495:
 
<!--
 
<!--
 
Code here will be executed every time a unit is attacking.  
 
Code here will be executed every time a unit is attacking.  
 +
Works with SVN rev 2823.
 
-->
 
-->
 
</unitAttacking>
 
</unitAttacking>
Line 790: Line 532:
 
[[Category:Scenarios]]
 
[[Category:Scenarios]]
 
[[Category:Lua]]
 
[[Category:Lua]]
[[Category:XMLs]]
 

Please note that all contributions to MegaGlest are considered to be released under the Creative Commons Attribution Share Alike (see MegaGlest:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)