-
Notifications
You must be signed in to change notification settings - Fork 2
09. Lua Scripting API
This page details all the functions that are available in Lua scripts.
The unit/infantry/building names and rule names/values used in Lua can all be found here.
Note: Parameter values are not case sensitive
The NCO mod will call event handlers when a certain events happen in game. These allow configuring the game based on the scenario being played, the player side (GDI/NOD) etc. See here for more info.
Register an event handler that will be called when a user starts a new scenario
onScenarioStart(function(scenarioName)
log(string.format("Scenario: %s"), scenarioName)
-- do awesome stuff...
end)Parameters: callback - a event handler function which accepts one parameter:
-
scenarioNamestring in the formatSC<SIDE><SCENARIO_NUMBER><MAP_CHOICE>, for exampleSCB04EAis NOD mission 4 in the east
Returns: nothing
Register an event handler that will be called when a user loads a save game, this is useful because rules set on scenario load are not preserved in a save game (you will need to set them again)
onSaveLoad(function(playerHouse, scenarioNumber)
log(string.format("%s Scenario: %d"), playerHouse, scenarioNumber)
-- do awesome stuff...
end)Parameters: callback - a event handler function which accepts two parameters:
-
playerHousesave game player side:GOODGUY,BADGUYetc. -
scenarioNumbersave game scenario number
Returns: nothing
Register an event handler that will be called as the game plays, every few hundred milliseconds or so. This is useful monitor the state of the game, change rules as the scenario progresses and print messages to the user ingame.
Note: The interval in milliseconds that this event is fired at is configurable using the GameTickIntervalInMs rule, see the RULES.INI Guide
onGameTick(function()
log("Game tick event fired")
-- do awesome stuff...
end)Parameters: nothing
Returns: nothing
These functions give you information about the types of infantry, units, aircraft and buildings.
Get a list of all weapon types in the game
getWeaponTypes()Parameters: none
Returns: A table containing all the Weapon names (FLAMETHROWER, PISTOL etc.)
Get a list of all bullet types in the game
getBulletTypes()Parameters: none
Returns: A table containing all the Bullet names (BULLET, S.S.M etc.)
Get a list of all warhead types in the game
getWarheadTypes()Parameters: none
Returns: A table containing all the Warhead names (HE, FIRE etc.)
Get a list of all Infantry types in the game
getInfantryTypes()Parameters: none
Returns: A table containing all the Infantry names (E1, RMBO etc.)
Get a list of all Unit types in the game
getUnitTypes()Parameters: none
Returns: A table containing all the Unit names (MVC, STNK etc.)
Get a list of all Aircraft types in the game
getAircraftTypes()Parameters: none
Returns: A table containing all the Aircraft names (ORCA etc.)
Get a list of all Building types in the game
getBuildingTypes()Parameters: none
Returns: A table containing all the Building names (FACT, HPAD etc.)
These functions give you the names of available rules for specific game object types.
Get a list of all rules that can be set for the [Game] section of the INI file
getGameRuleNames()Parameters: none
Returns: A table containing all the Game rule names (AircraftRepairFactor, MaxHarvesterCapacity etc.)
Get a list of all rules that can be set for Weapon types
getWeaponRuleNames()Parameters: none
Returns: A table containing all the Weapon rule names (Damage, Range etc.)
Get a list of all rules that can be set for Infantry types
getInfantryRuleNames()Parameters: none
Returns: A table containing all the Infantry rule names (Speed, IsCivilian etc.)
Get a list of all rules that can be set for Unit types
getUnitRuleNames()Parameters: none
Returns: A table containing all the Unit rule names (CanHarvest, CanCloak etc.)
Get a list of all rules that can be set for Aircraft types
getAircraftRuleNames()Parameters: none
Returns: A table containing all the Aircraft rule names (CantHover, RateOfTurn etc.)
Get a list of all rules that can be set for Building types
getBuildingRuleNames()Parameters: none
Returns: A table containing all the Building rule names (Factory, PowerOutput etc.)
These functions allow you to get current rule values and set them at runtime.
Get the value for a rule found in the [Game] section of the Rules
getGameRule("HideApcFromNod")Parameters:
ruleName
Returns: The value of the rule as a string/boolean/number - this will match the data type found in RULES.INI. NOTE: if the game rule is not present in either RULES.INI or RULES-DEFAULT.INI this may return nil as the defaults are stored in those files.
Set the value for a rule found in the [Game] section of the Rules
setGameRule("OnlyGdiCanUseIonCannon", false)Parameters:
ruleNameruleValue
Returns: nothing
Get the value of a rule for a given Weapon type
getInfantryRule("DRAGON", "RateOfFire")Parameters:
weaponTyperuleName
Returns: The value of the rule as a string/boolean/number - this will match the data type found in RULES.INI
Set the value of a rule for a given Weapon type
setWeaponRule("CHEMSPRAY", "Damage", 160)Parameters:
weaponTyperuleNameruleValue
Returns: nothing
Get the value of a rule for a given Infantry type
getInfantryRule("E6", "CanCapture")Parameters:
infantryTyperuleName
Returns: The value of the rule as a string/boolean/number - this will match the data type found in RULES.INI
Set the value of a rule for a given Infantry type
setInfantryRule("RMBO", "PrimaryWeapon", "OBELISK_LASER")Parameters:
infantryTyperuleNameruleValue
Returns: nothing
Get the value of a rule for a given Unit type
getInfantryRule("APC", "PrimaryWeapon")Parameters:
unitTyperuleName
Returns: The value of the rule as a string/boolean/number - this will match the data type found in RULES.INI
Set the value of a rule for a given Unit type
setUnitRule("FTNK", "PrimaryWeapon", "chemspray")Parameters:
unitTyperuleNameruleValue
Returns: nothing
Get the value of a rule for a given Aircraft type
getInfantryRule("HELI", "PrimaryWeapon")Parameters:
aircraftTyperuleName
Returns: The value of the rule as a string/boolean/number - this will match the data type found in RULES.INI
Set the value of a rule for a given Aircraft type
setAircraftRule("ORCA", "Cost", 350)Parameters:
aircraftTyperuleNameruleValue
Returns: nothing
Get the value of a rule for a given Building type
getBuildingRule("GTWR", "Prerequisite")Parameters:
buildingTyperuleName
Returns: The value of the rule as a string/boolean/number - this will match the data type found in RULES.INI
Set the value of a rule for a given Building type
setBuildingRule("GUN", "Houses", "GOODGUY,BADGUY")Parameters:
buildingTyperuleNameruleValue
Returns: nothing
Functions that allow you to interact with the game engine.
Write a string to the ingame text notification box in the top left of the game HUD.
showGameMessage("Kane Lives!")Parameters:
message
Returns: nothing
Refresh the player sidebar, this will load all units/infantry/aircraft and buildings they currently have the ability to build. Useful to show a dyanmic change to the rules is made via lua and you want the player to be aware that things have changed.
refreshSidebar()Parameters: none
Returns: nothing
Reveal the entire map to the player.
revealEntireMap()Parameters: none
Returns: nothing
Hide the entire map from the player, leaving only Player owned units/structures etc. visible.
hideEntireMap()Parameters: none
Returns: nothing
Get the name of the house that the player is currently playing as.
local house = getPlayerHouse()Parameters: none
Returns: The name of the player house as a string, in the format seen in the RULES.INI file; GOODGUY etc.
Get the name of the house that the player is currently should behave like. This is used in multiplayer games to allow MULT1 for example to behave like GOODGUY if the user selected GDI in the game setup.
local baseHouse = getPlayerBaseHouse()Parameters: none
Returns: The name of the player base house as a string, in the format seen in the RULES.INI file; BADGUY etc.
Get the names of the houses that are currently active in the game, in multiplayer this is houses of AI and human players and in single-player this is the houses listed in the scenario INI file.
local houses = getActiveHouses()Parameters: none
Returns: A table containing the names of currently active houses. Houses are strings in the format seen in the RULES.INI file; BADGUY etc.
Add/subtract credits from a house in the currently running scenario.
modifyHouseCredits("BADGUY", 2000) -- add 2K
modifyHouseCredits("MULTI1", -130) -- remove 130Parameters:
-
houseNameThe name of the house (same format as seen in theRULES.INIfile,GOODGUYetc.) -
creditsThe amount of credits to add or subtract
Returns: The amount of credits the house now has (after modification)
Give a house a superweapon immediately, if they don't already have it. Note: The superweapon will not require the house to maintain a building if not marked as oneTimeOnly (see parameters) - i.e. don't need to have an Adv. Comm Center to keep the Ion Cannon. The only way to remove a superweapon that is not one time use only is to call disableSuperweaponForHouse.
enableSuperweaponForHouse("BADGUY", "NuclearStrike")
enableSuperweaponForHouse("MULT3", "AirStrike", true) -- one airstrike use onlyParameters:
-
houseNameThe name of the house (same format as seen in theRULES.INIfile,GOODGUYetc.) -
superWeaponNameThe name of the superweapon to enable (same format as seen in theRULES.INIfile,IonCannonetc.) -
oneTimeOnly(optional) Remove the superweapon from the house when it is fired? (similar to a superweapon from a crate in multiplayer)
Returns: nothing
Fully charge a superweapon for a house, has no affect if they don't have it.
chargeSuperweaponForHouse("GOODGUY", "AirStrike")Parameters:
-
houseNameThe name of the house (same format as seen in theRULES.INIfile,GOODGUYetc.) -
superWeaponNameThe name of the superweapon to charge (same format as seen in theRULES.INIfile,IonCannonetc.)
Returns: nothing
Remove a superweapon from a house immediately, does nothing if they don't have it.
disableSuperweaponForHouse("MUTLI2", "IonCannon")Parameters:
-
houseNameThe name of the house (same format as seen in theRULES.INIfile,GOODGUYetc.) -
superWeaponNameThe name of the superweapon to disable (same format as seen in theRULES.INIfile,IonCannonetc.)
Returns: nothing
Clears down any pending UI messages; these are generated when functions are called to updates the sidebar/map etc.
clearGameUiMessages()Parameters: none Returns: nothing
Clears down any pending Game Loop messages; these are generated by the game engine - currently only a game tick places an event on the queue.
clearGameLoopMessages()Parameters: none Returns: nothing
Clears down any pending House messages; these are generated when functions are called for superweapon control.
clearHouseMessages()Parameters: none Returns: nothing
Logging and error handling functions.
Write a string to the NCO log file
log("Some important message")
log(string.format("The current value of my var is %s", "some value")) -- write some values to the logParameters:
logMessage
Returns: nothing
Show the user a error message box with a string and write the error to the NCO log file
showError("Oh crap - something went really wrong")Parameters:
errorMessage
Returns: nothing
Get the current log level, see here for the various log levels available
local logLevel = getLogLevel()Parameters: none
Returns: String containing the current log level
Sets the current log level, see here for the various log levels available
setLogLevel("debug")Parameters:
logLevel
Returns: nothing
Turn on/off echoing non-error log messages to the console (Used for the Lua Console)
toggleConsoleLog()Parameters: none
Returns: nothing
Get the time in epoch milliseconds - the number of milliseconds since midnight on Jan 1st 1970 (UTC).
local millisSinceEpoch = getNowInEpochMillis()Parameters: none
Returns: Current time in epoch milliseconds
Get the full path to the NCO mod Data folder
local modPath = getModDataPath()Parameters: none
Returns: Path as a string
Get the full path to a file that is inside the NCO mod Data folder
local someCsvFile = buildModDataFilePath("data.csv")
local someTxtFile = buildModDataFilePath("my-mod\data\something.txt") -- you can specify sub-foldersParameters: none
Returns: Path to the given file as a string