Game Development Community

TGB: %this.name does not work?

by Johnathon · in Technical Issues · 11/27/2007 (5:36 pm) · 4 replies

I am working with TGB to create a solitaire game, and I have run into a road block with trying to get the name of each card that has been selected. I tried the following method and I receive an ID in the console

function Card::onMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
	echo (%this);
}

however when I try
function Card::onMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
	echo (%this.name);
}

I recieve nothing. I have assigned the to the 'Card' class and given it a name (Club2), but the console just prints a blank line. Could someone give me a little insight into how I would do this? I tried following along with the Strategy Game tutorial, however some of the calls it made (like Selections.getCount()) where custom methods and I couldn't find them anywhere.

I dug around a little more with the search feature and found this.

tdn.garagegames.com/wiki/Torque_2D/Getting_Started/ObjectSelection1Tutorial

but the following code that I am using does not work.

function sceneWindow2D::onMouseDown( %this, %mod, %worldPos, %mouseClicks )
{
        if($debugMsg::mouse::callBacks)
                echo("Mouse Down");

        //lets get a list of all the objects at the clicked point in the t2dScene
        %objList = t2dSceneGraph.pickPoint(%worldPos);
        //lets get a count of how many objects in the list
        %objCount = getWordCount(%objList);
        
        //we will start looping through the list
        for(%i=0;%i<%objCount;%i++)
        {
                //grabing the entry corresponding to the loop
                %obj = getWord(%objList, %i);
                
                //if we find an object in the list that "isSelectable = true"
                if(%obj.isSelectable)
                {
                        //we toggle a value so we know we found an object that "isSelectable"
                        %selected = true;
                        //we kick out of the loop
                        %i = %objCount;
                }       
        }
        
        //if we found an "isSelectable" object
        if(%selected)
        {
                //we then store that object as the selectedObj
                %this.selectedObj = %obj;
        
                if($debugMsg::mouse::selection)
                        echo("You have selected:" SPC %obj SPC "with the name:" SPC %obj.objectName);
        }
}

I think to problem is in the t2DSceneGraph.PickPoint(%worldPos) call. If I echo the %objList in the console it is empty. I have tried t2dScene.PickPoint (as mentioned in the tutorial) but it still prints blankies to the console. Within the TGB I have created a dynamic field on my object called isSelectable and set it to true, so in theory it should be printing the name of the object to the console shouldn't it?

Any thoughts or suggestions?
Thanks!

#1
11/27/2007 (11:10 pm)
I think if you assign a name to an object, the object's ID becomes that name. So, when an object is created, lets call it "1500" it is referenced using 1500.dynamicField or 1500.someFunction (i.e. %this = 1500). When an object has a name assigned to it, it stops being "1500" and starts being "objectName" and is therefore referenced by objectName.dynamicField or objectName.someFunction (i.e. %this = "objectName").

This means you *should* just be able to echo(%this) and the name of the object will pop out.
#2
11/28/2007 (12:39 am)
All objects have an ID, regardless of whether or not they also have a name.

to get the name of an object (if any), use %obj.getName().

eg
==>$a = new scriptobject(foo);
==>echo($a);
1540
==>echo(foo.getId());
1540
==>echo(1540.getId());
1540
==>echo($a.getId());
1540
==>echo(1540.getName());
foo
==>echo($a.getName());
foo
==>echo($a.getId().getName().getId().getName());
foo

.. you get the idea
#3
11/28/2007 (3:51 pm)
WOOO!!! That answered my question perfectly! Thanks alot Orion, my trial on TGB is running out and I've been desperatly trying to get this game put together so I can see how simple the engine is. If I can get a simple solitair game built in 30 days I will probably move off my TGE license and use TGB instead, I can't get over how simple it is to make 2D games.

@Phillip: Thanks, I was aware of how ID's worked, I assigned a Name to my object in the TGB editor and wanted to get access to it. Trying to write code around the objects ID would have been more of a headache.

Thanks guys.
#4
11/23/2012 (7:49 am)
@Phil and Orion: thanks! That's pretty crazy that Phil's method works, but Orion's method gives me the illusion that I'm working with a decent language, lol.