Game Development Community

ScriptObject problem

by Ted "darkhitman" Nubel · in Torque Game Builder · 03/24/2005 (5:36 pm) · 7 replies

I've been working on the basic framework of my RTS project, and, in doing so, I needed a system to add objects to the game. So, I'm using this function:

function addObject(%army,$name)
{

	$unit[$unitCount] = new ScriptObject(ENTITY_UNIT)
	{
   	        exec("./data/units/" SPC %name SPC "_info.cs");
		%unitNumber = $unitCount + 1;
		$unitCount += 1;
	};

	
}
However, it returns an error at the first " in the exec statement. This is not because of the statement itself, but rather, the $unit[... line. What could possibly cause T2D to throw an error based on that line? It follows the correct syntax, to my knowledge. Like all of my bugs, this one is probably quite obvious. Anyone have any ideas?

#1
03/24/2005 (5:41 pm)
You're putting an exec in an object declaration. Which isn't going to work.
#2
03/24/2005 (6:18 pm)
I wish that were the real problem, but when I comment out the exec(), it still throws an error...this time, in the = sign after %UnitNumber.
#3
03/24/2005 (8:34 pm)
Yeah, on execs, you only need to call those once. The call you'd do instead of an exec will be the eval function. We can get into that later, because you still have a problem with your core instantiation here.

I'm working with O-O in Torquescript too. I was never able to get my constructor to take in values the way you seem to be trying. I had to set them after the call to "new". I'm assuming "unitNumber/unitCount" are "members" of your "ENTITY_UNIT" class? Hows about showing your constructor:

function ENTITY_UNIT::onAdd(%this)
{
}
#4
03/25/2005 (5:39 am)
I think the problem is that you're treating the object initialization block like a function block. I don't think you can just put any code there. It must be initialization values for fields specific to that object, not just any variables from your script. So, the syntax looks incorrect to me.

$.02
#5
03/25/2005 (11:13 am)
Ok, I moved the exec() call and the increment into my constructor function. The assignment statement works, though, so I left it in there.

The exec() statement actually does what I wanted it to do, so I guess that's good news. Thanks for your help.

The problem, I guess, is that I'm still adapting to the OO of T2D. Hopefully, I'm past the worst of it all.
#6
03/25/2005 (1:58 pm)
The OO of T2D as it stands is rather weak. There is documentation on it and I'm still new to T2D so I might have something a bit off, but it goes roughly like:

$newObject = new ScriptObject(ENTITY_UNIT)
   {
      // initialize base objects data members
      // initialize your data members
   };

The OO resource by Bryan Edd is probably what you want. It allows indefinite subclassing, provides a structure for constructors, destructors, etc. Much more familiar if you know C++
#7
03/25/2005 (3:42 pm)
Think of it this way:

-your exec() statement is like an "#include" statment.
-put all of your members, constructors, destructors, and functions specific to a class into one file and that is your class implementation. (there is no analogy for a .h file)

then, instantiation is done just like you have been, and like tim said:

$obj=newScriptObject(ClassName);

from there, you have access to all your methods and functions with the dot operator. So not too different if you really think about it.