Game Development Community

Player Position

by Howard Dortch · in Torque Game Engine · 05/22/2004 (5:52 am) · 29 replies

How would I retreive the player current position on the landscape?
I looked in the player.cc file and see pos but it's private and I dont see a function to call the data.
Page «Previous 1 2
#1
05/22/2004 (7:18 am)
In the script you can call

%player.getTransform();

returns a string containing the player pos and lot's of other stuff. the Pos are the first 3 numbers. you can access them with the getword ( i'm not sure of the sintax ) command.

you won't find getTransform() in player.cc cause it's inherited from other class ( like shapebase)

HTH

Bruno
#2
05/24/2004 (11:22 am)
GetTransform() returns a matrix that I could get the data from but isn't there a %player.getPosition() I could call?
I'm trying to put this in the metrics.cs file like so but so far I can't get it to work, seems I havent thrown enough $ or % at it

function playerposMetricsCallback()
{
Point3F ppos;

ppos = $Player::getPosition();
return " POSITION: x=" @ %ppos.x @ " y=" @ %ppos.y @ " z=" @ %ppos.z;
}

Anyone know how to set this up?
#3
05/24/2004 (11:36 am)
To get pieces of data in script you have to use getWord(). So x = getWord(0, ppos), y = getWord(1, ppos), z = getWord(2, ppos).
#4
05/24/2004 (2:24 pm)
I've tried that and 100 other things.
first of all I guess I need to know if this is correct syntax

Point3F ppos;
ppos = $Player::getPosition();

I tried using %player.getPosition() just not sure of the way it works.

the data returned by getPosition() should be 3 floats not 3 words so how hould getWord() work.

The only thing this prints is x=1 y=2 z =3

I have to believe this is easy to do.
#5
05/24/2004 (2:56 pm)
If you are wanting the x y and z coordinates for a player on a map, then

%pos = %player.getPosition();


should work just fine.


If you are trying to define the player as a global to do testing, then you would use...


%pos = $Player.getPosition();


instead, but it would work the same.



If you want to extract the position from a transform, you would do this....

// FIRST GET YOUR TRANSFORM

%trans = %player.getTransform();

// OR

%trans = $Player.getTransform(); 


// THEN


%x = getWord(%trans, 0);
%y = getWord(%trans, 1);
%z = getWord(%trans, 2);

Hope this helps. If you cannot get the proper figures from these examples, then you are passing the wrong object as a player. If you post an example of your work, I could look at that and see if there was a problem.
#6
05/24/2004 (3:00 pm)
The advice to you was toruqe script.

Looks like you want it from C++.

From C++ just call Point3F pos = obj->getPosition();
#7
05/24/2004 (7:37 pm)
Doesn't work either way. When I activate it the console streams error messages
This is the error
Unable to find object: '' attempting to call function 'getPosition'

function playerposMetricsCallback()
{
%ppos = $Player.getPosition();
// %ppos = %player.getPosition();

return " POSITION: x=" @ ppos.x @ " y=" @ ppos.y @ " z=" @ ppos.z;
}

This is in metrics(%expr)

case "location": %cb = "playerposMetricsCallback()";
#8
05/24/2004 (9:57 pm)
If your typeing it into the console you need to swap %player with the players actual ID number , if you go to the editor you can see the ID number of objects.

example consol comand

1043.getPosition();
#9
05/25/2004 (1:19 am)
That function is not a natural function in the metrics.cs file. It's also running on the client side. The only way it would begin to work is if you are running the client and server together, and even then, the syntax is incorrect.


function playerposMetricsCallback()
{
%ppos = $Player.getPosition();
// %ppos = %player.getPosition();
//
//
// For starters you need to use the % sign on your variable as defined above.
// also, %poss.x and %poss.y haven't been defined either and would not be
// legitimate variables anyway.
//
//
return " POSITION: x=" @ %ppos.x @ " y=" @ %ppos.y @ " z=" @ %ppos.z;

}



Quote:Unable to find object: '' attempting to call function 'getPosition'

That's because %player or $player are not being defined correctly. You can't just pull them out of thin air.

Since you are using this function...

