Game Development Community

Spawning and setting up a t2dTextObject in the script [solved]

by Maxim Eremeev · in Torque Game Builder · 11/01/2011 (2:28 pm) · 4 replies

Hi!

I'm trying to write a script, that would spawn a t2dTextObject for each object of a particular class (Point), that's in the scene. I also want each t2dTextObject to be of the same size and on the same position as it's "parent" object (essentially - on top of it). And for whatever reason I'd like it to display a value, that the script defines at random. The problem is, that I get an error in the console each time I try to test this script and the problems seem to start right at the point, when I try to set the text object's position. If I get rid of that line, the next one causes problems as well. No text is ever displayed in the scene window itself.
Here's the script I came up with so far. I'm really new to this, so it's probably full of holes and the answers are obvious, but this is exactly why I'm writing this post: because I want to learn a way to do things right and can't find any info that could help on my own.

function Point::onLevelLoaded(%this, %scenegraph)
{
   %this.countCalc = getRandom(1,4);
   
    switch(%this.countCalc)
   {
      case 1:
      %this.value = 10;
      
      case 2:
      %this.value = 15;
      
      case 3:
      %this.value = 20;
      
      case 4:
      %this.value = 25;
   }
   
   %this.position = %this.getPosition();
   %this.size = %this.getSize();
   %this.createOutput();
   
   Point.schedule(1, "updateOutput");
}
   
function Point::createOutput(%this)
{
   %this.valueOutput = new t2dTextObject()
   {
      size = %this.size;
      font = "Arial";
      textAlign = "Center";
   }
   
   %this.valueOutput.setPosition(%this.position);
   %this.valueOutput.setSize(%this.size);
   %this.valueOutput.text = %this.value;
}

function Point::updateOutput(%this)
{
   %this.valueOutput.text = %this.value;
}


#1
11/01/2011 (4:37 pm)
Could you mount the t2dTextObject to point?

%this.mount(%object, %offsetX, %offsetY, %force, %trackRotation,
%sendToMount, %ownedByMount, %inheritAttributes)

It looks like you have a mistake in the first function.

Instead of %this.createOutput() in the onlevelloaded function try createOutput(%this)
#2
11/01/2011 (6:56 pm)
Hmm, could you provide more information about the error?

Oh! Wait, I think I found the error.

Line 8:
function Point::createOutput(%this)  
{  
   %this.valueOutput = new t2dTextObject()  
   {  
      size = %this.size;  
      font = "Arial";  
      textAlign = "Center";  
   }; <--
     
   %this.valueOutput.setPosition(%this.position);  
   %this.valueOutput.setSize(%this.size);  
   %this.valueOutput.text = %this.value;  
}

You need that semicolon since you are declaring the fields of the object.
#3
11/02/2011 (8:18 am)
Thanks, guys!

Mounting works and the additional semicolon helped too. I'm now at the point, where I don't get any errors, when testing out the scene, but I still can't get my text objects to actually display any text inside the scene window.

Here's what I do:

%this.valueOutput = new t2dTextObject()  
   {  
      font = "Arial";  
      textAlign = "Center";  
      scenegraph = %this.scenegraph;
   };
   
   %this.valueOutput.text = %this.value;
   %this.valueOutput.mount(%this, 0, 0, 0, false, true, true, true);

   %this.valueOutput.setLayer(5);
   %this.valueOutput.setSize(%this.getSize());

As far as I can understand, the objects are in fact created and all the values are right, because when I add this

$pointSet.add(%this.valueOutput);
   echo(%this.valueOutput.getSize());
   echo(%this.valueOutput.text);
   echo(%this.valueOutput.getPosition());
   echo($pointSet.getCount());

the console returns correct values: the size, the text message, the position of each object and number of entries in the SimSet, where every new text object gets added after it's created.
Is there something I'm missing here?

#4
11/02/2011 (1:08 pm)
Okay, problem solved. I only had to add a couple of fields.

%this.valueOutput = new t2dTextObject()  
   {  
      font = "Arial";  
      textAlign = "Center";  
      scenegraph = %this.scenegraph;     
      fontSizes = "80";
      hideOverflow = "0";
   };

Again, thank you guys.