Child sprite
by Isaac Barbosa · in Torque Game Builder · 03/18/2007 (11:04 am) · 7 replies
GetMountedParent(); I know how to use this to get the parent sprite if another sprite is mounted.
But I can't use or figure a method to get the child sprite from the parent sprite in a mounting case.
I see getMountOwned() method, but I simply can't use it. Some ideas?
Thanks
But I can't use or figure a method to get the child sprite from the parent sprite in a mounting case.
I see getMountOwned() method, but I simply can't use it. Some ideas?
Quote:Gets the owned by mount flag of the object if it is mounted.
Thanks
#2
03/18/2007 (12:26 pm)
Thanks Gary, I will try that :)
#3
I have tried -unsuccessfully- your approach placing this code into my sprites creation function:
so I try to call a safeDelete for the child from the parent, but it is not found... what could be wrong?
I hope this makes sense
Thanks
03/19/2007 (10:01 am)
@Gary:I have tried -unsuccessfully- your approach placing this code into my sprites creation function:
if (%montableup.getIsMounted())
{
%montableup.mountParent = %montableup.getMountedParent();
%montableup.mountParent.mountChild = %montableup;
}so I try to call a safeDelete for the child from the parent, but it is not found... what could be wrong?
%this.mountParent.mountChild.safeDelete();
I hope this makes sense
Thanks
#4
What it's done is to create the sprite inside a parent function. That way you can have all the data you need to create references without using more methods.
Hope this helps.
03/19/2007 (10:59 am)
I had this problem a while back. The solution was quite simple. The function that creates the child is a class function of the parent. Let me try to explain by script:function parent::createChild ( %this )
{
%child = new t2dStaticSprite {
// all the creation stuff here
};
// Mounting stuff here
%this.child = %child;
%child.parent = %this;
}What it's done is to create the sprite inside a parent function. That way you can have all the data you need to create references without using more methods.
Hope this helps.
#5
It look like you are trying to reference your parent with the mountParent dynamic field which is not a dynamic field in the parent.
Try this (I am assuming that %this is your parent) %this.mountchild.safedelete();
The way the relationship is set from looking at the code
mountparent is stored in the child object (this is redundant as you can use the .getmountedparent() method to get that info)
mountchild is stored in the parent object and references the child object.
so to get the child object from the parent you would use parent.mountchild
and to find the parent object you would use child.mountparent
Ricardo's code lays it out very straight forward.
03/19/2007 (2:00 pm)
@Isaac:It look like you are trying to reference your parent with the mountParent dynamic field which is not a dynamic field in the parent.
Try this (I am assuming that %this is your parent) %this.mountchild.safedelete();
The way the relationship is set from looking at the code
mountparent is stored in the child object (this is redundant as you can use the .getmountedparent() method to get that info)
mountchild is stored in the parent object and references the child object.
so to get the child object from the parent you would use parent.mountchild
and to find the parent object you would use child.mountparent
Ricardo's code lays it out very straight forward.
#6
That was really confusing since I lost the reference point easily. But now that do the trick. Thanks
03/19/2007 (4:19 pm)
@Guy:%this.mountchild.safedelete();
That was really confusing since I lost the reference point easily. But now that do the trick. Thanks
#7
05/07/2012 (5:30 am)
Would onLevelLoaded() be called when I clone the object from script? I tried setting the child reference in onAddToScene() but that returns 0 from getMountedParent().
Torque Owner Gary Preston
The way I've worked around this is to have any scene objects that have been mounted, to notify their parent mount. E.G
function SomeObject::onLevelLoaded( %this, %scenegraph ) { if (%this.getIsMounted()) { %this.mountParent = %this.getMountedParent(); %this.mountParent.mountChild = %this; } }This way the child notifys the parent that it's been mounted to it and you now have the ability to access the "mountParent" from the child, or the "mountChild" from the parent.
Obviously if the "parent" can have more than a single object mounted to it, then the above would need modifying to set mountChild as part of a list,set or however you fancy doing it.