[function playerposMetricsCallback()

and you are not sending the player variable to the function, the only way you could get a correct position would be to define the player to $Player and use...

%ppos = $Player.getPosition();
#10
05/25/2004 (4:40 am)
I was trying to call it with a keybind so I could just pop it up when I needed a location. Well so far everyone has told me how not to do it can someone tell me how to print the player posision on the display? I can do this with one call in directx and it's so very simple. Could someone supply me with the code to print the player position on the display and update as the player moves? My only alternative is to get into the *.cc code and write an engine function in the render loop I would like to keep things pure script if possible. Thanks for any help...
#11
05/25/2004 (5:18 am)
You cannot, I repeat, cannot call %ppos.x %ppos.y or %ppos.z! The variable doesn't contain member fields. It is just a string containing floats separated by spaces. Thus, calling getWord is just splitting the string at spaces... getWord doesn't get a 32-bit int, it get's a word -- an actual word (of sorts) since words are split by spaces in sentences.

So, if the string looks like this (raw): "236.112 183.245 0.000"

Then using getWord will split it up at the spaces. Index zero being "236.112" etc. And since Torque script is not type-specific then it will be handled as whatever you want (a float, a string, a boolean...)

- Brett
#12
05/25/2004 (6:00 am)
So if I declare like this?
Point3F ppos;

then ppos.x works?

How would you print the player posision on the display?
#13
05/25/2004 (6:11 am)
Oh god this thread is just a huge mess. I think Howard is even more confused than when he started and I can't blame him.

First, the MAIN Problem you are having is that the system doesn't know what $Player is. There is no such thing in default Torque. You will need to save the player variable to a global to work with. To do that, go into (Assuming you are workign with starter.fps) the file:
starter.fps/server/scripts/game.cs

Look for the function:
function GameConnection::onClientEnterGame(%this)

Inside that you will see a line like this:
%this.spawnPlayer();

Right after that add:
$player = %this.player;

Now you have the player's variable stored off as a global.

Now the function you want will look like this (This is in TorqueScript):
function playerposMetricsCallback()
{
	%ppos = $player::getPosition();
	%x = getWord(%ppos, 0);
	%y = getWord(%ppos, 1);
	%z = getWord(%ppos, 2);
	
	return " POSITION: x=" @ %x @ " y=" @ %y @ " z=" @ %z;
}
#14
05/25/2004 (6:13 am)
GetPosition returns a STRING containing each coord seperated by spaces. For example:
"10 20 5"

The getWord() function takes each word out. So getWord(%blah, 0) takes out the first word in the string, in this case "10".
#15
05/25/2004 (7:53 am)
Here is what I have
game.cs
function GameConnection::onClientEnterGame(%this)
{
[code]
%this.spawnPlayer();
$player = %this.player
}

metrics.cs
function playerposMetricsCallback()
{
%ppos = $player::getPosition();
%x = getWord(%ppos,0);
%y = getWord(%ppos,1);
%z = getWord(%ppos,2);
return \\\" POSITION: x=\\\" @ %x @ \\\" y=\\\" @ %y @ \\\" z=\\\" @ %z;
}


And the result on the display
prints POSITION x=y=z=

console.log

common/client/metrics.cs (122): Unable to find object: '' attempting to call function 'getPosition'

Seems the metrics.cs file has been activated before the game.cs file and therefore has no forward reference for $player would be my guess.
#16
05/25/2004 (8:40 am)
Doh typo in my code. I simply copied your code and modified it, and I didin't notice the bug you had.

It should be:
%ppos = $player.getPosition();

NOT player::getPositon.

Function calls are object.method
#17
05/25/2004 (9:36 am)
I had already assumed that possibility and changed it, still no go.

I think I've use every iteration of %$ :: . in every combination. Still seems like a forward reference problem.

If I put a callback in the game.cs file and tagged it after I spawn the player would that work? Not sure how the script knows whats a function and what is a callback.

The console.log tells me that the object player does not exist and therefore can't call a member function getPosition()
#18
05/25/2004 (9:49 am)
Howard, I stil don't know why are you calling getPosition() instead of getTransform().

In scripting there is no as variable typing. So functions return either strings or numbers. If you want a a function to return a structure, you'll need to a) create a class of that structure, b) create an object of that class, c) return the handle of that object. All this takes processing time, which you don't want to loose when creating a game.

Just try Jonh's algorithm using $player.getTransform() and see if it works for you.

HTH

Bruno
#19
05/25/2004 (10:34 am)
Well I dont get the error now but it doesn't print anything just shows a white box in the upper left of the display.
#20
05/25/2004 (11:36 am)
Try to send the result to the console using

echo(" whatever you want to echo");

to see if it's working before trying to print it in the game window.
Page «Previous 1 2