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

target instance

Tales

Member
Hello

I search a function or a way to target an object but I have 2 objects : obj_A. The obj_A have an "instance_create(x+20,y,obj_B)" a lot of times and the obj_B have a {if obj_A.x<x x-=1}. But the obj_B dont target the goob object..

How to tell to the obj_B which obj_A have created him? Its important to tell the obj_A move a lot and there is possibility that both obj_A create obj_B at same time.
 
Last edited:

Arvel

Member
You have to understand the difference between instance and object.

GML:
var something = instance_create.....
something will contain the ID of the instance object you just created with instance_create_* functions.

This way, you can do
GML:
something.x += spd;
 

Tales

Member
HO ok I see. Is there a way to create those something I need about 200 "something".. like something1 something2 etc until something 200 but without writte all.

EDIT ill try with a "string" function.

Thanks
 

Arvel

Member
HO ok I see. Is there a way to create those something I need about 200 "something".. like something1 something2 etc until something 200 but without writte all.
Better if you use arrays.
GML:
something[0] = instance_create..
something[1] = instance_create..
 

Tales

Member
I dont understand the "out of range error" :/ I never used array, I search but this dont works :s

Code:
if nbparticule<200
{
     nameparticule[nbparticule] = instance_create(x+irandom_range(-40,40),y+irandom_range(-40,40),obj_particlebads01);
     nbparticule+=1;
}

     if x>nameparticule[nbparticule].x nameparticule[nbparticule].x+=2
     if x<nameparticule[nbparticule].x nameparticule[nbparticule].x-=2
     if y>nameparticule[nbparticule].y nameparticule[nbparticule].y+=2
     if y<nameparticule[nbparticule].y nameparticule[nbparticule].y-=2
 

Tales

Member
Thats why I precisely describe my problem, I konw instance Id etc exists, but how to tell to the obj_B the number of instance of obj_A created him if at the same time there is another obj_A creating other objet_B.
 

Nidoking

Member
Well, if you change the index in the array after storing something, it's not pointing to the thing you just stored. The steps run in order. Top to bottom. The problem has been answered - you're just not implementing the solution properly.
 

Tales

Member
I guess I understand a little, this code is a little better, but not really what I need.

GML:
if nbparticule<200
{
     nameparticule[nbparticule] = instance_create(x+irandom_range(-100,100),y+irandom_range(-100,100),obj_B);
    
     if x>nameparticule[nbparticule].x nameparticule[nbparticule].hspeed+=2
     if x<nameparticule[nbparticule].x nameparticule[nbparticule].hspeed-=2
     if y>nameparticule[nbparticule].y nameparticule[nbparticule].vspeed+=2
     if y<nameparticule[nbparticule].y nameparticule[nbparticule].vspeed-=2
    
     nbparticule+=1;
}
 
Last edited:

Tales

Member
but this code is on the obj_A and I guess, it be better if its on the obj_B..

I mean I really need "nameparticule[nbparticule].x+=2" because with hspeed+=2 particle go everywhere... BUT the nameparticule[nbparticule].x+=2 only works one time..
 
Last edited:

Tales

Member
for a better comprehension of my problem, imagine a lot of obj_A (a simple square) each obj_A is moving and create a lot à of obj_B(little circle) around of obj_A and all obj_B go in the middle of the obj_A that created him....
but obj_B are moving during other obj_B are createdand obj_A is moving that why hspeed cant works in my example and x+ is working one time because I'm out of the loop then.
 

Nidoking

Member
Why does obj_B not have its own movement code that puts it where you want based on the obj_A id that you've stored in a variable?
 

FrostyCat

Redemption Seeker
This has gone on enough. Once again, you must learn the difference between an object and an instance.

If a dog should follow its owner, you don't teach the dog to just "follow human", you teach the dog who its owner is and follow that exact instance of a human. "Follow human" is an absurd instruction if applied in a dog park where there will be multiple dogs and multiple owners.

The article shows exactly what should be done in your setup:
Subsidiary instance(s). Same as created or copied instance. If the subsidiary instance needs to reference its creator, the creator should assign its instance ID to an instance variable in the subsidiary instance. Conversely, if the creator needs to reference its subsidiary (or subsidiaries), it must store the return value of instance_create() (GMS 1.x and legacy) / instance_create_layer() or instance_create_depth() (GMS 2.x) immediately. NEVER use instance_nearest() to establish or maintain this relationship --- being the closest does NOT imply being the most relevant, especially in close quarters.
obj_A Create:
GML:
for (var i = 0; i < 200; ++i) {
    var inst_B = instance_create(x+irandom_range(-100,100), y+irandom_range(-100,100), obj_B);
    inst_B.owner = id;
    my_Bs[i] = inst_B;
}
obj_A Destroy:
GML:
for (var i = 0; i < 200; ++i) {
    with (my_Bs[i]) {
        instance_destroy();
    }
}
obj_B Create:
GML:
owner = noone;
obj_B Step:
GML:
if (instance_exists(owner)) {
    if (x != owner.x) {
        hspeed += sign(owner.x-x)*2;
    }
    if (y != owner.y) {
        vspeed += sign(owner.y-y)*2;
    }
}
 

Tales

Member
it was my first question : I dont know how to tell to obj_B which obj_A created him.
Its important to tell the obj_A move a lot and there is possibility that a lot of obj_A create a lot of obj_B at same time.
 
Last edited:
it was my first question : I dont know how to tell to obj_B which obj_A created him.
Its important to tell the obj_A move a lot and there is possibility that a lot of obj_A create a lot of obj_B at same time.
for (var i = 0; i < 200; ++i) { var inst_B = instance_create(x+irandom_range(-100,100), y+irandom_range(-100,100), obj_B); inst_B.owner = id; my_Bs = inst_B; }
Frosty literally answered exactly that question (as well as all your others). Here, each instance of obj_b gets given a variable called owner which holds the instance ID for whichever instance of obj_a created that obj_b.
 

Tales

Member
I didnt saw his answer..........

ho ok I see! Good logic I understand thanks I gonna try!

I only dont understand why this destroy?
for (var i = 0; i < 200; ++i) {
with (my_Bs) {
instance_destroy();
}
}
this destroy all obj_B from obj_A? the destroy moment is when th obj_B touch his obj_A.
 
Last edited:

Alexx

Member
To clarify, every instance in the room has it's own unique id.
Whether you have 5 or 100 of the same object in a room, each has own unique id.

By this logic, when you create an instance, you can "send through" this unique id of the creator as a variable.
Doing this, the created instance knows the creator's id.
 

Tales

Member
Yeah I understand better, "By this logic, when you create an instance, you can "send through" this unique id of the creator as a variable. " very clear here... and so simple..
THANKS TO ALL, for all explanation, sorry for my questions, sometime I think I understand, but in fact no... Its not easy to all understand in eng ^^ Thank you all.
 
Top