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

Handling auditory events


Handling auditory events requires you to add additional time-to-live information to each stored event. As a large number of bullet shots and impacts events are emitted, the time-to-live information will be used to prune out old events:

AgentSenses.lua:

local function HandleBulletImpactEvent(
    userData, eventType, event)
    
    local blackboard = userData.blackboard;
    local bulletImpacts = blackboard:Get("bulletImpacts") or {};
    
    table.insert(
        bulletImpacts,
        { position = event.position, ttl = 1000 });
    blackboard:Set("bulletImpacts", bulletImpacts);
end

local function HandleBulletShotEvent(userData, eventType, event)
    local blackboard = userData.blackboard;
    local bulletShots = blackboard:Get("bulletShots") or {};
    
    table.insert(
        bulletShots,
        { position = event.position, ttl = 1000 });
    blackboard:Set("bulletShots", bulletShots);
end

We can now add our auditory event handlers during the AgentSenses initialization...