Scripting Languages
by Jason Guzzardo · in Torque Game Engine Advanced · 06/19/2008 (6:34 am) · 8 replies
Are there any resources for scripting language alternatives to Torque Script? I saw something for Python when doing a search but it seems this resource isn't fully functional in 1.7? Is this correct? I'm just looking for something that supports more OO features than Torque Script, which seems to have none. I am new to Torque but it seems that I can't define a generic object (in script) with properties and methods etc, without some kind of C++ counter part. I tried using ScriptObject for generic script objects in an effort to encapsulate functionality but i'm not sure if this is a good approach. While I have no problem modifying C++ code, it seems reasonable to want lightweight script objects that only exist in script. For example, it would be nice to create a script object or some other type of abstraction for a server that encapsulates server based functions, client connections, etc. There are other places in game scripts where creating abstractions like this would be useful.
Anyway, it's not a big deal, I'm just wondering if there are any alternatives or maybe there are features of Torque Script I'm not aware of.
P.S. I am aware of packages but these aren't really true ad-hoc polymorphism which is achieved through inheritance. Unless I'm missing something, this only provides the ability to override/chain function calls kind of like the chain-of-responsibility GoF design pattern . However, if I'm correct, they are useless in terms of creating abstractions or achieving some form of encapsulation.
Any suggestions or a point in the right direction would be greatly appreciated.
Anyway, it's not a big deal, I'm just wondering if there are any alternatives or maybe there are features of Torque Script I'm not aware of.
P.S. I am aware of packages but these aren't really true ad-hoc polymorphism which is achieved through inheritance. Unless I'm missing something, this only provides the ability to override/chain function calls kind of like the chain-of-responsibility GoF design pattern . However, if I'm correct, they are useless in terms of creating abstractions or achieving some form of encapsulation.
Any suggestions or a point in the right direction would be greatly appreciated.
About the author
#2
06/19/2008 (8:49 am)
Btw: I'm not sure what you mean when you say Torque Script has no OO features. As far as I can tell it does, including encapsulation.
#3
Additionally, encapsulation implies that an object exposes a public interface while keeping it's implementation details private.
Here is the wikipedia definition
In computer science, the principle of information hiding is the hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. The protection involves providing a stable interface which shields the remainder of the program from the implementation (the details that are most likely to change). In modern programming languages, the principle of information hiding manifests itself in a number of ways, including encapsulation (given the separation of concerns) and polymorphism.
I don't see a way to to create private methods or properties on an object in script, hence no encapsulation. Additionally, the only way I can even create an object in script is if it exists in the engine. If Torque Script supports OO then I should be able to implement OO design patterns like "Abstract Factory", "Chain of Reponsibility", "Visitor", etc. You would not be able to accomplish this without modifying the engine because, again, objects can't live in script without also living in the engine. If I were using a scripting language like Python or Lua, I would be able to mix and match custom script objects with engine objects in any way I want.
My simple goal is to use some OO features, in script, to create abstractions that promote re-use, encapsulation, and help make my scripts more maintainable.
If this is possible through some community developed resource cool, if not no big deal. Just checking to see what my options are, if any.
06/19/2008 (9:29 am)
Thank you. I'm currently working with ScriptObject in the manner you demonstrated. However, I get a whole slew of methods and properties that don't apply to the object I'm trying to build. I'd like, if possible, to define my own generic custom objects, like I would in C++ but have them live only in script. Additionally, encapsulation implies that an object exposes a public interface while keeping it's implementation details private.
Here is the wikipedia definition
In computer science, the principle of information hiding is the hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. The protection involves providing a stable interface which shields the remainder of the program from the implementation (the details that are most likely to change). In modern programming languages, the principle of information hiding manifests itself in a number of ways, including encapsulation (given the separation of concerns) and polymorphism.
I don't see a way to to create private methods or properties on an object in script, hence no encapsulation. Additionally, the only way I can even create an object in script is if it exists in the engine. If Torque Script supports OO then I should be able to implement OO design patterns like "Abstract Factory", "Chain of Reponsibility", "Visitor", etc. You would not be able to accomplish this without modifying the engine because, again, objects can't live in script without also living in the engine. If I were using a scripting language like Python or Lua, I would be able to mix and match custom script objects with engine objects in any way I want.
My simple goal is to use some OO features, in script, to create abstractions that promote re-use, encapsulation, and help make my scripts more maintainable.
If this is possible through some community developed resource cool, if not no big deal. Just checking to see what my options are, if any.
#4
* - If you're planning to do AI, Lua has co-routines which are extremely awesome.
** - I find the whole "indents define scope" thing to be highly ridiculous ;p
06/19/2008 (11:18 am)
There are a couple of things lying around to create a bridge between TorqueScript and Python or Lua (personally I'd go with Lua though, for various reasons* including speed and flexibility...and its syntax isn't nearly as bad**). There's also a resource that adds some OO features to TS.* - If you're planning to do AI, Lua has co-routines which are extremely awesome.
** - I find the whole "indents define scope" thing to be highly ridiculous ;p
#5
06/19/2008 (11:29 am)
Excellent! Thanks Ross! I'll look into these resources.
#6
You're right that Script objects don't provide "true" public/private encapsulation, but I'm willing to give up some of those stricter principles for the ability to do really nifty things:
When creating a "pure" Script object, you don't need to predefine properties, only its methods. Any field (property) you attempt to reference on the object will either be returned (if that property has been assigned a value) or will return a NULL string. Methods can be scoped to the object name, the object "class" (creating a group of functionality for similar objects), or for the object type.
06/19/2008 (1:05 pm)
What is it that you are getting with a ScriptObject that you don't want? The only properties and methods you get when you declare a ScriptObject is what is necessary for it to "live" as an object in the Torque Simulation.You're right that Script objects don't provide "true" public/private encapsulation, but I'm willing to give up some of those stricter principles for the ability to do really nifty things:
for(%i = 0; %i < $MAX_INDEX; %i++) {
%myObject = "NameKnown" @ %i;
%myObject.doSomethingCool();
}When creating a "pure" Script object, you don't need to predefine properties, only its methods. Any field (property) you attempt to reference on the object will either be returned (if that property has been assigned a value) or will return a NULL string. Methods can be scoped to the object name, the object "class" (creating a group of functionality for similar objects), or for the object type.
new ScriptObject(MyTestThing);
function MyTestThing::getStuff(%this) {
// note that the property "stuff" never needs to be pre-defined, you can simply just start using it
return %this.stuff;
}
#7
I guess the best way to describe it would be to look at web development many years ago when scripting the browser was largely a roll your own solution using JavaScript. These days there are object oriented JavaScript libraries that make things like AJAX and DOM scriping much easier. As I become more familiar with common patterns for game development, using Torque, it would be nice to be able to refactor code into frameworks using basic OO techniques. However, an OO framework is not really required to make a great game using Torque so I don't want to get too concerned about limitations in TS. I just wanted to make sure I wasn't overlooking something.
Thank you again for all the help!
06/19/2008 (2:53 pm)
Hey Mark, yes the kind of capability you displayed is definately cool. I understand why the scripting language is so tightly integrated with the engine and it does make sense. Additionally, you are right, the only functions that exist in the ScriptObject are those required for the simulation. Being relatively new to the technology I just wanted to see if there were some additional OO features I might have missed. I also wanted to find out if there were any community projects that might have extended Torque Script to include some OO features. These are more nice-to-haves than requirements. I also agree with you that there are some pretty cool things you can do with TS and I'm definately not one of those rigid OO puritans. I guess the best way to describe it would be to look at web development many years ago when scripting the browser was largely a roll your own solution using JavaScript. These days there are object oriented JavaScript libraries that make things like AJAX and DOM scriping much easier. As I become more familiar with common patterns for game development, using Torque, it would be nice to be able to refactor code into frameworks using basic OO techniques. However, an OO framework is not really required to make a great game using Torque so I don't want to get too concerned about limitations in TS. I just wanted to make sure I wasn't overlooking something.
Thank you again for all the help!
#8
06/19/2008 (2:59 pm)
No problem. Good luck in your efforts!
Torque 3D Owner mb
$foo = new scriptObject()
{
class = response;
text = "I'm foo";
}
$bar = new scriptObject()
{
class = response;
text = "I'm bar";
}
function response::getText(%this) {
return %this.text;
}
echo($foo.getText())
=> "I'm foo"
echo($bar.getText())
=> "I'm bar"