Game Development Community

Referencing the objects

by Mirko Topalski · in Torque Game Builder · 11/17/2006 (1:55 pm) · 9 replies

How do i reference to mounted object from parent class?

i have:
function parentClass::someEvent(blah blah){
%this.doSomething()
%mountedObject.doSomethingElse()
}

How do i call %mountedObject ?

And, how do i use castCollision(%time) function?

#1
11/23/2006 (10:02 am)
Hmmm... nobody...

Ok, here is another noob's question:
is it possible to create some custom class::function() and to pass %this to it by defoult?

I have a few objects of a same class on the screen, and i want for all of them to do the same thing independently called from sceneWindow2d.onMouseMove() event. But i cant pass %this to a function!
#2
11/23/2006 (5:23 pm)
Quote:How do i reference to mounted object from parent class?

I'm not sure you can. Assuming I've not missed something, then a way around this is in the the "onLevelLoaded" callback for the objects that are getting mounted to the parent class, you can run a bit of code to inform the parent class

function someobject::onLevelLoaded(%this)
{
   // update inter-object references
   if (%this.getIsMounted())
   {
      %this.parentMount = %this.getMountedParent();
      %this.parentMount.someObject = %this;
   }
}

Obviously if you have more than one object mounted to the "parentClass" then you'll need to make a more advanced onLevelLoaded setup. Perhaps adding the objects to a simset that's a part of parent mount E.G

function someobject::onLevelLoaded(%this)
{
   // update inter-object references
   if (%this.getIsMounted())
   {
      %parentMount = %this.getMountedParent();
      %parentMount.addMountedObj(%this);
   }
}

function parentClass::addMountedObj(%this, %obj)
{   
   if (%this.mountedObjects $= "")
      %this.mountedObjects = new SimSet();

   %this.mountedObjects.add(%obj);
}

Then when you're in the "parentClass" code you can loop over all mounted objects.

%this.doSomething();
  for (%i=0; %i < %this.mountedObjects.getCount(); %i++)
  {
     %obj = %this.mountedObjects.getObject(%i);
     %obj.doSomethingElse();
  }

I could be wrong though, there could be a way to access a list of mounted objects from script, but after a quick look I couldn't see anything obvious. It probably wouldn't be too hard to modify the engine to do so though, but again I've not looked, so even that is just a "guess" :)
#3
11/23/2006 (5:26 pm)
Quote:Ok, here is another noob's question:
is it possible to create some custom class::function() and to pass %this to it by defoult?

I have a few objects of a same class on the screen, and i want for all of them to do the same thing independently called from sceneWindow2d.onMouseMove() event. But i cant pass %this to a function!

When you call a method on an object "%this" is automatically passed in for you.

so for example, in the post above, "%obj.doSomethingElse();" is a method called on whatever class the object referenced by %obj is. If we assume it's of the class "someObject" then the following would be its definition

function someObject::doSomethingElse(%this)
{
  echo("hello world!");
}

Notice how %this has been automatically passed in for you, you don't need to pass it in the doSomethingElse() call.
#4
11/24/2006 (9:15 am)
Gary, thnx a lot for a first post!
But for a second one, when i write:

function objects::onAdd(%this)
{
objects::custom();
}

function objects::custom(%this)
{
echo( %this.getID() );
}

And it doesnt work.
Note: i COULD pass %this to a custom() function, but thats not my goal, since i nead to call custom() from sceneWindow2d::onMouseMove() event.
#5
11/24/2006 (11:45 am)
%this is just a parameter that gets passed automatically when you call a method on an object. Your onAdd() should look like this:

function objects::onAdd (%this)
{
   %this.custom();
}

Then it will call your custom() method and pass the same %this that was passed to onAdd(). If you want to call it from sceneWindow2D::onMouseMove(), you don't call custom() by passing %this explicitly since that would mean the scene window. You need to call .custom() on all the objects.
#6
11/25/2006 (2:14 am)
UUuuu, yea, now i get it.

Ok, so i do i do that? (call that function on all objects)
I tried to onAdd() function to place an

array[ %this.getId() ] = %this.getId();

So now i have id af all those objects on different array element. The problem is, that indexes of those array elements are huge (6532, 6535...) And every time i start game, those indexes are shanging. in onMouseMove() function i can place:

for ( i=1 , i>10000 , i++ )
{
if (array[ i ] > 0)
array[ i ].custom();
}

But would't that be a little bit laggy and non-elegant solution?
#7
11/26/2006 (1:54 am)
In that solution you would want if (isObject(array[i])) because uninitialized vars are "" not 0. Also, you're going to create up to 10,000 empty vars in that loop if nothing is stored in the array, because that's what TS does when you access a var for the first time and it doesn't exist yet.

If you want/need to store them in an array then store them with contiguous indices starting at 0.

Better solution is to use a SimSet. You can store your objects in it, you can loop over it (it's a collection), and when things are deleted from the sim they are automatically removed from the SimSet so you don't have to worry about holes.
#8
11/28/2006 (9:41 am)
Yea, but i dont get it, what sim sets ARE, or how do i use them. Tutorials are weak :(
#9
11/28/2006 (1:06 pm)
A SimSet is a set of simulation objects. They work very similarly to any standard collection from a number of programming languages. Ed Maurina's Game Programmer's Guide To Torque goes in depth about how to use SimSet

Tag: TGB in GPGT