Game Development Community

how to get object ID from parent?

by Brian Carlson · in Torque 2D Beginner · 07/11/2016 (7:09 pm) · 2 replies

Hello there!
I have a question about objects and arrays. I created a sprite object which itself is made up of an array of sprites. (I'm still fuzzy on objects, but I found a sprite object can have members, which includes other sprites.)

In my program, %lvl contains arrays of sprites: buildings, towns, and characters. For one case, I have onCollision called when something collides with a town. From that function call, I want to access everything else in the %lvl object.

function declareCharacterArray()
{
	%lvl = new Sprite( levelConstruct );
	
	%lvl.bldgs[0] = 0;
	%lvl.towns[0] = 0;
	%lvl.regions[0] = 0;
}

function levelConstruct::declareTowns( %this )
{
	%this.towns[0] = new Sprite( town );
}

function town::onCollision(%this, %sceneobject, %collisiondetails)
{
	// %this is the ID of a specific town from town[] array
	// how can I get the ID of %lvl from the town sprite?

	echo("  %this.townID = " @ %this.townID);
}

Let's say in the onCollision function, %this = 1234. The %lvl object is 5678, but I don't know of a straightforward way to get to that. Is there something like %this.parent to get that value?

Thanks in advance!
Brian

#1
07/11/2016 (9:39 pm)
Not sure if you can by default, but it's not too hard to simply add a field to the object as you're adding it to the parent...

function levelConstruct::declareTowns( %this )  
{  
    %townsprite = new Sprite(town);
    %townsprite.parentObj = %this; // you can add custom fields just this easily.
    %this.towns[0] = %townsprite;
}

Especially when you consider the array %this.towns[] is just a custom field that happens to be an array....
#2
07/12/2016 (3:04 pm)
Hi Richard,
That sounds good to me.
Thanks!