- Learning Game AI Programming with Lua
- David Young
- 459字
- 2021-08-05 17:02:13
Creating an agent Lua script
To start creating an agent, we need to create another Lua script that implements the Agent_Cleanup
, Agent_HandleEvent
, Agent_Initialize
, and Agent_Update
functions:
Create the Lua file as follows:
src/my_sandbox/script/Agent.lua
Agent.lua
:
function Agent_Cleanup(agent) end function Agent_HandleEvent(agent, event) end function Agent_Initialize(agent) end function Agent_Update(agent, deltaTimeInMillis) end
Now that we have a basic agent script, we can create an instance of the agent within the sandbox. Modify the initialization of the sandbox in order to create your AI agent with the Sandbox.CreateAgent
function.
Tip
Remember that each AI agent runs within its own Lua virtual machine (VM). Even though a separate VM is running the agent logic, you can still access and modify properties of an agent from the sandbox Lua script, as the C++ code manages agent data.
Modify the initialization of the sandbox in order to create your AI agent with the Sandbox.CreateAgent
function.
Sandbox.lua
:
function Sandbox_Initialize(sandbox) ... Sandbox.CreateAgent(sandbox, "Agent.lua"); end
Creating a visual representation
Now that you have an agent running within the sandbox, we need to create a visual representation so that we can see the agent. This time, we use the Core.
CreateCapsule
function to procedurally generate a capsule mesh and attach the mesh to the agent itself. Passing the agent to Core.CreateCapsule
will attach the Ogre mesh directly to the agent and automatically update the position and rotation of the capsule as the agent moves.
We only need to create a visual representation compared to a Sandbox.CreateObject
object, as agents are already simulated within the physics simulation with a capsule representation:
Create the Lua file as follows:
src/my_sandbox/script/AgentUtilities.lua
AgentUtilities.lua
:
function AgentUtilities_CreateAgentRepresentation( agent, height, radius) -- Capsule height and radius in meters. local capsule = Core.CreateCapsule(agent, height, radius); Core.SetMaterial(capsule, "Ground2"); end
Agent.lua
:
function Agent_Initialize(agent) AgentUtilities_CreateAgentRepresentation( agent, agent:GetHeight(), agent:GetRadius()); end
Running the sandbox now will show our agent's visual representation, a capsule using the same Ogre Ground2
material.

A basic capsule representation of our agents
Updating an agent position
To start moving an agent around directly, we can set the agent's position. As agents are simulated within the physics simulation, they will fall to the ground if they are positioned in the air or will be pushed to the top of the ground plane if placed below:
-- Position in meters. local position = Vector.new( xCoordinate, yCoordinate, zCoordinate); Agent.SetPosition(agent, position);
Updating an agent orientation
Changing the orientation of an agent is similar to setting a position vector, except that a forward vector must be provided. As the sandbox simulates humanoid agents, the physics simulation locks the orientation of the agent to force agents upright. When setting a forward vector of an agent, the sandbox assumes that the y axis is considered the up axis within the sandbox:
local forwardDirection = Vector.new( xDirection, 0, zDirection); Agent.SetForward(agent, forwardDirection);
- ETL數(shù)據(jù)整合與處理(Kettle)
- 大數(shù)據(jù)導(dǎo)論
- Learning JavaScriptMVC
- 大數(shù)據(jù):規(guī)劃、實(shí)施、運(yùn)維
- Remote Usability Testing
- 數(shù)據(jù)庫技術(shù)及應(yīng)用教程
- 金融商業(yè)算法建模:基于Python和SAS
- 高維數(shù)據(jù)分析預(yù)處理技術(shù)
- Python數(shù)據(jù)分析與數(shù)據(jù)化運(yùn)營
- 探索新型智庫發(fā)展之路:藍(lán)迪國際智庫報(bào)告·2015(上冊(cè))
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- 離線和實(shí)時(shí)大數(shù)據(jù)開發(fā)實(shí)戰(zhàn)
- 數(shù)據(jù)挖掘與機(jī)器學(xué)習(xí)-WEKA應(yīng)用技術(shù)與實(shí)踐(第二版)
- 標(biāo)簽類目體系:面向業(yè)務(wù)的數(shù)據(jù)資產(chǎn)設(shè)計(jì)方法論
- 算力芯片:高性能CPU/GPU/NPU微架構(gòu)分析