OOP in TorqueScript, continued
by Vincent Kane · in Torque Game Engine · 09/19/2005 (2:59 pm) · 5 replies
I've been going around in circles with these online docs and some of the example scripts trying to figure out exactly how classes can be extended purely in the script. The following code ("exampleized" from the SDK/example/weapon & crossbow .cs's) gives me the error "Unknown command Output()", but works when I replace %foo.Output() with Class1::Output(%foo). What am I doing wrong? Or am I always going to have to use the className namespace? That just doesn't sound right. And is there a good writeup somewhere of the OOP aspects the torquescript? I don't think the official online docs do a decent job.
// test.cs
// a script file for general testing of script functionality
echo("------ Entering test.cs ------");
datablock ItemData(Class1Data) {
category = "Class1";
className = "Class1";
};
datablock ItemData(Class2Data) {
category = "Class2";
className = "Class2";
};
// simple class methods
function Class1::Output(%this) {
echo("Class 1");
}
function Class2::Output(%this) {
echo("Class 2");
}
//new objects
%foo = new Item() {
dataBlock = "Class1Data";
};
%bar = new Item() {
dataBlock = "Class2Data";
};
%foo.Output();
%bar.Output();
// test.cs
// a script file for general testing of script functionality
echo("------ Entering test.cs ------");
datablock ItemData(Class1Data) {
category = "Class1";
className = "Class1";
};
datablock ItemData(Class2Data) {
category = "Class2";
className = "Class2";
};
// simple class methods
function Class1::Output(%this) {
echo("Class 1");
}
function Class2::Output(%this) {
echo("Class 2");
}
//new objects
%foo = new Item() {
dataBlock = "Class1Data";
};
%bar = new Item() {
dataBlock = "Class2Data";
};
%foo.Output();
%bar.Output();
#2
09/19/2005 (4:12 pm)
Topics "Inheriting object methods in Torque Script" and "Some general newbie torque script questions" might help.
#3
But I guess my biggest question remains unanswered. The way I understand datablocks is basically as a static struct for all instantiations of that datablock "class"...
But what if I want two or more objects with the same methods and the same fields (but variable - I don't care about any shared data, except maybe the fact that the objects are members of the same class) - how would I define such a class in the script? I mean, this is pretty basic OOP stuff. And maybe the answer is that it can't be done in script, but if it can be done, I'd like to know - that makes prototyping much easier than having to extend classes in the engine and then recompiling.
09/20/2005 (9:14 am)
Thanks, those helped a little bit... it sounds like script inheritance is geared more towards associating "class" functions with datablocks than with generic classes, though.But I guess my biggest question remains unanswered. The way I understand datablocks is basically as a static struct for all instantiations of that datablock "class"...
But what if I want two or more objects with the same methods and the same fields (but variable - I don't care about any shared data, except maybe the fact that the objects are members of the same class) - how would I define such a class in the script? I mean, this is pretty basic OOP stuff. And maybe the answer is that it can't be done in script, but if it can be done, I'd like to know - that makes prototyping much easier than having to extend classes in the engine and then recompiling.
#4
09/20/2005 (9:29 am)
And not to be an @$$, but if you can't see what I'm attempting to do from the code I posted, then you probably don't need to be responding.
#5
09/20/2005 (10:52 am)
You don't want to be using datablocks with inheritance. Use regular classes like those in the tsoo.csl example that comes with the OOP in script resource. Although the resource allows inheritance with datablocks, it's probably not what you need to do what you want.
Torque Owner Dracola