### get player position in torque c++

There are no stupid questions, just stupid answers.

#### get player position in torque c++

subabrain
Posts: 190
Joined: Sat Aug 20, 2016 4:58 pm

Hello Friends,

i tried to look for a Function to get the Position from the Player - i know the thing in Torquescript but i need it for the c++ code of Torque 3D.

would be nice if you could tell me

thx and gn8!
Robert

#### Re: get player position in torque c++

LoLJester
Posts: 65
Joined: Thu Aug 13, 2015 5:58 pm

getPosition()

#### Re: get player position in torque c++

subabrain
Posts: 190
Joined: Sat Aug 20, 2016 4:58 pm

hi,

yes i tried this already - i think its from the player class?

So i tried this with included the player.h :

Player *pl = new Player();
Point3F pos = pl->getPosition();

but its not the active position it always goes to point 0.000 0.000 100.000 :/

maybe you know why?

thx and greetz!
Robert

#### Re: get player position in torque c++

irei1as
Posts: 76
Joined: Fri Feb 27, 2015 5:13 pm
Ah, the Player class it isn't only for the active game player. It's just an object that moves like a Player and it can be used as the control object.
Maybe something like "actor" could have been more appropriate.

When you use "new" you create a new instance that is independent of any other Player object.
You are using getPosition() correctly but as you're using it in a new object you just get the initial value of the position as defined in Player::Player().

What you need is to first find the pointer of the player character and then use getPosition() on that.
For that you can use getControlObject with the GameConnection

Code: Select all

GameConnection* connection = GameConnection::getConnectionToServer(); if( !connection ) { //fail with the game. Maybe you haven't started the level. Return an error. } Player* playerObject = dynamic_cast< Player* >( connection->getControlObject() ); if( !playerObject) { //fail. Maybe the control object is a camera or a vehicle not parented to Player } Point3F pos = playerObject->getPosition(); 
If you add that I think you may need as extra
#include "T3D/gameBase/gameConnection.h"

#### Re: get player position in torque c++

subabrain
Posts: 190
Joined: Sat Aug 20, 2016 4:58 pm

Hey irei1as,

thanks a lot for your help - it works perfect!

Greetings
Robert

#### Who is online

Users browsing this forum: No registered users and 5 guests