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

Determine child object, and adressing child-parent

Q

qyuodash

Guest
[SOLVED]
Good day, comrades! In this Video only one plane has the shadow.

I want every plane has its own shadow (obj_shadow).

1) How can i determine that Certain Shadow is a child of a Certain Plane ?
2) How can i check Child's variable from Parent object and Parents variable from Child object.

Thank you for attention.
 
Last edited by a moderator:

Hyomoto

Member
The simplest answer is to tell that child when it is created who it's parent is. The parent should also know who it's children are. In this case it's probably also worthwhile to have the parent handle drawing it's children. Just set the children visible to false and have the parent iterate through its children and draw them itself.

Whenever you create an instance, the function returns the ID. If this is called by the parent it is easy to just assign that variable to something, and then you can access it easily using child.variable syntax:
Code:
shadow.x = x
Just as a simple example.
 
A

anomalous

Guest
I agree with Hyomoto in that this doesn't really sound like parent/child but "which shadow belongs to with plane". In which case yes, when the plane creates its own shadow (or when its created), it should record the id it created, and pass its id to the child, so they know each other.

If you want a function to determine parents, that's object_is_ancestor().
 
Q

qyuodash

Guest
The simplest answer is to tell that child when it is created who it's parent is. The parent should also know who it's children are. In this case it's probably also worthwhile to have the parent handle drawing it's children. Just set the children visible to false and have the parent iterate through its children and draw them itself.

Whenever you create an instance, the function returns the ID. If this is called by the parent it is easy to just assign that variable to something, and then you can access it easily using child.variable syntax:
Code:
shadow.x = x
Just as a simple example.
So what should i write in Creation code of the Plane to make the Shadow - Its Child ?
Now code is : instance_create (x,y+400,obj_plane_shadow);

and what should i write in child code to check parents variables?
 

Hyomoto

Member
Alright, let's learn a bit! First off, what we need is some way for the two objects to communicate. Honestly, this is often the hardest thing for new users to wrap their head around, so hopefully I can shed some light on that. Right now you have a shadow, and you want it to be able to communicate with it's parent, the plane. The thing is, the only way you can really do that is to search for its parent. Even then, if there were more than 1 shadow, or more than 1 planes, how would each shadow know which plane was it's parent? How would each plane know which shadow was its child? The answer is simple: we tell them.
Code:
shadow = instance_create( x, y + 400, obj_plane_shadow )
In this case, wee call this bit of code from the create event for the plane. You should be familiar with the varA = varB syntax if you've gotten this far so I'm not going to bother explaining that, what is important is that the instance_create function is returning the ID of the shadow you just created. All we have to do is 'capture' that by assigning it to a variable, the imaginatively named 'shadow'. Now that is done there are a lot of things we can accomplish. For example, how can you tell the shadow which plane is it's parent? We tell it. Now that we know who the child is, the plane can tell it whatever it wants.
Code:
shadow.parent = id
The section of the manual, Addressing Variables in Other Instances, is the section you need to read. It should give you a better understanding of how and why this works. Instead, let's talk about why the child shadow probably doesn't need to know who its parent plane is. Generally, especially if you are just learning, you have no idea what order instances update in. Sure, you should know they'll update during the step event if you put code there, but how do you know which code runs first? The plane or the shadow? For example, if the shadow sets its position, but updates before the plane, its position will be set for this step. When the plane processes, it may move because of user input or other actions. Since the shadow already updated, it won't change position again until it updates next step. This means the shadow will not follow the plane smoothly. To fix that you just need to make sure the shadow is always updated after the plane, and the easiest way to do that is to have the plane update it. Since the shadow is, weakly speaking, a part of the plane, we can just have the plane do all the work.
Code:
shadow.x = x
One last thing, you also need to make sure parents delete their children! If the parent plane is destroyed, you should make sure it also does:
Code:
instance_destroy( shadow )
During it's destroy event, otherwise you'll have a shadow with no plane!

Hope that helps and good luck!
 
Last edited:
Q

qyuodash

Guest
Alright, let's learn a bit!
Thank you very much!
With your help i came up to this working code:

// Shadow
child = instance_create (x, y+300,obj_plane_shadow);
child.parent = self;
 

Hyomoto

Member
Looks good except that self is not the id of the plane, and isn't a 'global' variable. If you were to
Code:
show_debug_message( id );
show_debug_message( self )
You'll see the two do not match. When you want to set the id for the parent, you should just use 'id'.
 
Top