Make stuff follow the mouse
by Hjalmar · in Torque 3D Beginner · 08/05/2014 (12:13 pm) · 7 replies
I'm trying to make a game where it is possible for the player to place buildings. I've completed the RTS Prototype tutorial but need some pointers on how to make the building visible before being placed. I suppose I could create the TSStatic object, pass it to some function that keeps track of it until some onMouseDown event and change its location by constantly checking the mouse pointers location until the event. But is there a neater way to do it?
#2
#edit: Were is onMouseDragged declared? Can't find it in gameTSCtrl.cpp
08/06/2014 (1:40 am)
Ok, thanks. An other problem, how do I move a StaticShape() ? It has a getter for position but no setPosition, is position public so I can get it with "$obj.position" ? Or do I have to implement a set:er?#edit: Were is onMouseDragged declared? Can't find it in gameTSCtrl.cpp
#3
08/06/2014 (6:50 am)
GUI control mouse callbacks are declared in GuiControl (engine/gui/core/guiControl.h around line 597). It is left to the child controls to implement it. See engine/T3D/guiObjectView.cpp @ line 278 for an example implementation (this control uses left mouse drag to move the camera, right mouse drag to orbit around the focus object).
#4
As Richard has explained, take a peek at GuiControl.h for a sample on how the engine works with mouse events. You'll just want to make one that uses the advanceTime method, or whatever method that is. Just store the current mouse position every frame and throw it in a variable accessible by the gui.
08/06/2014 (5:13 pm)
I'd just create a new update method in the C++ to accomplish this.As Richard has explained, take a peek at GuiControl.h for a sample on how the engine works with mouse events. You'll just want to make one that uses the advanceTime method, or whatever method that is. Just store the current mouse position every frame and throw it in a variable accessible by the gui.
#5
08/06/2014 (5:41 pm)
The lack of setPosition is stupid. For now, you need to use setTransform, but you have to pass the whole transform with rotation as well. Something like this:%newPos = "1 2 3"; // or whatever %trans = %obj.getTransform(); %rot = getWords(%trans, 3, 6); %newTrans = %newPos SPC %rot; %obj.setTransform(%newTrans);
#6
08/06/2014 (6:16 pm)
Wait - you mean%obj.position = "1 2 3";doesn't work? I was pretty sure it worked...
#7
In playGui.cs:
And moveCurrentBuilding just does $currentBuilding.position=%pos;
08/06/2014 (11:59 pm)
Great! Now it works! I added this to gameTSCtrl:void GameTSCTRL::onMouseDragged(const GuiEvent &evt)
{
Parent::onMouseDragged(evt);
if( isMethod( "onMouseDragged" ) )
makeScriptCall( "onMouseDragged", evt );
}In playGui.cs:
function PlayGui::onMouseDragged(%this, %pos, %start, %ray)
{
if(%this.placingBuilding)
commandToServer('moveCurrentBuilding',%pos, %start, %ray);
}And moveCurrentBuilding just does $currentBuilding.position=%pos;
Torque Owner Richard Ranft
Roostertail Games