Overview

The Easy Red 2 Scripting System is an optional add-on that allows you to create new game modes and customize missions as well as AI and soldier behaviors. It works alongside the default Mission Editor and enables you to:

The system leverages Lua, meaning you have access to all standard Lua features like tests, loops, functions, and conditions. For general Lua language documentation, please refer to the Lua Documentation.

For Easy Red 2–specific Lua commands and further examples, check out our dedicated scripting wiki.

Scripting Mission Phases

It is possible to run a script at the beginning of each mission phase. The script starts executing when the phase begins and is interrupted when a new phase starts or when it has finished executing. If a previously run phase is started again, the script will run once more.

Within the Mission Editor UI, you will find a script button:

Script Button in Mission Editor

Clicking this button opens a code editor that allows you to edit the script for the current phase. To save your changes, simply close the editor by clicking the red cross:

Code Editor with Red Cross to Save

Additionally, scripts are stored in a dedicated subfolder of the mission folder. This allows you to edit them using an external code editor if you prefer:

Mission Folder with Script Subfolder

When playing offline, your machine runs all the scripts. In online matches, the phase script runs on the master client only, although most scripting commands generate actions synchronized across all players.

Note: If the master client leaves, a new one is assigned and the phase script will run from the beginning on that machine. For this reason, it is recommended to use the global class to set global variables that keep track of the phase state, ensuring that operations already performed are not unnecessarily repeated.

Below is an example of a mission phase script:

-- Mission Phase Script Example
-- check if a certain "target_spawned" global boolean variable have been set already;
-- If not, then the script is running for the first time in this phase
-- and we want to do the initialization actions, like spawning targets
if not global.get("targets_spawned") then
    print("Spawning targets to eliminate...")
    -- Perform targets spawn operations here ...
    
    -- Next, set the "targets_spawned" to true,
    -- so if this script will run again* we know that this action has performed.
    global.set(true, "targets_spawned")
    
    -- *Why could the script run again?
    -- Phase script is executed every time the phase starts (or re-start)
    -- or whenever the host has changed
end

-- Example of main phase loop
local phaseCompleted = false
while not phaseCompleted do
    sleep(3) -- always recommended to put some wait time on every loop!
    
    if checkTargetsStillAlive() then -- dummy function
        phaseCompleted = true
    end
end

--loop is done so continue with next phase...
er2.nextPhase()

Scripting AI & Soldier Behavior

In addition to mission phases, you can script the behavior of AI and soldiers. This is done through the Squad Editor where you can create, edit, and delete AI scripts.

In the Squad Editor, navigate to the Manage AI Scripts section:

AI Script Manager - Open Button

Manage AI Scripts Menu

AI scripts can be assigned in two ways:

For example, you might assign an AI script manually like this:

-- AI Script Assignment Example
        --spawn an unit
local aiUnit = spawnSoldier(vec3(10,0,10), "England_allies", 0, "eng_infantry_gunner")

if aiUnit then
    -- give the unit some lua script
    aiUnit.setBrain("ai_patrol.lua")
end

AI scripts run locally on the owner’s machine. When using a squad spawner, the entire squad is owned by you so the script executes on your computer. However, AI squads are ultimately managed by the master client – including their Lua scripts.

Important: If a player leaves an online match, their units (and associated scripts) transfer to the master client, where the scripts will restart. It is therefore recommended to use the global class to verify whether certain operations should be skipped on a fresh run.

Like phase scripts, AI scripts are stored in their own subfolder within the mission folder, making them accessible for external editing.

Manage AI Scripts Menu

A notable feature in soldier scripts is the myself() function, which returns a reference to the Soldier class instance running the script. This allows you to call specific soldier functions directly.

For example, a soldier script might look like this:

-- Get the unit which is running this script
local soldier = myself()

-- Make the unit play the VoiceClip.scream_long clip every 5 seconds while being alive
while sold.isAlive() do
    sold.say(VoiceClip.scream_long)
    sleep(5)
end