Several instances of a same patrolling enemy

Hi there! I’m a long time reader but, possibly for the first time, I’m facing a problem I can’t find a solution to in the posts, so here it is:

I’d like to have two instances of a single guard object move independently from each other. Since my guard has a pretty long and complicated program, I’d rather avoid having to copy/paste/edit it for each new guard I want to add.

I tried using the Linked Objects extension (and the ForEach event) by following the tutorial, but all I could achieve was to make one guard move while the other stays still. I tried different combinations with several copies of the same object in groups, but so far nothing has worked.

Using a forEach event is the solution. It’s hard to tell what is wrong with what you did, but it’s the best way to have independant instances of an object acting like different AI.

We’ll need to know more about the kind of game you make, what your code is actually doing on one instance, and what you want the instances to do. If the for each don’t work as you want, it’s probably because of hard coded event (force, angle, etc.) in your project.

Thanks for the reply!

I have two parts for the guard’s program, one that controls the idle behavior (patrolling) and one that controls it’s reactions to the player (various states of alerts).

I’ll start with just the patrol as it is the simpler part, I’ll add the alert later (anagrams, yay!) if needed:

My latest attempt was to put a ForEach event at the top, and to put two guards named Guard1 and Guard2 into a “Guard” group – so that the code stayed the same. I also grouped a DetectionR1 and DetectionR2 into “DetectionR”, a “GoLeft1” and “GoLeft2” into “GoLeft”, etc.
Then I linked everything like this:


[Edit: Just realised I didn’t keep the ForEach. All the conditions in 12 (second image) were originally conditions of the ForEach event ]

When I preview the game, the guards just stay still : /

I’m not about what you are trying to do on the second picture, and the linked object… But, for your first problem (picture), i can tell this : the problem are variable.

You are use scene variables to control the alert levels, so all guards share the same value : if one make the value change, it will impact the other ! Change your variable to object variables, so each instance of your guard will have his own variables and value. Also using a timer like you do will cause problem, or least something unatural : all guard will have the same timer shared (like for scene variable!), so it’s not the best option : You should try to make individual timers for all object, with object variables and TimeDelta() value, to allow each guard to have a personal timer.

Darn it, I feel so stupid now! I modified variables when I tried to duplicate the guards by copying/pasting the codes, but it didn’t occur to me that I needed to do it again when using the ForEach event.

The detection cones are still problematic, but changing the scene variables to object variables (and removing the links, for some reason) fixed the movement problem. I can’t thank you enough!