Chaotic Soul Gaming

Clustered Entities

May 7th, 2011

Well, I kind of wanted to write this article a lot sooner than this, but I wanted to have a working sample in Game Maker before anything else, and it took me a bit longer than I expected it to take since I’m still learning GMP as I go.

I mentioned clustered entities in my Dynasty Warriors 7 review and wanted to give some basics about what I meant by them.

What I mean by clustered entities are sprites or objects being controlled as a group by a single other object.  This is a kind of basic AI to not have to calculate or update the route or movements of each individual objects/sprites.

The concept is like this, use one object as a leader, and the rest as followers, all followers have a link to their leader, and these followers can also be leader as well.

The way I did this in Game Maker is having a leader create its own followers like this:


follower = instance_create(self.x - argument0,self.y-argument1,Follower_Obj)
follower.leader = self.id
follower.offsetx = argument0
follower.offsety = argument1

This is a script called on the Leader Create Instance event, it takes an X/Y offset as it’s 1st and 2nd argument.  What this code does is create an instance of “Follower_Obj” and keep it’s id, then it sets its “leader” attribute as it’s own id “self.id”.  It also gives it the offset attributes as well.

Then, on the Follower Object, you can attach another script on the Step Event which in the sample I called “DroneAI”:

if(instance_exists(self.leader))
{
    image_angle=self.leader.direction-90
    direction = self.leader.direction
    speed = 5
}
else
{
    move_towards_point(mouse_x, mouse_y, 5)
    image_angle=direction-90
}

This is just a simple AI implementation, which will make the object follow the direction of it’s leader as long as it exists. If the leader gets removed, it will start following the mouse cursor.

From there you can do cascading clustered entities,
like this:
Commander -> Squad Leaders -> Followers, using the DroneAI script on the Squad Leaders and Followers means that everyone will follow the Commander, if the commander dies, then Squad leaders will zone on the mouse cursor while the Followers will follow their Squad leader, if this one dies, then the followers will zone on the mouse.

If anyone want to try it out, you can download the sample following this link:
ClusterEntity.zip

One Response to “Clustered Entities”

  1. Zantagor

    test

Leave a Reply

Proudly powered by WordPress. Theme developed with WordPress Theme Generator.
Copyright © Chaotic Soul Gaming. All rights reserved.