• 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 adding increasing value to a variable per instance created

R

Remix The Idiot

Guest
I need help, I wanna have an object get a variable that has increasing value for every object in the room.
for example I want my object called obj_object with his variable called "id", so there is 3 of them in a room, the first obj_object will have 1 as its "id" variable and the 2nd will have 2 and the 3rd a 3.

At first I thought it was simple, just put a create code that says this: "obj_object.id+1" but all it did was make the first of that object have 1 and everything else a 2. I know why this is what happens when I do it, but I just don't know the correct fix as there may be a function or something that I don't know yet that can easily maybe fix this.

I'm new to GML and has only had a little experience with coding in general so far, and helping me with this would be greatly appreciated for a novice.

Thanks in advance!
 

Simon Gust

Member
in every obj_object's create event you can put
Code:
variable = instance_number(object_index);
don't use "id" as a variable though, that name is already taken by the instance's real id.
 
R

Remix The Idiot

Guest
It makes all obj_objects return the same number though. I want all the different instances to each have different values in the variable.
maybe I'm missing something or being stupid. (side note, i knew? naming a variable "id" was bad, i just had to think of something and id was the first thing that came to my mind)
 

Simon Gust

Member
It makes all obj_objects return the same number though. I want all the different instances to each have different values in the variable.
maybe I'm missing something or being stupid. (side note, i knew? naming a variable "id" was bad, i just had to think of something and id was the first thing that came to my mind)
So that means all objects are already placed in the room. Try making a global variable.
object controller
Code:
global.counter = 0;
obj_object
Code:
variable = global.counter++;
 
R

Remix The Idiot

Guest
It worked! And now I know how increments and decrements work, for some reason the manual explained it weirdly for me.
Thanks.
 
R

Riley Nassour

Guest
So that means all objects are already placed in the room. Try making a global variable.
object controller
Code:
global.counter = 0;
obj_object
Code:
variable = global.counter++;
I have the same problem but I am using drag and drop, can you explain how to do this in drag and drop?, Thanks in advance.
 
Top