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

[SOLVED] Multiple instances of the same object, how to make them run their own code

I'm setting up something where, when a drawn, moving sprite reaches the edge of a certain object, it triggers something. The object "obj_fire" is a child of "obj_lightparent".

There's a separate instance that draws the moving sprite and tells obj_lightparent what to do once the sprite reaches that object's edge.

Code:
with (obj_lightparent){

global.sx = (x - (sprite_get_width(spr_light)/2*image_xscale)); //start of sprite

.................other code that is irrelevant
}
This works fine if there's only one object in the room with the parent: obj_lightparent.
However, if I have 2 or more of the exact same object in the room with that parent (i.e. two torches), there's an issue where the moment the drawn sprite touches the edge of the the first object, it also triggers the other object, which is NOT what I want.

I want each object to run the code independently and not be triggered by other instances of that object meeting the drawn sprite.

How would I do that? I tried using id, other, etc. with no luck. It seems the x in the above code is what needs to be changed.
 

Joe Ellis

Member
If you use with, with an object, it will loop through all instances of that object, if the object is a parent of other objects, it will also loop through those child objects. So I think you need to revise the method. I'd start with initiating\detecting the situation your talking about and get the id(s) of the instances involved
 
If you use with, with an object, it will loop through all instances of that object, if the object is a parent of other objects, it will also loop through those child objects. So I think you need to revise the method. I'd start with initiating\detecting the situation your talking about and get the id(s) of the instances involved
The whole point of using a with statement and a parent object is so that I'm not writing chunks of code for each individual light source. The only light source I have is a fire, but I have multiple instances of that fire. I understand I would need to check for their id, but I don't want to have to write code for each possible id for each room that they're in cause that can get tedious.

Is there some way I could assign a variable to whatever child is running the code and then use that variable to determine things?
 

Joe Ellis

Member
AH, I just realized what the problem is, basically the instance that draws the moving sprite and tells obj_lightparent what to do once the sprite reaches that object's edge.. You can tell obj_lightparent what to do, but it won't work with a with statement, cus it would loop through all children etc.
What you need is the instance id of obj_lightparent, so if there is only one of those in the room, simply do: with obj_lightparent.id instead :D
Or you could in the obj_lightparent's create event, set global.lightparent = id. The result is the same, but it's "considered" good practice to not use object.variable, but neither way makes a difference if there's only one instance of an object
 
Last edited:
AH, I just realized what the problem is, basically the instance that draws the moving sprite and tells obj_lightparent what to do once the sprite reaches that object's edge.. You can tell obj_lightparent what to do, but it won't work with a with statement, cus it would loop through all children etc.
What you need is the instance id of obj_lightparent, so if there is only one of those in the room, simply do: with obj_lightparent.id instead :D
I was restructuring some code to fix a completely different bug (which is being addressed in my other topic), and somehow that actually fixed the issue... not sure why, though. I'm still using the with statement and the parent, and I haven't referenced an id or anything... weird.

Code:
    if instance_exists(obj_lightparent){
    with(obj_lightparent){ //i.e. the "source of light"

    radius = (sprite_get_width(spr_light)/2); //distance between edge of light and x value of the source of light
    height = (sprite_get_height(spr_light)/2);//distance between edge of light and y value of the source of light
    is_left = x - radius; //left side of sprite
    is_right = x + radius; //right side of sprite
    darkness_start = obj_lighting_cycle.end_x; //start of darkness
    is_dist = darkness_start - is_left; // distance between start of darkness and left side of sprite
   
    if (obj_lighting_cycle.day_timer == 20){ //sun starting to set
   
        if is_dist > 0 { //if darkness has reached the light
    draw_sprite_part_ext(spr_light,0,0,0,is_dist,sprite_get_height(spr_light),x,y,image_xscale + .2,image_yscale + .2,c_orange,.8); //OFFSET PROBLEM
    }
    }
    if global.night && obj_lighting_cycle.day_timer != 6{ //for after the sun has finished setting
        draw_sprite_ext(spr_light,0, x, y, image_xscale + .2,image_yscale + .2,0,c_orange,.8);  //WORKS!!
    }
EDIT: I think part of the issue might have been the use of global variables... but not sure.
 
Top