Game Development Community

ActionMap typo in game.cs?!?

by Max Kielland · in Torque Game Builder · 01/31/2013 (4:19 pm) · 7 replies

I tried to bind some keys in the main.cs function setupKeybinds() but I never got it to worked how hard I tried all examples I found on the net.

This is the default code from a fresh project:
//---------------------------------------------------------------------------------------------
// setupKeybinds
// Bind keys to actions here..
//---------------------------------------------------------------------------------------------
function setupKeybinds()
{
   new ActionMap(moveMap);
   //moveMap.bind("keyboard", "a", "doAction", "Action Description");
}

So I started to search for teh place where the moveMap is pushed to see if it got pushed at all and founs this in the default generated game.cs file:

//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level) {
    
  Canvas.setContent(mainScreenGui);
  Canvas.setCursor(DefaultCursor);
   
  new ActionMap(moveMap);   // <------- WTF is this?!?
  moveMap.push();
   
  $enableDirectInput = true;
  activateDirectInput();
  enableJoystick();

  %graph = sceneWindow2D.loadLevel(%level);
}

When I removed the new ActionMap(moveMap) my key bindings worked.
Can someone confirm if this is a typo in the source or if it is suposed to be something else (like a global map definition)?

The code is generated from an empty project in Torque2D 1.7.6

#1
01/31/2013 (4:33 pm)
I always thought it was a mini-example of how to bind keys and not an actual keybinding that was commented out - since those last two parameters are supposed to be function names. The other new actionMap() call was probably dropped in there because they never actually called setupKeybinds().

At the point with your // <--- wtf comment is where you should instead call that setupKeybinds() function and then put your keybinds in that function so that when you push that action map it is useful.
#2
01/31/2013 (4:57 pm)
After some more digging I found this function being called from initializeCommon()

function loadKeybindings()
{
   $keybindCount = 0;
   // Load up the active projects keybinds.
   if(isFunction("setupKeybinds"))
      setupKeybinds();
}

So I guess you are kind'a right that the whole setupKeybinds() should be commented out if you want to define the bindings in game.cs.

However I have trouble to bind the mouse buttons:

new ActionMap(moveMap);
  moveMap.bind(mouse,"button0", keyTest);
  moveMap.bind(mouse,"button1", keyTest);
  moveMap.bind(mouse,"button2", keyTest);
  moveMap.bind(keyboard, "a", keyTest);
  moveMap.push();

function keyTest(%val) {

  echo("KeyBind hit: "@%val);
}

When I press "a" the keyTest function is called, but never for any of the mouse buttons.
#3
02/01/2013 (6:40 am)
You have to put a zero after the mouse like "mouse0". If you were slightly crazy and had multiple mice attached you can access each mouse separately by changing the number after the mouse keyword.
#4
02/01/2013 (7:08 am)
I don't think you can overload the function like that. Last time I checked it would take the last written declaration (in this case the keyboard) to run keyTest. Function overloading isn't something that is straight forward in Torque script.
I think.. you can try it this way:
moveMap.bind(mouse0, "button0", keyTest1);
moveMap.bind(mouse0, "button1", keyTest2);
moveMap.bind(mouse0, "button2", keyTest3);
moveMap.bind(keyboard, "a", keyTest);

function keyTest1(%val)
{
  keyTest(%val);
}  
function keyTest2(%val)
{
  keyTest(%val);
} 
function keyTest3(%val)
{
  keyTest(%val);
} 
function keyTest(%val)
{
  echo("KeyBind hit: "@%val);
}
#5
02/01/2013 (7:15 am)
I have three pointing devices attached to my computer, one of which is a 3D "mouse".
#6
02/01/2013 (7:58 am)
I have been reading high and low in the "documentation" and: mouse "mouse" mouse0 and "mouse0" are all the same. I have tried all of the variations.

I first started with only one mouse bind, then I added the keyboard to see if it was working at all. After some hours of reading it seems like the mouse button bind broke somewhere down the line.

Some post say that I have to turn off the cursor to get the mouse buttons to bind, and then only works with the GlobalActionMap. So I tried.

new ActionMap(moveMap);
  moveMap.bind("mouse","zaxis",  "onScrollWheel");
  GlobalActionMap.bind("mouse","button2","keyTest"      );
  moveMap.push();
  
  $enableDirectInput = true;
  activateDirectInput();
  enableMouse();

  %graph = sceneWindow2D.loadLevel(%level);
  mainScreenGui.noCursor = true;

First of all the cursor is not hidden to begin with and the mouse button is not reacting. The mouse zaxis works fine!

After I have pushed and popped another gui the mouse pointer dissapear and mouse button2 will respond. But I need to see the mouse pointer. Not much of a point to not see where you are clicking, huh?!?

The enableMouse() also cause strange behaviour initially with one game mouse pointer and one desktop pointer at the same time.

Does someone have a working example of detecting mouse button2 in TGB 1.7.6?!?
I saw that v1.8.0 just got released, but several ppl have found severe problems with it already.

I really would appreciate some help on this issue. Just to get confirmed that "no it's not possible" would also be helpful, so I don't spend another day try to fix something that can't be done.

Thank you all for your patient with me... :P
#7
02/02/2013 (4:02 pm)
Okay I have solved the middle mouse button mystery, at least in one way of access.

The code to link the middle mouse button events in t2dSceneWindow is just missing!?! The whole structure to propagate the mouse middle button events are there but only as virtual functions doing nothing. It took me less than 5 min. to implement them as script events.

I make a separate post on how to update the source.