• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Change name of instance using code?

I am currently make a top down GTA 2 style game and am currently making the cars. I had made a piece of code, I called the vehicle spawn enumerator, to generate a special 5-digit PIN for the vehicle so it can be identified by other objects I will add later in the game. Since the cars are the same object, they all have the same name.

This is to avoid, for example, if a pedestrian leaves their car, and wishes to return to their vehicle, they wont just go to the nearest object with the name obj_car, they will instead go to the nearest car with the name obj_car(and the PIN the their car has). problem being, I don't know if I can change the instance name with code on the fly without using creation code. (The spawning system will spawn the same object every time, so I need the enumerator to identify each instance of the object)

If this is impossible, is it possible for the ped (or whatever other object such as the police AI) to check the PIN variable in every car, until it finds its car and can go to it?
 

FrostyCat

Redemption Seeker
You need to learn the difference between an object and an instance.

For the same reason "Human" is not your name, obj_car is NOT the name of your car instances. It's the name of their type, not their individual name. They each have their own numeric instance IDs, which are returned by things such as instance_nearest() or instance_place() at runtime, or by instance_create_layer() when you create them dynamically, or by the label you assign to them in the room editor when you create them from the IDE.

In addition, when multiple instances of an object exist, referring to a specific instance using the object ID is illegal. If you were talking to Alice and Bob and you wanted to know where they live, asking for "human's home address" would be absurd. Instead you'd ask for Alice's home address or Bob's home address.

Here's a rough example of what you should do:
Code:
var nearest_car = instance_nearest(x, y, obj_car);
if (nearest_car.pin == "12345") {
  // Do something using nearest_car, NOT obj_car
}
Any other code that mentions obj_car.variable or the like should instead find a way to get the instance ID they want, then reference the variable off that.
 

marasovec

Member
Not sure if this is what you need but you can generate the 5-digit number in the creation code using something like
Code:
pin = 10000+instance_number(obj_car);
and then just loop through all the car objects to find the car you need
Code:
for(var i = 0; i < instance_number(obj_car)-1; i++)
    {
    var car = instance_find(obj_car, i);
    if car.pin == 10005
        {
        // this is my car
        }
    }
 
You need to learn the difference between an object and an instance.

For the same reason "Human" is not your name, obj_car is NOT the name of your car instances. It's the name of their type, not their individual name. They each have their own numeric instance IDs, which are returned by things such as instance_nearest() or instance_place() at runtime, or by instance_create_layer() when you create them dynamically, or by the label you assign to them in the room editor when you create them from the IDE.

In addition, when multiple instances of an object exist, referring to a specific instance using the object ID is illegal. If you were talking to Alice and Bob and you wanted to know where they live, asking for "human's home address" would be absurd. Instead you'd ask for Alice's home address or Bob's home address.

Here's a rough example of what you should do:
Code:
var nearest_car = instance_nearest(x, y, obj_car);
if (nearest_car.pin == "12345") {
  // Do something using nearest_car, NOT obj_car
}
Any other code that mentions obj_car.variable or the like should instead find a way to get the instance ID they want, then reference the variable off that.
I never noticed that. I'm still new to GML.

So, I'm working on the system to enter and exit the car. I changed the nearest_car variable to enterCar, since the nearest car is the car to enter. The way I want this system to work, is the player will be put into a "driving vehicle state", where the player's controls are disabled, and the player is being constantly teleported to the car's X and Y coordinates. The car will use a collision circle to know that the player is in the car, and will change to a "controlled by player state", which allows for it to be controlled by the player, while not effecting other instances of the car object.

My new question is how do I get the coordinates of only that instance of the car? I have a strong feeling it's obvious but maybe not...
 
enterCar.x
enterCar.y
I tried this
Code:
if(keyboard_check(ord('F'))){
        enterCar = instance_nearest( x , y , obj_car);
        xPos = enterCar.x;
        yPos = enterCar.y;
        move_towards_point( xPos , yPos , 50 );
    }
But the player is still going to the coordinates (0,0) at the top left of the room. I tried it without defining the variables xPos and yPos but it still doesn't work...
 
Top