Game Development Community

Calling an object's function from within a different object's function?

by Infinitum3D · in Torque Game Engine · 04/30/2009 (10:48 am) · 6 replies

I have two animatable objects, a window and a curtain.

I want the curtain animation to play when I open the window, and I want the curtain animation to stop when the window closes. Sounds easy, right?

function window::OpenWindow(%this, %window)
{
%window.playthread(0,"open");
//--How do I call function curtain::WindBlowing?
}

function curtain::WindBlowing(%this, %curtain)
{
%curtain.playthread(0,"wind");
}
I think I need to use getObjectID or something, right?
Do I have to create the curtain in the window::onAdd? Then get it's ID?

Am I clueless (I can answer that... "Yes!")

Any help, as always, is greatly appreciated.

Thanks,
Tony

#1
04/30/2009 (11:36 am)
Here are some ideas for workarounds:
-Like you say, create the curtains when you create the window, and that way you can store the correct object ID.
-Name the curtains, and have the window search for an object of that name.
-Make the window and curtains one object (and one DTS file), so you don't have to deal with multiple objects.
#2
04/30/2009 (2:26 pm)
*Make the curtains and window one object...

What a great concept! Wish I 'd thought of that.

EDIT: Can't make them a single object. I need to be able to destroy them individually, like shoot out the window but keep the curtains, or burn off the curtains but keep the window open to let the smoke out...

Thanks Dan, by using the curtain's onAdd function, I can create the windows whenever a curtain is placed.

I'll figure out how to use 'getObjectID' to keep the objects recognizing each other.

Tony
#3
07/03/2009 (2:20 am)
I dont know how to get name's value that is located in any script object. ıf you know how to do this, please ınform me!

thanks,
#4
07/03/2009 (3:18 am)
%myOb = nameToId("NameOfYourObject");
This gets the name of your object, grabs it's ID number, and stores it in %myOb. You can do with it what you want from there.
#5
07/03/2009 (3:41 am)

Kinda older thread, but anyways...

Re the original question:

Either make the curtain a globally named object (bad) or simply store a reference to it on the window object (good). Like so:

function Window::onAdd( %this ) // or somewhere else
{
   %this.curtain = createMeACurtainObject();
   //...
}

// then you can reference the curtain that goes with a particular window like so

function Window::openWindow( %this ) // %window above really should be the receiver (%this) object here
{
   %this.playThread( 0, "open" );
   %this.curtain.windBlowing();
}

@Nazif
To get the value associated with a name, simply use the name in script:

%foo = MyNamedObject;

To dynamically look up the object for a name contained in a string, use nameToID:

%foo = nameToID( "MyNamedObject" );
%foo.ohYeah();

#6
07/03/2009 (4:04 am)
thanks guys, I solved my problem rıght now! Thanks agaın for sharıng ınformatıon wıth us!