Add AI status||doc135569

September 20, 2022


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


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


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 =
    local pos = selfEntity:getPosition()

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

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