Updating Control Object with gameTSControl
by Jason "fireVein" Culwell · in Torque Game Engine · 03/25/2008 (6:52 pm) · 2 replies
Hi guys,
I'm trying to call a function on a control object from gameTSControl. Heres my code:
Any advice on the proper way to do this? The code never gets below the if statement with isServerObject().. If I take that out of the if statement, it will reach setAimLocation but the player will just start jittering, like he doesn't know what direction to look, but will always move in the same direction. Btw, used code from the target locking resource to add setAimLocation functionality to the player class. I had to tweak it to make it work properly, but it is tested and working fine.
Any help is greatly appreciated!
I'm trying to call a function on a control object from gameTSControl. Heres my code:
void GameTSCtrl::onMouseMove(const GuiEvent &evt)
{
if(gSnapLine)
return;
MatrixF mat;
Point3F vel;
if ( GameGetCameraTransform(&mat, &vel) )
{
Point3F pos;
mat.getColumn(3,&pos);
Point3F screenPoint((F32)evt.mousePoint.x, (F32)evt.mousePoint.y, 1.0f);
Point3F worldPoint;
if (unproject(screenPoint, &worldPoint)) {
Point3F vec = worldPoint - pos;
Point3F lStart, lEnd;
lStart = pos;
vec.normalizeSafe();
lEnd = pos + vec * 1000;
GameConnection* conn = GameConnection::getConnectionToServer();
if(!conn)
return;
Player* control = dynamic_cast<Player*>(conn->getControlObject());
if (!control || !control->isServerObject())
return;
RayInfo info;
control->castRay(lStart, lEnd, &info);
control->setAimLocation(info.point);
Con::errorf("update mouse");
}
}
}Any advice on the proper way to do this? The code never gets below the if statement with isServerObject().. If I take that out of the if statement, it will reach setAimLocation but the player will just start jittering, like he doesn't know what direction to look, but will always move in the same direction. Btw, used code from the target locking resource to add setAimLocation functionality to the player class. I had to tweak it to make it work properly, but it is tested and working fine.
Any help is greatly appreciated!
About the author
http://www.microdotproductions.com - I am a self taught programmer that has been hacking away at code for a little over 10 years now. I am a very passionate and persistent programmer, and gamer. I love challenges and problem solving.
#2
I still have to throttle it, because for some reason sometimes when I load a mission once it finishes loading - nothing happens. It will just sit there with the load bar on full and the game will appear to freeze. I found that it has something to do with gClientContainer... If I keep that throttle in there everything works fine... everytime. Plus If I feel the need to tweak how often the the player's aim gets updated I can do so(prolly for bandwidth reasons).
Special thanks to Pat Wilson for his article on TDN about ITickable.
03/26/2008 (9:27 pm)
I took a different approach to what I was trying to do. I added ITickable to GameTSCtrl. Every tick the mouse coords are grabbed, processed and passed onto script which then do a commandToServer to update the player's aim. This way I won't have to worry about other Gui controls that are on screen intercepting the onMouseMove method of GameTSCtrl(for instance the chat hud). Plus it solves issues with using onMouseMove in the first place. onMouseMove only got called... when the mouse was moved... which if the mouse wasn't moving the player would just run to where the position from the last mouse move was, and then turn around and run back towards it... and so on. Using ITickable solved that.void GameTSCtrl::processTick(void)
{
if(Platform::getRealMilliseconds() - mMouseUpdateMS < 32) //throttle update
return;
else
mMouseUpdateMS = Platform::getRealMilliseconds();
GuiCanvas *Root = getRoot();
Point2I mousePos = Point2I(0,0);
if(Root)
mousePos = Root->getCursorPos();
else
return;
GameConnection* conn = GameConnection::getConnectionToServer();
if(!conn)
return;
Player* control = dynamic_cast<Player*>(conn->getControlObject());
if (!control )
return;
MatrixF mat;
Point3F vel;
if ( GameGetCameraTransform(&mat, &vel) )
{
Point3F pos;
mat.getColumn(3,&pos);
Point3F screenPoint((F32)mousePos.x, (F32)mousePos.y, 1.0f);
Point3F worldPoint;
if (unproject(screenPoint, &worldPoint)) {
Point3F vec = worldPoint - pos;
Point3F lStart, lEnd;
lStart = pos;
vec.normalizeSafe();
lEnd = pos + (vec * 1000);
RayInfo info;
gClientContainer.castRay(lStart, lEnd, TerrainObjectType | PlayerObjectType | StaticShapeObjectType | InteriorObjectType, &info);
char mousePosStr[128];
dSprintf( mousePosStr, 128, "%f %f %f", info.point.x, info.point.y, info.point.z );
Con::executef(this,"MouseMove",mousePosStr); //pass to script so it can be sent server side
control->setAimLocation(info.point); //client side only, makes character orientation smoother
//Con::printf("%i %i", mousePos.x, mousePos.y);
//Con::printf("%f %f %f", info.point.x, info.point.y, info.point.z);
}
}
}I still have to throttle it, because for some reason sometimes when I load a mission once it finishes loading - nothing happens. It will just sit there with the load bar on full and the game will appear to freeze. I found that it has something to do with gClientContainer... If I keep that throttle in there everything works fine... everytime. Plus If I feel the need to tweak how often the the player's aim gets updated I can do so(prolly for bandwidth reasons).
Special thanks to Pat Wilson for his article on TDN about ITickable.
Torque Owner Jason "fireVein" Culwell
void GameTSCtrl::onMouseMove(const GuiEvent &evt) { if(gSnapLine) return; MatrixF mat; Point3F vel; if ( GameGetCameraTransform(&mat, &vel) ) { Point3F pos; mat.getColumn(3,&pos); Point3F screenPoint((F32)evt.mousePoint.x, (F32)evt.mousePoint.y, 1.0f); Point3F worldPoint; if (unproject(screenPoint, &worldPoint)) { Point3F vec = worldPoint - pos; Point3F lStart, lEnd; lStart = pos; vec.normalizeSafe(); lEnd = pos + vec * 1000; GameConnection* conn = GameConnection::getConnectionToServer(); if(!conn) return; Player* control = dynamic_cast<Player*>(conn->getControlObject()); if (!control ) return; RayInfo info; gClientContainer.castRay(lStart, lEnd, TerrainObjectType | PlayerObjectType | StaticShapeObjectType | InteriorObjectType, &info); char mousePosStr[128]; dSprintf( mousePosStr, 128, "%f %f %f", info.point.x, info.point.y, info.point.z ); Con::executef(this,"MouseMove",mousePosStr); } } }function PlayGui::MouseMove(%this,%pos) { commandToServer('UpdateAim',%pos); } --- function serverCmdUpdateAim(%client,%pos) { %client.getControlObject().setAimLocation(%pos); }