Book Image

Learning Game AI Programming with Lua

By : David Young
Book Image

Learning Game AI Programming with Lua

By: David Young

Overview of this book

Table of Contents (16 chapters)
Learning Game AI Programming with Lua
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Getting Started with AI Sandbox
Index

Creating a behavior tree agent


To use our behavior tree, we can replace the finite state machine and perform the same update loop on our behavior tree instead. Regardless, if our agent is using a decision tree, state machine, or behavior tree, its actions will be nearly identical, as the logic is merely translated from one decision structure to another:

IndirectSoldierAgent.lua:

local soldier;
local soldierController;
local soldierBT;
local soldierUserData;

function Agent_Initialize(agent)

    ...
    
    soldierBT = SoldierLogic_BehaviorTree(
        soldierUserData);
end

function Agent_Update(agent, deltaTimeInMillis)
    if (soldierUserData.alive) then
        soldierBT:Update(deltaTimeInMillis);
    end

    soldierController:Update(agent, deltaTimeInMillis);
end