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

Building a behavior tree


Building a behavior tree is very similar to building a decision tree, except for the addition of selectors and sequence node types.

The soldier's behavior tree

We can start creating a behavior tree using a similarly wrapped function that instantiates a behavior tree and creates the first selector node for the tree:

SoldierLogic.lua:

function SoldierLogic_BehaviorTree(userData)
    local tree = BehaviorTree.new(userData);
    
    local node;
    local child;
    
    node = CreateSelector();
    tree:SetNode(node);
    
    return tree;
end

The death behavior

To add the first action, which is death, we add the required sequence, condition, and action nodes. As the behavior tree will completely re-evaluate once an action completes, we don't have to worry about other actions knowing about death. As prioritizing actions is based on how early in the tree they appear, we add death first, because it has the highest priority:

SoldierLogic.lua:

function SoldierLogic_BehaviorTree...