Game Development Community

Script to Engine problems

by Mehmet Kizil · in Torque Game Engine · 10/03/2005 (12:32 am) · 11 replies

I'm in need of a function where I can draw a primitive line from one objects new transform position to its original position.
This

This is how i'm calling the set method using script, $thePos works fine just by echoing it, so this variable is right. This is called just before I move the object.

%item.setLastPosition($thePos);


This is the consolemethod that I created to accept the script input

ConsoleMethod( Item, setLastPosition, void, 3, 3, "item.setLastPosition( \"x y z\");"
              "Sets the last position of the object")
{
   Point3F v;
   dSscanf( argv[2], "%f %f %f", &v.x, &v.y, &v.z );
 
   object->setLastPosition( v );
}

This is the setter method within Item.cc

void Item::setLastPosition( const Point3F &location )
{
  lastPosition = location;
}

and this is the renderObject that I added to item.cc

// Renders the object into the scene
void Item::renderObject(SceneState* state, SceneRenderImage* image)
{

	Parent::renderObject(state, image);

	Point3F currentPosition = getRenderTransform().getPosition();

	// Draw Primitives
	glLineWidth(5.0); 

    glBegin(GL_LINES);
      
      glColor3fv(mDataBlock->tracerColor); 

      glVertex3f(currentPosition.x, currentPosition.y, currentPosition.z);      
      glVertex3f(lastPosition.x, lastPosition.y, lastPosition.z);      

    glEnd();  
    glLineWidth(.1f);  
	
}

The problem is when I run it, it does indeed draw the primitive line, but from the objects new position to a wierd location, when I run through the VC++ debugger these are the values I get.

currentPosition.x = 502.220
currentPosition.y = 167.100
currentPosition.z = 224.720

lastPosition.x = -1.73483e+009
lastPosition.y = -1.73483e+009
lastPosition.z = -1.73483e+009

So lastPosition is obviously not getting updated by the script, i'm tearing my hair out over here, any help greately appreciated.

#1
10/03/2005 (1:19 am)
Can you show $thePos?

I think you should be looking at argv[1], not [2]. Tried looking at that code under the debugger?
#2
10/03/2005 (1:51 am)
I tried it under argv[1] and argv[2],

when I set a breakpoint in Torque dev, $thePos comes up as what it should be. It seems that its transition from $thePos to lastPosition stuffs up the values
#3
10/30/2005 (6:18 am)
Hey Mehmet,

I have the same problem. Did you find a solution to the problem? If you did i'm very interested in the solution.

/Imm
#4
10/30/2005 (9:49 am)
The technique for reading the ConsoleMethod argv line expects the position argument to be exactly that--one argument (not three). In other words, if:

$thePos = "100.4 200.3 55.1";

then it should be working (assuming the rest of the code is ok).

Also, the Point3F currentPosition = getRenderTransform().getPosition(); line looks suspicious. Might be better to simply take the position column of the matrix directly.
#5
10/31/2005 (3:07 am)
Hi again,

The weird thing is, that lastPostion seems to be working in setLastPosition function, but not in renderObject(SceneState* state, SceneRenderImage* image). I've made lastPostion a public Point3F. This code illustrates the problem:

void Item::setLastPosition( const Point3F &location )
{
lastPosition = location;
//Prints the correct lastPostion passed from the script ...
dPrintf("lastPosition: %d %d %d", (int)lastPosition.x, (int)lastPosition.y, (int)lastPosition.z);
}

void Item::renderObject(SceneState* state, SceneRenderImage* image)
{
...
// Prints a wrong lastPostion ... somthing like this:
// lastPosition.x = -1.73483e+009
// lastPosition.y = -1.73483e+009
// lastPosition.z = -1.73483e+009
dPrintf("lastPosition: %d %d %d", (int)lastPosition.x, (int)lastPosition.y, (int)lastPosition.z);
...
}

Any suggestions how to get the correct lastPostion in renderObject(SceneState* state, SceneRenderImage* image)?

/Imm
#6
10/31/2005 (6:05 am)
Where and how is lastPosition actually defined in your code?
#7
10/31/2005 (6:05 am)
Where and how is lastPosition (the variable) actually defined in your code?
#8
10/31/2005 (6:22 am)
In the header file: item.h as follows:

class Item: public ShapeBase
{
...
public:
Point3F lastPosition;
...
#9
10/31/2005 (6:33 am)
Argh, took me a bit to wrap my head about this but:

The issue in a nutshell is that you are setting the values of the object on the server side of your process, but the rendering occurs on the client side of the process. Since you haven't explicitly networked the value of lastPosition, the client side values are never modified, and therefore are not correct.

The quickest fix will probably be to look at the pack and unpackUpdates of your object and add in networking for lastPosition.

Alternatively (and this will take some design), you'll need some way to track and store the last position of your object on the client side (possibly every time setTransform() is called maybe?).

The root design here is that even in a single player game, there is both a "server" side, and a "client" side to your game (they just happen to be in the same executable--and while you can get away with some namespace sharing, for the most part you should try to conform to standard Torque networking).
#10
10/31/2005 (6:52 am)
Thanks for the information. I'm kind of new to Torque and im not sure what pack and unpackudates are and how they work or where to add them :o). Any insights/examples are greatly appriciated.

/Imm
#11
10/31/2005 (8:47 am)
TDN Article on Torque Networking should be a good place to start.