I'm trying to figure out the best way to set up the AI behaviors for the NPC:s in my game, but I keep running into problems because the different NPC instances gets mixed up. So, for example, an enemy designed to just move back and forth on the ground like a Goomba may start spontaneously flying when a flying enemy is spawned.
As it is now, I have set up the AI events in the following way:
1) Once an NPC is created, it's given a unique ID value, and one of its alterable strings named "Main AI" is set to the name of a specific AI routine, for example "Ground Pacing".
2) On every frame, a ForEach loop called "AI" is run on the NPC:s. If the NPC:s "Main AI" is different than "", a function with the name of the AI behavior is triggered (so, in this example, "Ground Pacing".)
3) Here's where the problems with my method start, because with each event in the AI function, I have to limit the scope and specify which NPC instance should be affected, by refering to it's ID value. Each NPC instance also have a sprite and a PMO object assigned to them, so I need to limit the scope on those objects as well on every event that affects them. If I forget to limit the scope on just one single line, it will result in bugs like the unwanted flying enemy.
4) The first time the Main AI Function is triggered, a sub-function called "AI Setup" is triggered, which sets up the initial values of the AI main behavior, and assigns a sub-behavior. Then, another sub-function is triggered, named from the main behavior and sub-behavior, separated by a "/". For example: Ground Pacing/Walking Left.
5) In the sub-behavior function, movements and actions of the NPC are triggered, and checks are made to see if the conditions are met for changing to another sub-behavior.
So, my problem is: how do I limit the scope for the affected objects, without having to do it again and again, in every single event of every AI behavior I program? Is it possible in any way to, say, limit the scope for the NPC object, sprite and PMO in the first event of the Main AI function ("Ground Pacing"), so that it keeps the scope in the following events and sub-functions?
Or, does anybody ave any other suggestions on how I can set up the AI behaviors differently to avoid the problem?
I'm dead tired of the constant bug testing and having to copy/paste the "ID of 'NPC' = Current NPC ID" event all over the AI code.