Game Development Community

Current mouse information?

by J Sears · in Torque Game Builder · 11/14/2006 (2:00 pm) · 8 replies

I've tried the tutorials for grabbing things with the mouse to death and tried the mini tutorial which says all you have to do is load it in as a resource and neither of them do a single thing.
Does anyone have a current how to on grabbing items with a mouse??

#1
11/14/2006 (2:31 pm)
As a simple solution, I'd make the object respond to mouseDrag and update it's position to the location of the mouse every time. That's a start, and then you can add stuff to make it robust.

Are you at least getting mouse input to objects? If you're not, remember to set the object to use mouse events and the sceneWindow to send mouse events to objects.
#2
11/14/2006 (4:19 pm)
As I was building through the first section of objectm ouse tutorials and using the echo command it would echo back mouse moving mouse down etc etc, but as I continued to build the code it would perform the action so I completly copied the code and tried that and nothing so I started building one step at a time form others code and this is where I'm at

So I decided to start small and make a mouse click teleport an object basically and that worked, so then I upgraded it to check for selectable objects under the mouse and if there is a selectable one select that and teleport it on mouse down, so now I'm trying to get a selected object on down click and store it so it can teleport on the up click (eventually I want to have it store it on a downclick so then you can move the mouse and next time you downclick it will put it there) But I can't get it to teleport on the mouseUp here's the code

function sceneWindow2D::onMouseDown( %this, %mod, %worldPos, %mouseClicks )
{
   //lets get a list of all the objects at the clicked point in the t2dScene
   %objList = t2dScene.pickPoint(%worldPos); 
   //lets get a count of how many objects in the list 
   %objCount = getWordCount(%objList); 
   //we will start looping through the list 
   for(%i=0;%i<%objCount;%i++)
   { 
      //grabing the entry corresponding to the loop 
      %obj = getWord(%objList, %i); 
      //if we find an object in the list that "isSelectable = true"
       if(%obj.isSelectable)
       { 
          //we toggle a value so we know we found an object that "isSelectable" 
          %selected = true; 
          //we kick out of the loop
          %i = %objCount;
       }
     } 
     //if we found an "isSelectable" object 
     if(%selected)
     { 
        //we then store that object as the selectedObj 
       %this.selectedObj = %obj;
       %this.selectedObj.dismount();
       %this.selectedObj.mount(pad2,0,0,0,false,false,false,true);
     } 
}

that part works so I tried to use selectedObj.dismount() and the mount line in mouseUp, but no luck so I figured it couldn't be local so I tried using a $ in from of this.selectedObj = %obj; but that didn't work. Do you have to define a global before ever using it is that my problem?
I want to eventually have like I said above able to click to mark the object and then reclick to place it as well as have a drag function to drag the obj while you hold the mouse down
#3
11/14/2006 (4:28 pm)
I assumed you were using object-specific callbacks, not sceneWindow ones. Well, can you post the onMouseUp code too? It should be pretty easy to see why it isn't working. ;) In theory.
#4
11/14/2006 (5:12 pm)
Well I wanted to start off as easy as possible and I couldn't see any advantage of object specific callbacks over sceneWindow, is there one?

I've tried a few things for onMouseUp nothing has worked

function sceneWindow2D::onMouseUp(%this, %mod, %worldPos, %mouseClicks)
{
   if(%this.objectSelected)
   {
    //%obj = %this.selectedObj;    
    %this.selectedObj.dismount();
    %this.selectedObj.mount(pad2,0,0,0,false,false,false,true);
   } 
}

that is my current attmept I've tried without the if statement, I tried it without %this, tried making card = %obj in the mousedown and then tried using card.dismount(); for onmouse up (I removed the dismount and remount from the mousedown for all of this of course) so now when I click and release nothing happens change code back to the mousedown above and works like a charm.

