Set AI state priority||doc135571

September 20, 2022

Format

 AIControl:setStatePriority(name, prior)
  • The class to which the function belongs:AIControl

Description

Set the priority of the state. When both states meet the entry conditions, the state with higher priority is entered.


Create ai_state_high_priority_dance state (high priority dance state).

script code

local AIStateBase = require("entity.ai.ai_state_base")
---Make AIStateHighPriorityDance inherit from AIStateBase / Inherit AIStateHighPriorityDance from AIStateBase
local AIStateHighPriorityDance = L("AIStateHighPriorityDance", Lib.derive (AIStateBase))

---State name / State name
AIStateHighPriorityDance.NAME = "HIGH_PRIORITY_DANCE"

- --Called automatically when entering the state
function AIStateHighPriorityDance:enter()
 print("enter high priority dance state")
end[ [[]]]
---Loop calls when the current state is in
function AIStateHighPriorityDance:update()
 print("high priority dance state update")
end

---Called automatically when exiting the state
function AIStateHighPriorityDance:exit ()
 print("exit high priority dance state")
end

RETURN(AIStateHighPriorityDance)


Parameters

Variable name type Description
name string file name, state name
prior int priority, maximum when priority is 1


Code Samples

local control = entity:getAIControl()

local enterEvaluatorFunc = function()
    return true
end

control:addCustomState("ai_state_high_prior_dance", {}, enterEvaluatorFunc)
control:addCustomState("ai_state_dance", {}, enterEvaluatorFunc)

control:setStatePriority("ai_state_high_prior_dance", 1)