AI-how much can you do with BGE and Python?

Hello all
All blender games i have seen seem to be lacking in one important element: AI artificial intelligence.
I’m just wondering exactly how much Blenders game engine and Python are capable of. Not having tried it yet myself i would say that all the tools necessary to create a fairly sophisticated AI are there. If anyone else has tried this or knows anything about good ways to implement this with Python then please let me know! it would be much appreciaed :eyebrowlift:

I know that to make significantly intelligent AI is quite a task. Often the most difficult part, from my understanding, is not creating intelligent processes (well for games, usually an illusion thereof), but creating the interface between that AI and its environment (this is even more so the case with real world AI applied to robotics or autopilot). For 3d games this usually requires navmesh, physics hulls or some other property of physical meshes for the AI to sense where it is, various sensors to simulate its sight or hearing as well as kind of direct input ‘intuition’ for it to just know stuff like its objective and ‘telepathy’ between AI.

Then there is the output side which is equally difficult, once the AI knows stuff about its environment and has decided what to do it has to implement that. I suppose the biggest thing here is pathfinding, there are many methods like the A* algorithm for dynamically moving and updating goals. Then theres other actions and their corresponding animations which can take a lot of tweeking.
Then theres the intelligent processes or decision making systems which probably vary the most from game to game. To me at least this is the fun part!
I have progressed in my python skills a bit and thought it was time to take it on!
I have found Python a bit weird after doing some C++ and machine code uuurgh :no: but ill get there eventually i hope :slight_smile:

Soooo, i’m just curious of what others know about/think of this as im gonna try anyways :slight_smile: so please let us know and hopefully eventually Blender will have a built in module for customising your own game AI!
Cheers, Myall

…iisthphir… fire that came from the darkness

Apparently in 2.5 we’re getting proper integrated pathfinding, which is a massive step toward AI- but it’s worth noting that there are teams of developers in games whose entire task is to work on the AI, which for any reasonably believable human-like behavior is remarkably complicated. A small game has better odds with simpler AI and scripted or otherwise closely controlled events, than trying to realistically simulate human behavior and interactions.

Given a lot of time and coders, I’d imagine some pretty impressive stuff is possible in the BGE, but if I were you I’d shoot for something closer to Half-life AI than Unreal Tournament.

Hi iisthphir,

Quite a good analysis of the AI situation.

my view on the game AI (for an adventure) is following:

Have a graph of options (usually a FSM wher node = state) which is the knowledge.

Goal setting:
First of all, there must be a goal or target. This can be defined by clicking an Object, pressing a key. If that happens the goal could be: Stay at this point or grap this thing.

The goal should be one of the states of the FSM.
If it is not present, you need to transform your goal into a state or add states that contain the goal (learning). Otherwise it is impossible to reach the goal. Sounds complicated, isn’t it.

Decision making:
Run a pathfinder (doesn’t matter wich one) to find a path from the current state to the goal state. The result is a plan.

Now let the character follow the plan. This shouldn’t be that difficult, it is just a list.

If the plan can’t be followed anymore, because the situation changed (path is blocked) or the goal changed go back to decision making.

If the character reaches the goal state, it is done. Usually the new goal is waiting for a new goal.

This does not mean the character can discover how to use a bike, just by looking at a wheel (it can mean that).
It simply means extend the knowledge. This can be done by adding new states and transitions to the knowledge FSM. This approach allows to add new knowledge while the game is running. This could be quite powerfull. (forgetting is an option too ;))

If you keep this AI parts separate you can replace each module by another e.g. different Pathfinding, different Action, different knowledge, different learning.
By the way the knowledgegraph is not limited to positions in space. It can contain, animation states, inventory states etc.

If you think about that, it would be possible to give a character the goal: “game end” to force the character to play the game without userinteraction :).

A finite state machine is most likely the easiest AI system to create and implement. I use them quite often as they are flexible and their uses can range from AI, movement, menu handling and basically anything that has a fixed number of states.

I have also worked on trying to create behavior trees in python, complete with an editor to create and export custom AI scripts:

However at the time my python skills were perhaps not as good as I had thought and the AI didn’t execute all that great, looking back I can definitely imagine much better ways of doing it. The execution of code in the bge was probably what really broke the system, I had continuous execution in mind while the game engine executes in ticks.

I am working on making a FSM with a goal of having enough utility functions to create a decent framework, however, like you have said its not just about reaching that decisions its also about carrying out the decision in the game. I have uploaded and added in some docstrings if you want to have a look. A FSM could be created using about 30 lines, I just feel that breaking it up helps to understand it.

I personally find AI quite interesting, you also seem keen on it so perhaps you might find these links useful: ( Resources > articles > AI)

Hi andrew,

