AIComponent

Uses BehaviorTree to give AI to an Entity.

# Properties

[{"index":"string BehaviourTreeId (ReadOnly)","0":"The unique identifier of the behavior tree entry."}]

[{"index":"Blackboard BlackBoard (ReadOnly)","0":"This is the data store that is used in the AI behavior tree. It's managed in key-value pairs, and data is shared between behavior tree nodes."}]

[{"index":"boolean IsLegacy (ReadOnly)","0":"Sets whether to support the legacy system. Previous systems must use the ExclusiveExecutionWhenRunning property in order to set the node as Running. The legacy system is no longer supported and will be deleted at a later date."}]

[{"index":"boolean LogEnabled (ReadOnly)","0":"Outputs execution information into a log when the value is true while BehaviorTree runs. Operates in Maker mode only."}]

[{"index":"UpdateAuthorityType UpdateAuthority (ReadOnly)","0":"Update permission. Executed on the specified location (Client or Server)."}]

##### inherited from Component:

[{"index":"boolean Enable (Sync)","0":"Checks whether Component is activated or not."}]

[{"index":"boolean EnableInHierarchy (ReadOnly) (HideFromInspector)","0":"Displays whether this Component is Enable in the hierarchy. If Entity Enable is false regardless of Component Enable, returns false."}]

[{"index":"Entity Entity (ReadOnly)","0":"The Entity that owns this component."}]

# Methods

[{"index":"BTNode CreateLeafNode(string nodeName, func -> BehaviourTreeStatus onBehaveFunction)","0":"Creates an Action node. Calls function that had been passed to onBehaveFunction when the node is executed. Parameter of onBehaveFunction is delta, which means time per frame."}]

[{"index":"BTNode CreateNode(string nodeType, string nodeName = nil, func -> BehaviourTreeStatus onBehaveFunction = nil)","0":"Creates an Action node based on BTNodeType. The nodeType is the type name of BTNodeType. If the onBehaveFunction isn't nil, the function that was passed to the onBehaveFunction will be called instead of functions OnInit() and OnBehave() of the BTNodeType. The parameter of the onBehaveFunction is delta, which means time per frame."}]

[{"index":"void SetRootNode(BTNode node)","0":"Sets the node as the top-level node."}]

##### inherited from Component:

[{"index":"boolean IsClient()","0":"Returns whether the current execution environment is a client or not."}]

[{"index":"boolean IsServer()","0":"Returns whether the current execution environment is a server or not."}]

# Examples

This is an example of a monster sleeping and telling the player to back off if the player approaches. AIComponent is extended and used. Add the created script component to the static monster to use it.
    
    
    @Component
    script ProximitySentinel extends Component
        @Sync
        property number DetectDistance = 4
    
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
        local chatBallon = self.Entity.ChatBalloonComponent
        if chatBallon == nil then
        	chatBallon = self.Entity:AddComponent(ChatBalloonComponent)
        end
    
        self._T.nearestPlayer = nil
    
        chatBallon.AutoShowEnabled = true
        chatBallon.ChatModeEnabled = false
        chatBallon.ShowDuration = 1
        chatBallon.HideDuration = 0
        chatBallon.FontSize = 1.5
    
        local function isNearPlayer(deltaTime)
        	local players = _UserService:GetUsersByMapComponent(self.Entity.CurrentMap.MapComponent)
        	self._T.nearestPlayer = nil
        	local dist = math.maxinteger
        	for i, player in pairs(players) do
        		if isvalid(player) then
        			local distTemp = Vector2.Distance(
        			player.TransformComponent.Position:ToVector2(), self.Entity.TransformComponent.Position:ToVector2())
        			dist = math.min(dist, distTemp)
        			if dist <= self.DetectDistance then
        				self._T.nearestPlayer = player
        			end
        		end
        	end
    
        	if self._T.nearestPlayer == nil then
        		return BehaviourTreeStatus.Failure
        	else
        		return BehaviourTreeStatus.Success
        	end
        end
    
        local function lookAtNearestPlayer(deltaTime)
        	local flipX = self.Entity.TransformComponent.Position.x < self._T.nearestPlayer.TransformComponent.Position.x
        	self.Entity.SpriteRendererComponent.FlipX = flipX
        	return BehaviourTreeStatus.Success
        end
    
        local function warn(deltaTime)
        	chatBallon.Message = "Don't come!"
        	return BehaviourTreeStatus.Success
        end
    
        local function sleep(deltaTime)
        	chatBallon.Message = "Zzz..."
        	return BehaviourTreeStatus.Success
        end
    
        local rootNode = SelectorNode("Root")
    
        local alertSeq = SequenceNode("AlertSequence")
        alertSeq:AttachChild(self:CreateLeafNode("IsNearPlayer", isNearPlayer))
        alertSeq:AttachChild(self:CreateLeafNode("LookAtNearestPlayer", lookAtNearestPlayer))
        alertSeq:AttachChild(self:CreateLeafNode("Warn", warn))
    
        rootNode:AttachChild(alertSeq)
        rootNode:AttachChild(self:CreateLeafNode("Sleep", sleep))
    
        self:SetRootNode(rootNode)
        end
    end
    

# SeeAlso

  * ChatBalloonComponent
  * SpriteRendererComponent
  * TransformComponent
  * BTNode
  * SelectorNode
  * SequenceNode
  * UserService
  * [Creating AI with Behavior Tree](/docs?postId=562)