• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GMS 2 [SOLVED] Follow an object with his ID.

Fleoh

Member
Hello guys ! I'm trying to make an object follow an other object thanks to the ids.
In fact, the first instance needs to follow the player, the second instance need to follow the first instance, the third the second [...]

So i've tried this code :


In create event of the object which needs to follow
Code:
if bot_number = 0        // Check if there is no object to follow except the player
{
    follow = obj_player; // Follow the player
    bot_range = id;      // Take the id of the next object to follow
}
else // If there's an object to follow
{
    follow = bot_range; // Follow the id of the last created object
    bot_range = id;     // Take the id of the next object to follow
}
In step event of the object which needs to follow
Code:
direction = point_direction(x,y,follow.x,follow.y) 

if distance_to_object(follow) > 16 {
// My code for moving...
}
Unfortunately, it didn't work. I don't know if it's come from the use of IDs or...
All the instances created are following the player and not the saved ID.
I've tried the same with "self" but it didn't work and when I watch the var follow, it always return 0..
Thanks for your help !
 
Last edited:

Mercerenies

Member
bot_range and bot_number are instance variables, so every instance you make has different variables. If you want to share variables between instances, you can either use "global.bot_range" or (more recommended) have some sort of controller object that manages all of them and assigns them targets. Additionally, when you write 'if bot_number = 0', you're actually assigning bot_number the value zero (single equals sign is always assignment) and then checking if it's nonzero (which, naturally, is never true). You want == for comparison.
 

Fleoh

Member
Thanks for your fast answer !
Yep I know this, and this is why I make it like this U_u, every instance need to follow a different object.
And thanks for the "==" tips.
Like I've said, the first object need to follow the player, the second object need to follow the first object and the third one need to follow the second...

Sorry if my explainations aren't good u_u

In fact, I could make it from differents ways, I just want to know why I can't to this with id or self.
 

Attachments

Mercerenies

Member
I agree that follow should be an instance variable; each instance absolutely wants its own follow variable. But you seem to be treating bot_range like it's shared between instances. When you make a new instance of this object, bot_range is undefined for that instance. It doesn't matter if you set it to id in the previous instance, because that was setting a different, unrelated variable. When you make a new instance, you get a new set of instance variables. You can't rely on the newly-made objects instance variables to get shared information between objects.
 

Rob

Member
Thanks for your fast answer !
Yep I know this, and this is why I make it like this U_u, every instance need to follow a different object.
And thanks for the "==" tips.
Like I've said, the first object need to follow the player, the second object need to follow the first object and the third one need to follow the second...

Sorry if my explainations aren't good u_u

In fact, I could make it from differents ways, I just want to know why I can't to this with id or self.
Your idea is right but your implementation is wrong.

Code:
//Inside player create event

var total_bots = 3;
follow = id;

for (var i = 0; i < total_bots; i ++){
   bot = instance_create_depth(x, y, depth, obj_bot);
   bot.follow = follow;

   follow = bot;
}
Code:
//Step event of bots
direction = point_direction(x,y,follow.x,follow.y)

if distance_to_object(follow) > 16 {
// My code for moving...
}
By creating all of the bots in the player you can easily use and reuse the player object's variable "follow" to set the follow id's of the bots. I don't know if you understood what Mercenaries was telling you about your code but from what you showed, it doesn't seem like the instances would be able to read the variables of the other instances.
 
Last edited:

Fleoh

Member
@Rob Thanks a lot, it works perfectly !

@Mercerenies Nope, bot_range wasn't created for being shared between instances, but I admit that it's a bad choice of variable name ^^ Thanks for your help too !
 
Top