New class in script?
by Jon Paynter · in Torque Game Engine · 07/27/2006 (10:09 pm) · 9 replies
Ive searched around the forums and all the posts ive found skirt around the edge of what im trying to do here, but nothing directly addresses things.
I have the following:
and then a couple methods:
which all compilese fine. but when I try to run: %NewPlayer.initData(); or %NewPlayer.ReRoll();
I get the following errors:
starter.rpg/client/ui/CharacterCreatorGui.gui (490): Unknown command initData.
Object ClientPlayer(16835) SimObject
starter.rpg/client/ui/CharacterCreatorGui.gui (491): Unknown command ReRoll.
Object ClientPlayer(16835) SimObject
how do I get the above to work?
Namely the ability to create my own classes or class-like constructs
I have the following:
%NewPlayer = new SimObject(ClientPlayer)
{
class = ClientPlayer;
};and then a couple methods:
function ClientPlayer::initData()
{
// stuff
}
function ClientPlayer::ReRoll()
{
// stuff
}which all compilese fine. but when I try to run: %NewPlayer.initData(); or %NewPlayer.ReRoll();
I get the following errors:
starter.rpg/client/ui/CharacterCreatorGui.gui (490): Unknown command initData.
Object ClientPlayer(16835) SimObject
starter.rpg/client/ui/CharacterCreatorGui.gui (491): Unknown command ReRoll.
Object ClientPlayer(16835) SimObject
how do I get the above to work?
Namely the ability to create my own classes or class-like constructs
#2
07/29/2006 (12:04 am)
Try changing your SimObject name to something other than your class name (so that they are not the same). Also make sure that your functions are defined before they are used.
#3
heres what I had:
Unknown command initData.
one thing that still hasnt been answered is weather or not I can even DO this.. say nothing if im doing it right.
07/29/2006 (6:30 pm)
Tried that... still didnt work.heres what I had:
function ClientPlayer::initData()
{
echo("initData called");
}
function ClientPLayer::ReRoll()
{
echo("ReRoll called");
}
function CharacterCreatorGui::onWake(%this)
{
$NewPlayer = new SimObject(ClientPlayer1)
{
class = ClientPlayer;
};
$NewPlayer.initData();
$NewPlayer.ReRoll();
}instead of getting "initData called" and "ReRoll called" in the console I get the same 2 errors above: Unknown command initData.
one thing that still hasnt been answered is weather or not I can even DO this.. say nothing if im doing it right.
#4
07/29/2006 (7:37 pm)
Instead, do:$NewPlayer.getDataBlock().initData(); $NewPlayer.getDataBlock().ReRoll();
#5
now i get this:
==>Canvas.pushDialog(CharacterCreatorGui);
starter.rpg/client/ui/CharacterCreatorGui.gui (712): Unknown command getDataBlock.
Object ClientPlayer(16870) SimObject
starter.rpg/client/ui/CharacterCreatorGui.gui (712): Unable to find object: '' attempting to call function 'initData'
replacing getDataBlock() with dataBlock gives this error:
starter.rpg/client/ui/CharacterCreatorGui.gui (712): Unable to find object: '' attempting to call function 'initData'
any more suggestions?
08/04/2006 (6:06 pm)
That doesnt work either.now i get this:
==>Canvas.pushDialog(CharacterCreatorGui);
starter.rpg/client/ui/CharacterCreatorGui.gui (712): Unknown command getDataBlock.
Object ClientPlayer(16870) SimObject
starter.rpg/client/ui/CharacterCreatorGui.gui (712): Unable to find object: '' attempting to call function 'initData'
replacing getDataBlock() with dataBlock gives this error:
starter.rpg/client/ui/CharacterCreatorGui.gui (712): Unable to find object: '' attempting to call function 'initData'
any more suggestions?
#6
to
08/07/2006 (11:44 am)
Try changing%NewPlayer = new SimObject(ClientPlayer1)
{
class = ClientPlayer;
};to
%NewPlayer = new ScriptObject(ClientPlayer1)
{
class = ClientPlayer;
};
#8
Originally, TGE was built with the concept that everyone would always implement a new c++ class for their objects, and simply use TorqueScript to manipulate those objects. Namespace linking was designed such that whenever you called a method on a Torque Object, the scripting system would search the namespaces associated with the inheritence tree of the c++ class.
Now, in some cases, it does make sense to have some basic namespace classing system in TorqueScript, but it is important to note: TorqueScript is not fully Object Oriented. We fake some things with special classes of objects to give some "oop-ness", but in general if you want full oop, you should create new c++ classes.
In stock TGE, any script object instantiated from a GuiObject, or a ScriptObject, will allow you to use the "class" and "superclass" persistent fields to link additional namespaces--as well as a namespace for the instantiation's unique nae (ClientPlayer1 in the above example), but this does not apply for all object types.
08/10/2006 (11:58 am)
For those that may follow along on this discussion, the reason ScriptObject works when "normal" objects don't is a design decision:Originally, TGE was built with the concept that everyone would always implement a new c++ class for their objects, and simply use TorqueScript to manipulate those objects. Namespace linking was designed such that whenever you called a method on a Torque Object, the scripting system would search the namespaces associated with the inheritence tree of the c++ class.
Now, in some cases, it does make sense to have some basic namespace classing system in TorqueScript, but it is important to note: TorqueScript is not fully Object Oriented. We fake some things with special classes of objects to give some "oop-ness", but in general if you want full oop, you should create new c++ classes.
In stock TGE, any script object instantiated from a GuiObject, or a ScriptObject, will allow you to use the "class" and "superclass" persistent fields to link additional namespaces--as well as a namespace for the instantiation's unique nae (ClientPlayer1 in the above example), but this does not apply for all object types.
#9
creating a myrad of C++ classes is exactly what im trying to avoid. im not looking for any of the advanced OO concepts here - simple inheritance and method override will work fine.
Although if there was a java or Smalltalk interface to torque - that would be a different ballgame.
08/11/2006 (3:31 pm)
Thanks for the summary.creating a myrad of C++ classes is exactly what im trying to avoid. im not looking for any of the advanced OO concepts here - simple inheritance and method override will work fine.
Although if there was a java or Smalltalk interface to torque - that would be a different ballgame.
Torque 3D Owner Jon Paynter