.xs scripting for is a new functionality added to Age of Empires II: Definitive Edition, available with the November 2020 update. It adds another layer of scripting functionality to the existing trigger system in the scenario editorand in random map scripts.
This is official reference for RMS/Trigger functions accessible in AoE2. For a more in-depth guide, including a reference for beginners in programming, as well as the usage of Rules and other XS features, please check out The UGC Guide for XS created by the community!
Summary:
- Make sure function names are unique. (e.g. having multiple functions called Function() will cause a conflict)
- Make sure the function returns the defined type (e.g. bool Function() should return a boolean expression)
- If you wrote a function inside of the Scenario Editor, it will be compiled into an .xs file called default0.xs. These functions will be called automatically for that specific Script Call.
- You must place your .xs scripts in resources\_common\xs
- You can find the Constants.xs file with all constants (like for instance
cAttributeStone) and effects (SetAttribute)that can be used within some functions in \resources_common\xs - When entering the Script Name inside the Editor, you do not need to include the final .xs extension.
- You can define as many functions as you want inside an .xs file.
- To call functions in the Editor, you only need to pass in the function name (e.g. if you had a function called doesPlayerHaveFood() you will only need to write doesPlayerHaveFood in the Script Call textbox.)
Scenario Editor Functions
| xsTriggerVariable | Returns the value of the desired Trigger Variable.
Syntax:
|
// Returns the value of Trigger Variable 0.
|
| xsSetTriggerVariable | Sets a trigger variable to the desired value.
Syntax:
|
// Set Trigger Variable 0 to 100.
|
| xsEffectAmount | Allows you to modify a unit or building with research-style type effects.
Set playerId to -1 to apply the change to all players. Syntax:
|
// Modify players starting resources, in this case, set starting stone to 50.
|
| xsResearchTechnology | Allows you to research a technology for a specific player. This can be done forcefully without any condition checking.
You can also check to research the technology if it’s available. This function will return true if research was successful or false if it was not. Syntax:
|
// Research Loom for player 2.
|
| xsGetPlayerNumberOfTechs | Returns the number of technologies a player can research.
Syntax:
|
// Get the number of technologies from player 3.
|
Random Map Script Functions
| xsEffectAmount | Allows you to modify a unit or building with research-style type effects.
Set playerId to -1 to apply the change to all players. Syntax:
|
// Modify players starting resources, in this case, set starting stone to 50.
|
Shared Functions
| xsChatData | Sends a chat with the desired message/variable. This is useful for testing.
Syntax:
|
// Chat player 1’s food.
|
| xsPlayerAttribute | Returns the value of the desired player attribute.
Syntax:
|
// Get the food amount of player 1.
return (
|
| xsSetPlayerAttribute | Sets the desired player attribute to the value specified.
Syntax:
|
// Set player 1 food to 100.
|
| xsGetNumPlayers | Returns the number of players in a match (Excluding Gaia) E.g. if there is 2 players in a match, only 2 is returned.
Syntax:
|
// Get the number of players.
|
| xsGetPlayerCivilization | Returns the civilization ID of a specific player.
Syntax:
|
// Get the civilization ID of player 1
|
| xsGetPlayerInGame | Returns true if the specific player is in the game still, if the specified player has been defeated, resigned or dropped from the match, it will return false.
Syntax:
|
// Check if player 4 is still in the match.
|
| xsGetGameTime | Returns the elapsed game time (in seconds).
Syntax:
|
// Get the game time.
|
| xsGetVictoryPlayer | Returns the player ID of who will win using relics, wonder, monastery or monuments.
Syntax:
|
// Get the winning player ID
|
| xsGetRandomNumber | Generate a random number.
Syntax:
|
// Get a random number
|
| xsGetRandomNumberLH | Generate a random number using between low and high.
Syntax:
|
// Generate a random number between 5 - 10.
|
| xsGetRandomNumberMax | Generate a random number using a maximum value.
Syntax:
|
// Generate a random number with a maximum value of 3.
|
| xsCeilToInt | Receives a float and return an int rounded up.
|
// Round up a float value 4.1
|
| xsGetObjectAttribute | Get an attribute from an object, such as HP.
Syntax:
|
// get P1's Archer HP
|
| xsGetPlayerName | Get a player’s name
Syntax:
|
// get P1's Name
|
| xsIsObjectAvailable | Check if an object is enabled (available)
|
// check if P1 can make archers:
|
| xsGetTechState | Get the technology’s state
Syntax:
The possible tech states are: NotReady = 0 |
// Get the tech state for Loom for P1:
|
| xsGetTechName | Get a tech’s name.
Syntax:
|
// get the name of tech 22 (Loom) for P1
|
| xsGetObjectName | Get an object’s name.
Syntax:
|
// get the name of object 4 (Archer) for P1
|
| bitCastToFloat | Perform a bit cast from int to float
Syntax: |
bitCastToFloat(1065353216)
|
| bitCastToInt | Perform a bit cast from float to int
Syntax: |
bitCastToInt(1.0)
|
Unit Functions
| xsGetPlayerUnitIds | Get a list of reference unit IDs
Syntax:
|
// get P1's Archers
|
| xsGetUnitOwner | Get the owner player ID of the given unit ID.
Syntax:
|
// Get the player of unit ID 0
|
| xsGetUnitTargetId | Get the target unit ID of the given unit ID.
Syntax:
|
// Get the target unit of unit ID 0
|
| xsDoesUnitExist | Check if the given unit ID exist on the map.
Syntax:
|
// Check if unit ID 0 exists
|
| xsGetUnitPosition | Get the position of the given unit ID on the map.
Syntax:
|
// Get the position of unit ID 0
|
| xsGetUnitMoveTarget | Get the destination of the unit if it is moving, cInvalidVector otherwise.
Syntax:
|
// Get the position of unit ID 0
|
| xsGetUnitMoveTarget | Get the destination of the unit if it is moving, cInvalidVector otherwise.
Syntax:
|
// Get the destination of unit ID 0
|
| xsGetUnitGroupId | Get the group ID (formation) of the given unit ID.
Syntax:
|
// Get the group ID of unit ID 0
|
| xsGetGroupMoveTarget | Get the destination of the unit group if it is moving, cInvalidVector otherwise.
Syntax:
|
// Get the destination of group ID 0
|
| xsGetUnitName | Get a unit’s name
Syntax:
|
// get unit 0's name
|
| xsGetUnitAttribute | Get an attribute from a unit on the map, such as HP.
Syntax:
|
// get unit 0's Max HP
|
| xsGetUnitHitpoints | Get a unit’s current HP.
Syntax:
|
// get unit 0's Current HP
|
| xsSetUnitHitpoints | Set a unit’s current HP.
Syntax:
|
// set unit 0's HP to 50000
|
| xsGetUnitBuildPoints | Get a unit’s current BP.
Syntax:
|
// get unit 0's Current BP
|
| xsSetUnitBuildPoints | Set a unit’s current BP.
Syntax:
|
// set unit 0's BP to 200
|
| xsGetUnitObjectId | Get a unit’s object ID
Syntax:
|
// get unit 0’s object Id
|
| xsGetUnitCopyId | Get a unit’s copy ID. This describes which object this unit was last upgraded into
Syntax:
|
// get unit 0’s current copy Id
|
| xsGetObjectCopyId | Get an object’s copy ID. This describes which object this unit was last upgraded into
Syntax:
|
// Get the current copy id of player 1's archers
|
| xsGetUnitClass | Get a unit’s class (+900, see Constants.xs)
Syntax:
|
// get unit 0’s class
|
| xsGetObjectClass | Get an object’s class (+900, see Constants.xs)
Syntax:
|
// Get the class of player 1's archers
|
| xsGetUnitType | Get a unit’s type (see Constants.xs)
Syntax:
|
// get unit 0’s type
|
| xsGetObjectType | Get a unit’s type (see Constants.xs)
Syntax:
|
// Get the object type of player 1's archers
|
| xsGetUnitAttributeTypesHeld | Get an array containing the types of resources that a unit is holding
Syntax:
|
// Get a list of the type of resources held by unit 1
|
| xsGetUnitAttributeHeld | Get the amount of a particular resource held by a unit
Syntax:
|
// Get the amount of wood held by unit 1
|
| xsSetUnitAttributeHeld | Set the amount of a particular resource held by a unit
Syntax:
|
// Set the amount of wood held by unit 1 to 10
|
| xsGetUnitCharge | Get a unit’s current charge.
Syntax:
|
// get unit 0's Current charge
|
| xsSetUnitCharge | Set a unit’s current charge.
Syntax:
|
// set unit 0's charge to 50
|
| xsSetUnitPosition | Sets a unit’s position
Syntax:
|
xsSetUnitPosition(0, vector(5, 5, -1)) // teleports unit 0 to 5, 5 |
| xsGetWorldPlayerId | Gets the world player (lobby slot) from the scenario player index
Syntax:
|
xsGetWorldPlayerId(5) // find out which world player Aqua |
| xsGetGarrisonedInUnitId | Gets the unit that this unit is garrisoned in (or -1 if not garrisoned)
Syntax:
|
xsGetGarrisonedInUnitId(0) // returns the id of the unit that unit 0 is garrisoned in |
| xsGetGarrisonedUnitIds | Gets all the IDs for units that are garrisoned in this unit
Syntax:
|
xsGetGarrisonedUnitIds(0) // returns an array of unit IDs garrisoned inside unit 0 |
| xsGetPlayerType | Get whether a player is human or AI
Syntax:
|
xsGetPlayerType(1); // get the player type of P1 |
| xsRemoveUnit | Removes a unit from the map
Syntax:
|
xsRemoveUnit(0); // removes unit 0 from the map |
| xsGetDiplomacy | Get the diplo of source player against target player
Syntax:
|
xsGetDiplomacy(1, 2); // returns the stance of p1 against p2 |
| xsSetDiplomacy | Set the diplo of source player against target player
Syntax:
|
xsSetDiplomacy(1, 2, cDiplomacyAlly); // make p1 ally p2
|
| xsGetColorMood | Get the color mood of the current map
Syntax:
|
xsGetColorMood(); // get the current color mood |
| xsSetColorMood | Set the color mood of the current map
Syntax:
|
xsSetColorMood(cColorMoodWinter, 10); // transition the color mood to winter over 10 seconds |
| xsGetDifficulty | Get the difficulty setting in the lobby
Syntax:
|
xsGetDifficulty() // get the AI difficulty |
| xsCreateUnit | Create a unit on the map with the specified options
Syntax:
|
xsCreateUnit(cArcher, 1, vector(4, 4, -1)) // create an archer at 4, 4 on the map for P1 |
Read/Write Functions
| xsCreateFile | Opens a file with the .xsdat extension for writing to. Returns true if the file was successfully opened to write to. Setting append to false will result in a new file being written and the old one being lost.
The file name will use the name of the scenario and random map. Files can only be a maximum of 1 megabyte. Each file is unique to each player’s profile.
|
// Create a brand new xs data file.
|
| xsOpenFile | Opens a .xsdat file for reading only. Will return true if file was successfully opened for reading.
|
// Open a file for reading
|
| xsWriteString | Writes a string to the file that has been opened using xsCreateFile. Returns true if the write was successful.
|
// Write a string to the xs data file.
|
| xsWriteInt | Writes an integer to the file that has been opened using xsCreateFile. Returns true if the write was successful.
|
// Write an integer to the xs data file.
|
| xsWriteFloat | Writes a float to the file that has been opened using xsCreateFile. Returns true if the write was successful.
|
// Write a floatto the xs data file.
|
| xsWriteVector | Writes a vector to the file that has been opened using xsCreateFile. Returns true if the write was successful.
|
// Write a vector to the xs data file.
|
| xsReadString | Reads a string from the currently opened file at the current file position. Returns a string if successfully read.
|
// Read a string from the xs data file
|
| xsReadInt | Reads an integer from the currently opened file at the current file position. Returns a int if successfully read.
|
// Read an int from the xs data file
|
| xsReadFloat | Reads a float from the currently opened file at the current file position. Returns a float if successfully read.
|
// Read a float from the xs data file
|
| xsReadVector | Reads a vector from the currently opened file at the current file position. Returns a vector if successfully read.
|
// Read a vector from the xs data file
|
| xsSetFilePosition | Sets the current position in file directly for the currently opened file. Returns true if it was successful.
|
// Set the file position (buffer) to the beginning
|
| xsOffsetFilePosition | Jump forward or backward from the current file position by using the desired data type. Returns true if it was successful.
Supported data types are: cOffsetString (assumes current position is at a string) cOffsetInterger cOffsetFloat cOffsetVector
|
// Skip reading an integer
|
| xsCloseFile | Closing the currently opened file (either by xsCreateFile or xsOpenFile) Returns true if a file was successfully closed. | // Close the currently opened file.
|
Functions for Creating, Editing, and Removing Tasks from Units
| xsTaskAmount | Allows you to specify the value of a field in a task which will be used in either a following xsTask or xsRemoveTask function call.
Syntax:
|
// Modify King Henry II's weapon damage.
|
| xsTask | Insert a new task, if a task with the specified key fields does not already exist.
Set playerId to -1 to apply the change to all players. Syntax:
|
// Modify King Henry II's weapon damage.
|
| xsRemoveTask | Remove a task from a master unit, if a task with the specified key fields already exists.
Set playerId to -1 to apply the change to all players. Syntax:
|
// Modify King Henry II's weapon damage.
|
| xsGetObjectTaskCount | Get the number of tasks on a data object
Syntax:
|
int xsGetObjectTaskCount(cArcher, 1); // get the number of tasks on the archer object of player 1 |
| xsGetUnitTaskCount | Get the number of tasks on a unit on the map.
Syntax:
|
int xsGetObjectTaskCount(0); // get the number of tasks on the unit with ID 0 on the map |
| xsGetObjectTaskAmount | Copies the specified task from the data object into XS
Syntax:
|
int xsGetObjectTaskAmount(cArcher, 1, 0); // get the first task on the archer object of player 1 |
| xsGetUnittTaskAmount | Copies the specified task from the unit on the map into XS
Syntax:
|
int xsGetUnitTaskAmount(0, 0); // get the first task on the unit with ID 0 on the map |
| xsGetTaskAmount | Gets a field from the XS task struct
Syntax:
|
xsGetTaskAmount(cTaskAttrTaskType); // get the task type of the XS task struct |
| xsModifyObjectTasks | Adds/Edits/Removes a task from a data object
Syntax: |
bool xsModifyObjectTasks(cArcher, 1, 0) // inserts the XS task struct into the archer object for player 1
|
| xsModifyUnitTasks | Adds/Edits/Removes a task from a unit on the map
Syntax: |
Same as above, except the int objectId, int playerId parameters are replaced with int unitId to affect a specific unit on the map. |
XS scripts within RMS
To use an XS script in a Random Map Script, do the following:
- At the top of your RMS file (before <PLAYER_SETUP>) add #includeXS scriptname.xs
- “scriptname.xs” is the name of the file located in your resources\_common\xs folder. You can put the file name in quotation marks if it contains spaces. Paths relative to the “xs” folder are allowed.
- In the XS file, create a void main() function definition. All of its content will be executed upon loading the Random map script. You can use all general functions and RMS-specific functions (described at the top of this document).
How to write functions which accept parameters
If you want to write a function which accepts parameters (e.g. you want to chat the amount of food a player has) you must initialize your parameter variables.
Example:

Example on how a function written inside the editor looks:
