Add AI status||doc135569

September 20, 2022

Format

 AIControl:addCustomState(name, params, enterEvaluatorFunc)
  • The class to which the function belongs:AIControl

Description

Adds a custom AI state, and can also add basic states.
The last state added has the highest priority by default.

Duplicate addition is invalid


Parameters

Variable name type Description
name string file name, state name
params table AI configuration, similar to configuration information usage
enterEvaluatorFunc func entry condition, the state machine switches to this state when the condition returns true


Code Samples

local control = entity:getAIControl()

---Are there other entities in scope 4
local enterEvaluatorFunc = function()
    local selfEntity = control:getEntity()
    local map = selfEntity.map
    local pos = selfEntity:getPosition()

    local entities = map:getNearbyEntities(pos, 4)
    return #entities > 1
end

---Dance when someone approaches
control:addCustomState("ai_state_dance", { danceAction = "happy_dance" }, enterEvaluatorFunc)