Lua and XML: Evaluator Scripts

Location: Data/Scripts/Evaluators

Evaluator scripts are used to get information from Lua into Perceptual Equations. The Evaluator scripts are called in XML using Script_ScriptName.Evaluate. Arguments can be passed to the script function using Parameter_Script_String and Parameter_Script_Number. The script must contain a function called Evaluate, and usually a function called Clean_Up.

Example: GetDistanceToNearestWithProperty.lua

This evaluator finds the nearest GameObject with the property flag passed to the function using Parameter_Script_String. As noted in the comments, the affiliation_type limits the GameObject search to enemies or friendlies relative to the PlayerObject calling the script (which is the same as the PlayerObject calling the perception).

Then the script returns either the distance to the target in alamo units or a BIG_FLOAT value, which would usually be larger than the map size.

=== Modding Notes ===

Notice that the string comparison is capitalized. Strings sent from XML to Lua always end up capitalized.

The script EvaulateInGalacticContext allows you to evaluate equations in tactical mode as though you are in GC mode. The vanilla game uses this to decide if a planet is worth too much to retreat from, as an example. There are many other powerful uses of this script.

It is imperative that object pointers (i.e. Lua variables that point to game objects) are set to nil in the Clean_Up function, or memory errors can result.

Unlike other scripts, Evaluators must be inside a MEG file to be read!