Accessing one scriptable object from another?
by Paul Griffiths · in Torque Game Engine · 07/08/2006 (10:27 am) · 12 replies
I have 2 scriptable objects, one is a GUI object, the other isnt.
How do I access another scriptable objects functions?
GUI object is called VideoScreen.
non GUI object is called VidCapture
Script is:
In my GUI object c++ code I need to access a function called getDevice() in my VidCapture object.
Any ideas?
How do I access another scriptable objects functions?
GUI object is called VideoScreen.
non GUI object is called VidCapture
Script is:
%VidCapture = new VideoCapture();
In my GUI object c++ code I need to access a function called getDevice() in my VidCapture object.
Any ideas?
#2
%Associated_Object = %Mainobject.associated_object;
%Associated_Object.variable or %Associated_Object.function(Bla, bla, bla);
Works fine, but it's a bit hackish.
07/08/2006 (11:00 am)
What I do usually, even thought it's a big hack is create a new variable for the object, which contains the ID of the associated object. %Associated_Object = %Mainobject.associated_object;
%Associated_Object.variable or %Associated_Object.function(Bla, bla, bla);
Works fine, but it's a bit hackish.
#3
07/08/2006 (11:04 am)
Thanks simon but im in c++ not script.
#4
07/08/2006 (11:25 am)
You could write a ConsoleMethod for the VideoScreen class called 'setVidCapture' that takes a VidCapture object as a parameter, then inside that ConsoleMethod, use something like this to translate the object ID you'll be passing to the method into the actual object:ConsoleMethod( VideoScreen, setVidCapture, void, 3, 3, "( VidCapture object)"
"Set the VidCapture object for this VideoScreen")
{
//argv[2] should contain the object instance name/id of the VidCapture object
VidCapture *vc = dynamic_cast<VidCapture*>(Sim::findObject(argv[2]));
//then do what you want to with the VidCapture instance.
//an automatically declared variable named 'object' will contain your VideoScreen instance.
}
#5
07/08/2006 (11:38 am)
Thanks Brian, looks exactly what i want.
#6
My GUI VideoScreen is placed by the GUI editor and not script.
07/08/2006 (11:53 am)
How do i go about setting setVidCapture?My GUI VideoScreen is placed by the GUI editor and not script.
#7
(in the .gui file, it should look something like this)
Then in script somewhere (such as in the GUI window's "onWake" method):
07/08/2006 (12:22 pm)
I think you can just give it a name in the GUI editor, and reference it that way, ie.,:(in the .gui file, it should look something like this)
new VideoScreen(MyVideoScreen) {
//parameters, etc...
};Then in script somewhere (such as in the GUI window's "onWake" method):
%VidCapture = new VideoCapture(); MyVideoScreen.setVidCapture(%VidCapture);
#8
07/09/2006 (6:09 am)
I tried these 2 lines on it's own but it just crashes. any ideas?VideoCapture *vc = dynamic_cast<VideoCapture*>(Sim::findObject("VidCapture"));
CVVidCapture* vidCap = vc->getDevice();
#9
Am i using it correctly?
07/09/2006 (11:45 am)
When I use this line of code it's always null.VideoCapture *vc = dynamic_cast<VideoCapture*>(Sim::findObject("VidCapture"));Am i using it correctly?
#10
Basically the approach Brian outlined above is the right way to do this.
ie, instead of having C code searching for a script object of a particular name,
provide "setter" and "getter" methods in the object and use those explicitly from script.
the resulting code will be much more robust,
and it also sidesteps whatever issue it is you're having with findObject().
07/09/2006 (12:31 pm)
Hey Paul -Basically the approach Brian outlined above is the right way to do this.
ie, instead of having C code searching for a script object of a particular name,
provide "setter" and "getter" methods in the object and use those explicitly from script.
the resulting code will be much more robust,
and it also sidesteps whatever issue it is you're having with findObject().
#11
07/09/2006 (12:39 pm)
Thanks brian & orion, ill look in to it more.
#12
07/09/2006 (1:31 pm)
Thanks all, I tried Brian's approach and it works nicely.
Torque Owner Paul Griffiths
VideoCapture vc = ???::getObjectByName???("VidCapture");
Then i go:
MyDevice mydev = vc.getDevice();