Game Development Community

Object Oriented Programming in Torque Script

by Bryan Edds · in Technical Issues · 07/05/2004 (5:41 pm) · 85 replies

(NEW) IMPORTANT NOTE: Ben and I are trying to get this OO stuff to work by modifying the back end of the engine. There's no promise as to whether this will work out of not, but we will be finding that out soon. And if it works, you can expect a great tutorial frrom me very soon!

This is a rough draft for the tutorial I'm making. The tutorial aims to teach people how to program Torque Script in an object-oriented fashioned. If you find that your script's complexity is ballooning out of control with too many global variables and functions, then you may wish to organize your code with with the object orientation paradigm. But, it may be even better to put that code in C++ instead of OO script if it requires more than one layer of inheritance.

First of all, here is a highly commented template which will show a general pattern for organizing a Torque script class and explains how the object oriented features are implemented. Note you will substitute the word Class with the name of the class you wish to use, and substitute varName with the name you want the variable to be -

/////////////////////////
// CLASS Class
/////////////////////////
	
	/////////////////////////
	// INITIALIZATION:
	/////////////////////////
	function Class::create(%objectName)
	{
		// you should usually use "new ScriptObject(...)" when creating a new class
		%this = new ScriptObject(%objectName)
		{
			class = Class;
		}
	
		// create member variables and optionally initialize them to parameters.
		// once a member is created here, it persists for the life of %this object.
		
		/////////////////////////
		// PUBLIC MEMBERS:
		/////////////////////////
		%this.varName2 = 0;
		
		/////////////////////////
		// PRIVATE MEMBERS:
		/////////////////////////
		%this._varName = 0;
	
		return %this;
	}
	
	function Class::delete(%this)
	{
		// call the parent class's delete method as well
		parent::delete(%this);
	
		// insert rest of deletion code here...
	}
	// you MAY want to override this function to handle destruction, but as
	// it stands, you must ALWAYS call delete on any OO class you make
	// if you want to be certain delete() is called.
	
	/////////////////////////
	// PUBLIC INTERFACE:
	/////////////////////////
	
	// you can optionally make an accessor and mutator function for each of your
	// private members if you want them to be accessible and / mutable to the 
	// client. Remember, you do not need to make accessor or mutators functions 
	// for public members since they are already accessible as they are.
	
	function Class::getVarName(%this)
	{	return %this._varName;}
	
	function Class::setVarName(%this, %value)
	{	%this._varName = %value;}
	
	/////////////////////////
	// PRIVATE INTERFACE:
	/////////////////////////
	
	function Class::_incrementAge(%this)
	{	%this._age++;}
	// because this is a private function, it should only be called
	// by code in this class
	
/////////////////////////	
// END CLASS Class
/////////////////////////
Page«First 1 2 3 4 5 Next»
#81
07/31/2004 (2:35 am)
Figured out a way to put all the inheritance functionality back into SimObject, and have thus removed ScriptObject from the linkage of the scripting objects.

Still waiting for Ben to give me advice on GameDatablock :)

And if your curious as to why I'm constantly updating this thread for little reason, well, I'll give you a hint -

20-minute compiles :)
#82
07/31/2004 (10:55 am)
GameDataBlock should inherit from SimDataBlock unless you are making a subclass with it... right?
#83
09/20/2004 (8:33 pm)
Does anyone pay attention to programming paradigm history? People started programming in OOP way before C++ or any other OOP language was ever developed. They purposefully went to great lengths to try and manage the code in a more sophisticated way than what so called procedural techniques traditionally allowed. OOP languages are a formalization of those techniques. Does OOP have value? Yes. Can the techniques be used to help managed huge projects by breaking them into smaller pieces (objects) even with procedural compilers? Yes. Is somebody gonna tell your mommy, your CS teacher, or your pastor if you don't? No. Should you jump down the throat of people suggesting an alternate way of doing business? No.

Its my game I can OOP if I want to...

Thanks,
Frank
#84
09/20/2004 (9:19 pm)
Thats right you can code whatever way you want.

Thats post seemed to come a bit out of left-field for me. I'm following this thread with interest to see what he does, but in no way do I feel like I am forced to use what he comes up with.

Its his time, he can oop-ise the engine all he wants to.

Thanks,
Vernon.
#85
09/21/2004 (1:29 pm)
Hey guys, I figured everyone here would have already seen it, but the OOP in Torque script has been released for a while :)

I put it up on the GG resource for download. Sorry, I should've made note of it here, but it slipped my mind.

Check it out here -

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=6335

It builds with 1_2 of Torque. I have not tested it with 1_3. It should probably work with both. If not, leave a note here, and I'll make the necessary updates.
Page«First 1 2 3 4 5 Next»