And thinking about it the sceneWindow seems to at least my limited knowledge the way to go for what I'm trying, I'm attempting to recreate a FreeCell game so there will be stacks of cards have to decide where the person clicked and eventually where he's trying to move it to and if it's valid etc etc.
thanks for you help so far
#5
11/14/2006 (5:46 pm)
Well, I'd say that it might make a little more sense (if it's just a card game and only one or a few types of objects will be draggable) to put the callbacks on the objects themselves, simply because then the timer functions or schedules to make the object stay with the cursor could be on the object itself. You could do it the current way just fine (perhaps adding selected objects to a simset that gets checked every so often to update those objects' positions relative to the mouse.

Anyway, I put in your code, got it working and edited it a bit. All the callbacks worked fine (though I did change the %this.selectedObj to just be a global variable because I'm not sure if the %this from mouse callbacks will be consistent because I have no idea what object it's returning when you're using the sceneWindow.

The mouseDown code looks fine (I'd make some changes, but it works anyway ;), but to enable dragging you have to have some sort of update function that moves the selected object to the mouse. The logic of the whole thing should look like this:

onMouseDown -> selectable object found, remember it in a global variable, start a schedule that updates that object very frequently, moving it to the mouse position
onMouseUp -> stop the schedule

There are lots of little things there, but nothing very hard. I'd also recommend echo'ing out a lot of stuff to make sure the functions are performing as you expect. Echoing out the object you've selected and then the one it thinks it should drop would be very helpful, I think.

Hope that helps a bit! If it doesn't, post back here and I'll probably just write a simple drag-and-drop snippet.

Also, I'm not really sure what you're doing with the mounting (that might even be your problem). You aren't setting it to sendToMount or moving the object at all, so it's not going to do anything.
#6
11/14/2006 (6:27 pm)
Strange I retried again after yous aid it worked with globals and it worked this time, I was pretty sure I tried it before thank you very much, now on to trying to get the dragging working, but why did you suggest doing draggign with mouse Up and mouse Down as opposed to mousedragged??
#7
11/14/2006 (6:59 pm)
It doesn't really matter in this case, I suppose. Dragging is probably easier anyway ;)
#8
11/14/2006 (7:31 pm)
Well I got the mouse to select an item on mouse down then let you drag the mouse while holding mouse down and when you let the mouse up it drops the item and the you can keep picking it up and move it around more, It seems simple now but since I had a problem with it I figured I'd post the result in case anyone else was having any issues with it

function sceneWindow2D::onMouseDown( %this, %mod, %worldPos, %mouseClicks )
{
   //lets get a list of all the objects at the clicked point in the t2dScene
   %objList = t2dScene.pickPoint(%worldPos); 
   //lets get a count of how many objects in the list 
   %objCount = getWordCount(%objList); 
   //we will start looping through the list 
   for(%i=0;%i<%objCount;%i++)
   { 
      //grabing the entry corresponding to the loop 
      %obj = getWord(%objList, %i); 
      //if we find an object in the list that "isSelectable = true"
       if(%obj.isSelectable)
       { 
          //we toggle a value so we know we found an object that "isSelectable" 
          %selected = true; 
          //we kick out of the loop
          %i = %objCount;
       }
     } 
     //if we found an "isSelectable" object 
     if(%selected)
     { 
        //we then store that object as the selectedObj 
         $selectedObj = %obj;
       
     } 
}

function sceneWindow2D::onMouseUp(%this, %mod, %worldPos, %mouseClicks)
{
}

function t2dSceneWindow::objectFollowCheck(%this)
{ 
   $selectedObj.dismount();
   %mousePos = %this.mousePos;
   $selectedObj.setPosition(%mousePos);
} 

function sceneWindow2D::onMouseDragged( %this, %mod, %worldPos, %mouseClicks )
{ 
   //lets store the mouses position 
  %this.mousePos = %worldPos; 
  %this.objectFollowCheck(); 
}

I put all this in mouse.cs and of course for anyone knew make sure you exec it in game.cs, thanks again for the help Tom. Now I will be working for my purposes on when MouseUp I check to see if the area is a "isMountable"area and make it so when I just click on a selectable object and then click again on a mountable object it will move it to that spot. Both these steps should be quick and easy and then onto making it specific for the card rules.