Spawning Entities

From Age of Empires 4 Modding


Spawning units

To spawn units we have to first get the squad blueprint for the unit with BP_GetSquadBlueprint. Then we can create a squad group and deploy the squad into it. Finally the entity can be extracted from our squad group.

Keep in mind that the unit has to be precached by adding it to the wincondition's precache list or it won't show up properly in game.

-- Get the squad blueprint by name
local squad_blueprint = BP_GetSquadBlueprint("gaia_herdable_sheep")

-- Get or create a squad group named sg_sheep
local squad_group = SGroup_CreateIfNotFound("sg_sheep")

UnitEntry_DeploySquads(player.id, squad_group, {{sbp=squad_blueprint, num_squads=1}})

-- Select the single entity in our squad group
local entity = Squad_EntityAt(SGroup_GetSpawnedSquadAt(squad_group, 1), 0)

// Get the squad blueprint by name
const squadBlueprint = BP_GetSquadBlueprint("gaia_herdable_sheep")

// Get or create a squad group named sg_sheep
const squadGroup = SGroup_CreateIfNotFound("sg_sheep")

UnitEntry_DeploySquads(player.id, squadGroup, [{sbp: squadBlueprint, num_squads: 1}])

// Select the single entity in our squad group
const entity = Squad_EntityAt(SGroup_GetSpawnedSquadAt(squadGroup, 1), 0)

If you know of a better way to spawn single units without creating entire squads first please edit this page.


Spawning Buildings

Spawning buildings is same as spawning units for most parts, but requires few additional steps.

Following example spawns keep next to start position of player at index 1.

-- Obtaining position and adjusting it to correct grid location. 
-- It must be aligned otherwise it will incorrectly place influence of buildings.
local player = World_GetPlayerAt(1)
local ps = Player_GetStartingPosition(player)
local nx = 5 * math.floor(math.floor(ps.x) / 5)
local ny = 5 * math.floor(math.floor(ps.y) / 5)
local nz = 5 * math.floor(math.floor(ps.z) / 5)
-- this is position at which building will appear
local psx = World_Pos(nx + 15, ny, nz)
-- this vector is used for rotation target, so building is facing correctly toward camera
local pst = World_Pos(nx + 15, ny, nz + 5)
-- Spawning is following 4 lines
local e = Entity_CreateFacing(BP_GetEntityBlueprint("building_defense_keep_control_abb"), player, psx, pst, true)
Entity_Spawn(e)
Entity_ForceConstruct(e)
Entity_SnapToGridAndGround(e, false)

// Obtaining position and adjusting it to correct grid location. 
// It must be aligned otherwise it will incorrectly place influence of buildings.
const player = World_GetPlayerAt(1)
const ps = Player_GetStartingPosition(player)
const nx = 5 * Math.floor(Math.floor(ps.x) / 5)
const ny = 5 * Math.floor(Math.floor(ps.y) / 5)
const nz = 5 * Math.floor(Math.floor(ps.z) / 5)
// this is position at which building will appear
const psx = World_Pos(nx + 15, ny, nz)
// this vector is used for rotation target, so building is facing correctly toward camera
const pst = World_Pos(nx + 15, ny, nz + 5)
// Spawning is following 4 lines
const e = Entity_CreateFacing(BP_GetEntityBlueprint("building_defense_keep_control_abb"), player, psx, pst, true)
Entity_Spawn(e)
Entity_ForceConstruct(e)
Entity_SnapToGridAndGround(e, false)