# Squads, I fail to find an elegant solution...

This topic is 2879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

entity->getActionManager()->addAction(new move_action(coordinates(x,y)));

The actions step functions are then called by the units step function... At the moment im working on a "flock" of small spacecraft. They have a "idle_move" action that makes them just move around slowly at their momentary position. Now I wanted to implement a flock_move action but am unable to simultaneously "terminate" the orders of the whole squad when the action is completed. The problem is, I am not issuing one movement command to the whole squad, I just give the identical order to every member of the squad. But those actions are basically independent of each other, which makes them hard to coordinate. Now im not sure how to solve that problem? I could just stick with my version and try to find a condition that can be evaluated by every squad member and gives the same result. Or I could add some sort of communication system between the actions. Another possibility would be to move the action management up to the squad object and have a step function of the squad iterate through it's members. The problem there is, that there is no obvious way to iterate through the squads. So maybe the actions should exist only on the level of the squad object, but be called from the individual unit? like in:
void Entity::step()
{
}

Did I miss some clever way to do this or is my squad/action system just a bad idea in general? should I implement squads different?

##### Share on other sites
A squad is a single entity in itself with its own associated logic. The squad object purpose is to group and coordinate behavior of the agents it controls. The easiest thing is to create a squad object which basically keeps this meta-data appropriate for the squad level within itself, preventing data leaking between squad and agent.

So something like this Squad->(owns N agents)->emitCommand(moveTo)

Squad internally has a moveTo point which it checks periodically when all agents are within bounds, then it considers the squad moved to that spot.

Good Luck!

-ddn

##### Share on other sites
Quote:
 Original post by ddn3So something like this Squad->(owns N agents)->emitCommand(moveTo)Squad internally has a moveTo point which it checks periodically when all agents are within bounds, then it considers the squad moved to that spot.

Well, that is sort of the problem. At the moment there is no list<Squad> that allows me to somehow regularily update data of the squad or have it check the status of its members. The squads can only be accessed via the entities.
By using a loop that iterates through the entities and calls a function for each squad it finds, i am going to call that function multiple times... once for each of its members.

So i guess i should have that list<Squad>, but i am sort of allergic to having multiple independet datastructures contain pointers to the same elements.

So how would you implement squads in the first place?

##### Share on other sites
Quote:
 Original post by japroSo i guess i should have that list, but i am sort of allergic to having multiple independet datastructures contain pointers to the same elements.

But in this case, multiple pointers are the elegant solution. If both of those independent data structures need to know about the same elements, then letting them both have pointers to them is the most natural thing in the world.

##### Share on other sites
Quote:
 Original post by theOcelotBut in this case, multiple pointers are the elegant solution. If both of those independent data structures need to know about the same elements, then letting them both have pointers to them is the most natural thing in the world.

Hmm, ok. But that means i have to actually store "observers" and not pointers, right? Otherwise I run into all sorts of problems when an object gets deleted... Or should I make the second structure sort of "self organizing", so that squads add and delete themselves from the "squadlist"?

##### Share on other sites
You could do that, or just make sure that everything that contains pointers to entities are deleted before the entities themselves, or use smart pointers, which take care of only deleting things when nothing else points to them.

Or, you could do some system where objects remain for a while in a dead-but-not-deleted state, so stuff can check their entities' status and get rid of dead references. It would probably only have to be one cycle, just long enough for all interested parties to get the message.

There are lots of ways to do this. Which one you pick is probably dependent on what your architecture already looks like.