How should I manage the AI using lua scripts?

My game is in C++ and I want to make AI being managed by lua scripts, but I have no idea how should the scripts look like and the integration in C++.

Should the script be like

   if (whatever_happening) do_something
if (....) .....
etc


And in every frame in C++ I should call for every enemy:

   foreach(enemy)
enemy.handleAi(luascript.file)


Or it is wrong and I should handle it different?

A good approach is to design the behaviors in C++, and put the desicion parts in Lua scripts. The benefit is you can modify AI decisions without compiling your game; and with some good design, you may be able to modify AI desicions at runtime (opening up a debug window in game and change the variables to affect the decisions).

A behavior can be related to the position, the animations, and other status changing values. For example: Jump. This behavior changes the position, the velocity, and the animation of the AI, and also change the "IsInAir" and "IsJumping" flag.

A decision can be a branch of several behaviors. Here is the place you can use a state machine or behavior tree to implement the decision making system.

• I will go with this approach most likely. – user1873947 Feb 15 '13 at 23:22

This is a pretty general question with many different approaches. I'll just mention two major differences in coding AI logic into Lua.

As a prerequisite you will still need to expose C/C++ low level functions so that lua can make function calls to affect game systems or query for game information.

Movement, pathfinding, inventory, etc, etc

Script as low level decision logic

Basically you can use script for the very core decisions. How an AI may select a target, or how an AI chooses which weapon to use, etc. Think of lua functions or individual scripts as very small amounts of AI behavior.

This would mean C/C++ would call a function on the lua side, passing in values where the decision logic comes up with the result and passes the result back to the caller.

This type of approach works very well when the overall structure of what makes up an AI is in code and can be constructed either through hard coding or from a data source.

Structures like Finite State Machines with states in lua, or Behavior Trees where individual tree nodes are coded in lua fit perfectly in this model.

Script as high level decision logic

A completely different approach is to allow the lua script to completely control the AI. In this sense you could have a "think" lua function that is called every tick which represents the AI logic. The "think" function would need to be ticked either per frame or whenever is appropriate for the game you're making.

For example if lua is controlling animation playback it would probably need to be ticked per frame to have control over animation handling.

This approach isn't structured except for the known function that C/C++ calls into for the specific AI. All AI structure and logic can be written in any form you want in script with the AI storing any persistent knowledge as global variables in lua.

These are just two very different high level approaches to using scripting. Of course not taking into consideration memory or performance of the AI.

It depends. If determining_happening is expensive and well-defined, it may be beneficial to keep that calculation in C++. E.g. in C++

for (auto& enemy : enemies) {
if enemy.collide(player) enemy.callLua(collisionScript)
}

• good point. Are there any other simple approaches? – user1873947 Feb 13 '13 at 11:31