cool idea with the GUI. I used Python scripts containing dictionaries. But this was not flexible enought for me.
Now I have a table configuring the states and a table configuring the transitions. Both states and transitions are classes, so there is alot of things that can be done but coding.

What I really would like to have is a state editor similar to the OOPS schema of the outliner, where you can see the graph. But to program this is a project of its own.

Do you know that you can start threads? that enables you to have parallel processing (with all its benefits and obstacles).
In my opinion there is no need to run a FSM in a separate thread. The logic should be simple enough for a frame. Usually it does not need to be activated all the time.

I aggree with pathfinding. That can take some time an large graphs. With that I had the Idea, to make the processing interruptable. That you can process 1000 steps in one frame 1000 steps the next frame,200 the third frame. But this is just an idea.

@Captain Oblivion. lol i just googled ‘blender 2.5 pathfinding’ and the first hit was this thread :slight_smile: nice but not what i wanted. do you know where there is any documentation on the ge pathfinding implementation?

I would like to build a system that is very simple and at least functional but which can easily be upgraded and expanded upon. Modular and yet integral… :confused:

I suppose that in that case it would be much better to write it in C++ than python as being an interpreted language its ultimately slower no? I suppose that means a plugin or make a build with it included as you still cant script in C++, is this not a 2.5x dev proposal?

About the integrated pathfinding do you know if that is based on auto generated waypoints or waypoints at all?
Collision detection just based of the objects physical bounds? Don’t think it would be auto generated nav mesh that would be problematic unless coded by the god of code…well they seem to have got it working in unreal e3 :slight_smile:

You can use C and C++ in the BGE without having to build stuff into Blender. Just use the Python C API to compile the C code into a Python module and import that. This does, however mean that you’ll loose the portability of Python as you’ll need to compile the module for each platform. Also, I’d start first in Python as it’s quicker to develop with, and then switch to C/C++ only if you actually find performance problems.

its like one lonely neuron… :frowning:
@ andrew-101.
thanks for the links, i’ve had them bookmarked for quite a while :slight_smile: igda’s got good resources too. And thanks very much for that code, I’l have to have a closer look at it. I think fsm’s would lend themselves very nicely to a nodal editor that would be worth building in, if pathfinding is getting built in then there must be some kind of plans for further ai development:yes:

thanks for the info, knew there must be a way:) im not sure maybe with a little more python practice it will be easier than C++ but i like C++ it makes alot more sense to me… python seems to be all over the place, spose thats what you get with a high level versatile interpreted language, 20 ways to do any one thing :S

I suppose as long as we have linear processors all ai are fundamentally automata?

fsm can potentially make quite sophisticated decision making net i suppose but isnt it a bottleneck in a way for more dynamic datatypes.

i would like to use rbs and a standardized data type object io for the decision making and planning processes and knowledge representation for entities memory. This could give a lot of freedom to the entities decision making and be easily customised. Also with rbs you could easily add attributes to rules once the number/complexity of inputs demands it, or once you get around to it :slight_smile: Meaning complexity of behavioural routines could be easily increased if the game were to require but you could still use as simple a one as you would like.:yes:
Input objects having more attributes would mean very little conflict resolution was needed for hierarchies of plans to be created. And this standardized object type would mean better dynamic immediate action planning based on long and short term goals, memory and game-state data. Condensing game state data to conforming data bojects might be a tricky part:spin:
basically the framework could be about as simple as fsm’s but more easily expandable:confused: the hard part might be allways conforming to that datatype i guess that could take some work:eek:

even looking at simple fsm code in python seems detrimental to my health :S i think its good for fast scripting but id prefer to write anything complex in c++ anyway as you can manage it much more easily and have better fine control i think :confused:

if (WeakerThanEnemy())


\just thought that was funny :slight_smile: lol


I attempted to use threads for the behavior trees at one point, however they did not work well within blender and would remain after the bge had finished and any attempts to explicitly destroy them would not work.

A nodal editor for a FSM would be nice, I have thought about creating one before and haven’t ruled out creating one.

Well theres hope yet, this looks great:
i hope they can get it working.
Also this is worth looking at:
Also heres a tutorial on pathfinding havent gone into it yet but it would need to be converted fo 2,5:

You can grab the recast and detour 2.5 build from this thread:

Thanks for the link! I got side tracked and didnt read through all that… didnt know there was a build available yet, this will be fun to look at.
im currently not sure about the best actual approach to take on this but ill keep working on finding a system that will work well, and doing more research on possible methods.
my biggest concern from a very general perspective, is game state data representation integration into the decision/planning processes, but this is still all just theory, and ill have to work on a very simple system. I still think a type of rule based system that allows knowledge representation objects to carry as many attributes as you would like and a very hierarchical planning system would be good.