• 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!

Legacy GM [SOLVED] Assigning code to instances of an object

W

Wuzzems

Guest
Create Event:

Code:
// pink block position
pinkposition = room_width - (room_width * 0.992);


// y position of current object instance
var yco = ((room_height/2)*1.5);

var ycoor = (room_height - yco)/2;

yposition = (room_height - ycoor);


// setting instance position
x = x;

y = yposition;


// setting distance
distance = point_distance(x, y, pinkposition, yposition);

// setting time
time = 240;
time2 = 480;

// setting speed
objectspeed = distance/time;

//outside room variable
outside = 0;
Step Event:

Code:
distance = point_distance(x, y, pinkposition, yposition);

if distance > objectspeed
{
move_towards_point(pinkposition,yposition,objectspeed);
}
else
{
x = x;
speed = 0;
outside = 1;
}

if (outside == 1)

{
move_towards_point(pinkposition - 2500, y, objectspeed);
}
I have all of this code on one object, but as you can see I have two "time" variables. Only one "time" variable is being used at the moment and it's being used by all the instances of this object. I would like to assign basically all of this same code on different instances of the same object and just add more "time" variables to assign to all the different instances.

Any help is appreciated.
 
H

Homunculus

Guest
The simplest way to do this is to have just one "time" variable, but set it on a per instance basis when they are created.

If instances are created by code:
Code:
var inst = instance_create(x,y,obj);
inst.time = 200; //or whatever
inst.objectspeed = distance/time;
If instances are added from room editor:
Right click on an instance > creation code, and write:
Code:
time = 200; //or whatever
objectspeed = distance/time;
OR you could create a script that creates instances for you by setting the provided amount of time, called like scr_create_obj(x,y,420)
Code:
///scr_create_obj(x,y,time);
var inst = instance_create(argument0,argument1,obj);
inst.time = argument2;
inst.objectspeed = distance/argument2;
return inst;
 
Last edited by a moderator:
W

Wuzzems

Guest
Oh cool, I didn't know instances had a creation code area. Also, they were made within the room editor. It works but I'm not exactly sure why it works. Writing it like that feels like it's just going to overwrite the time variable and stick to one value instead of each instance having it's own value.

Also what's the significance of writing objectspeed = distance/time within the instance creation code?
 
H

Homunculus

Guest
If you are assigning a specific time value in the creation code for every instance you placed into the room, I don't get how you can fail to see that each one keeps its own specific value to be honest...

About overwriting, you are actually right, and It has to do with the order of the events being executed at the instance creation. The create event is executed BEFORE the creation code you add inside the room. This means that every instance runs time = 240; and objectspeed = distance/time; , with the same values, but then you overwrite it in the room creation code. You can see the create event as setting the defaults, and room creation code as setting (overwriting) the individual values.
 
W

Wuzzems

Guest
Oh okay. So if there was no "time" variable created within the object's create event, then the instances creation code won't work since the "time" variable wasn't created since the instances aren't creating it.(wow, try saying that 10 times.) I just saw it as every consecutive instance's creation code would overwrite the "time" variable for the other instances as well. For instance the final instance that's created would have a time variable of 600 and it would overwrite the time variable of all the other instances as well, good to know that they each keep their own.

Just another question out of curiosity. If I wanted to change the "time" variable for one of these instances from within the object's code, I imagine that I would have to take the instance's ID and write something like inst_F199F9F6.time = 500; right?
 
Top