ActionMap
ActionMaps assign platform input events to console commands.
Public Functions
bool
bind(string device, string action, string command)
Associates a function to an input event.
bool
bind(string device, string action, string flag, string deadZone, string scale, string command)
Associates a function and input parameters to an input event.
bool
bindCmd(string device, string action, string makeCmd, string breakCmd)
Associates a make command and optional break command to a specified input device action.
void
bindContext(string device, string action, string holdFunction, string tapFunction, uint holdTime)
actionMap.bindCmd( device, action, holdFunction, tapFunction, holdTime)
void
bindHold(string device, string action, string holdFunction, bool returnHoldTime)
actionMap.bindCmd( device, action, holdFunction, returnHoldTime)
bool
bindObj(string device, string action, string command, SimObjectID object)
Associates a function to an input event for a specified class or object.
bool
bindObj(string device, string action, string flag, string deadZone, string scale, string command, SimObjectID object)
Associates a function to an input event for a specified class or object.
string
getBinding(string command)
Gets the ActionMap binding for the specified command.
string
getCommand(string device, string action)
Gets ActionMap command for the device and action.
string
getDeadZone(string device, string action)
Gets the Dead zone for the specified device and action.
float
getScale(string device, string action)
Get any scaling on the specified device and action.
bool
isInverted(string device, string action)
Determines if the specified device and action is inverted.
bool
unbind(string device, string action)
Removes the binding on an input device and action.
bool
unbindObj(string device, string action, string obj)
Remove any object-binding on an input device and action.
Detailed Description
ActionMaps assign platform input events to console commands.
Any platform input event can be bound in a single, generic way. In theory, the game doesn't need to know if the event came from the keyboard, mouse, joystick or some other input device. This allows users of the game to map keys and actions according to their own preferences. Game action maps are arranged in a stack for processing so individual parts of the game can define specific actions. For example, when the player jumps into a vehicle it could push a vehicle action map and pop the default player action map.
Creating an ActionMap
The input system allows for the creation of multiple ActionMaps, so long as they have unique names and do not already exist. It's a simple three step process.
The following is an example of how to create a new ActionMap:
if ( isObject( moveMap ) ) moveMap.delete(); new ActionMap(moveMap);
Binding Functions
Once you have created an ActionMap, you can start binding functionality to events. Currently, Torque 3D supports the following devices out of the box
Mouse
Keyboard
Joystick/Gamepad
Xbox 360 Controller
The two most commonly used binding methods are bind() and bindCmd(). Both are similar in that they will bind functionality to a device and event, but different in how the event is interpreted. With bind(), you specify a device, action to bind, then a function to be called when the event happens.
// Simple function that prints to console // %val - Sent by the device letting the user know // if an input was pressed (true) or released (false) function testInput(%val) { if(%val) echo("Key is down"); else echo("Key was released"); } // Bind the 'K' key to the testInput function moveMap.bind(keyboard, "k", testInput);
bindCmd is an alternative method for binding commands. This function is similar to bind(), except two functions are set to be called when the event is processed.
One will be called when the event is activated (input down), while the other is activated when the event is broken (input release). When using bindCmd(), pass the functions as strings rather than the function names.
// Print to the console when the spacebar is pressed function onSpaceDown() { echo("Space bar down!"); } // Print to the console when the spacebar is released function onSpaceUp() { echo("Space bar up!"); } // Bind the commands onSpaceDown and onSpaceUp to spacebar events moveMap.bindCmd(keyboard, "space", "onSpaceDown();", "onSpaceUp();");
Switching ActionMaps
Let's say you want to have different ActionMaps activated based on game play situations. A classic example would be first person shooter controls and racing controls in the same game. On foot, spacebar may cause your player to jump. In a vehicle, it may cause some kind of "turbo charge". You simply need to push/pop the ActionMaps appropriately:
First, create two separate ActionMaps:
// Create the two ActionMaps if ( isObject( moveMap ) ) moveMap.delete(); new ActionMap(moveMap); if ( isObject( carMap ) ) carMap.delete(); new ActionMap(carMap);
Next, create the two separate functions. Both will be bound to spacebar, but not the same ActionMap:
// Print to the console the player is jumping function playerJump(%val) { if(%val) echo("Player jumping!"); } // Print to the console the vehicle is charging function turboCharge() { if(%val) echo("Vehicle turbo charging!"); }
You are now ready to bind functions to your ActionMaps' devices:
// Bind the spacebar to the playerJump function // when moveMap is the active ActionMap moveMap.bind(keyboard, "space", playerJump); // Bind the spacebar to the turboCharge function // when carMap is the active ActionMap carMap.bind(keyboard, "space", turboCharge);Finally, you can use the push() and pop() commands on each ActionMap to toggle activation. To activate an ActionMap, use push():
// Make moveMap the active action map // You should now be able to activate playerJump with spacebar moveMap.push();
To switch ActionMaps, first pop() the old one. Then you can push() the new one:
// Deactivate moveMap moveMap.pop(); // Activate carMap carMap.push();
Public Functions
bind(string device, string action, string command)
Associates a function to an input event.
When the input event is raised, the specified function will be called.
Parameters:
device | The input device, such as mouse or keyboard. |
action | The input event, such as space, button0, etc. |
command | The function to bind to the action. Function must have a single boolean argument. |
True if the binding was successful, false if the device was unknown or description failed.
// Simple function that prints to console // %val - Sent by the device letting the user know // if an input was pressed (true) or released (false) function testInput(%val) { if(%val) echo("Key is down"); else echo("Key was released"); } // Bind the 'K' key to the testInput function moveMap.bind(keyboard, k, testInput);
bind(string device, string action, string flag, string deadZone, string scale, string command)
Associates a function and input parameters to an input event.
When the input event is raised, the specified function will be called. Modifier flags may be specified to process dead zones, input inversion, and more.
Valid modifier flags:
R - Input is Ranged.
S - Input is Scaled.
I - Input is inverted.
D - Dead zone is present.
N - Input should be re-fit to a non-linear scale.
Parameters:
device | The input device, such as mouse or keyboard. |
action | The input event, such as space, button0, etc. |
flag | Modifier flag assigned during binding, letting event know there are additional parameters to consider. |
deadZone | Restricted region in which device motion will not be acknowledged. |
scale | Modifies the deadZone region. |
command | The function bound to the action. Must take in a single argument. |
True if the binding was successful, false if the device was unknown or description failed.
// Simple function that adjusts the pitch of the camera based on the mouse's movement along the X axis. function testPitch(%val) { %pitchAdj = getMouseAdjustAmount(%val); $mvPitch += %pitchAdj; } // Bind the mouse's X axis to the testPitch function // DI is flagged, meaning input is inverted and has a deadzone %this.bind( mouse, "xaxis", "DI", "-0.23 0.23", testPitch );
bindCmd(string device, string action, string makeCmd, string breakCmd)
Associates a make command and optional break command to a specified input device action.
Must include parenthesis and semicolon in the make and break command strings.
Parameters:
device | The device to bind to. Can be a keyboard, mouse, joystick or gamepad. |
action | The device action to bind to. The action is dependant upon the device. Specify a key for keyboards. |
makeCmd | The command to execute when the device/action is made. |
breakCmd | [optional] The command to execute when the device or action is unmade. |
True the bind was successful, false if the device was unknown or description failed.
// Print to the console when the spacebar is pressed function onSpaceDown() { echo("Space bar down!"); } // Print to the console when the spacebar is released function onSpaceUp() { echo("Space bar up!"); } // Bind the commands onSpaceDown() and onSpaceUp() to spacebar events moveMap.bindCmd(keyboard, "space", "onSpaceDown();", "onSpaceUp();");
bindContext(string device, string action, string holdFunction, string tapFunction, uint holdTime)
actionMap.bindCmd( device, action, holdFunction, tapFunction, holdTime)
bindHold(string device, string action, string holdFunction, bool returnHoldTime)
actionMap.bindCmd( device, action, holdFunction, returnHoldTime)
bindObj(string device, string action, string command, SimObjectID object)
Associates a function to an input event for a specified class or object.
You must specify a device, the action to bind, a function, and an object to be called when the event happens. The function specified must be set to receive a single boolean value passed.
Parameters:
device | The input device, such as mouse or keyboard. |
action | The input event, such as space, button0, etc. |
command | The function bound to the action. |
object | The object or class bound to the action. |
True if the binding was successful, false if the device was unknown or description failed.
moveMap.bindObj(keyboard, "numpad1", "rangeChange", %player);
bindObj(string device, string action, string flag, string deadZone, string scale, string command, SimObjectID object)
Associates a function to an input event for a specified class or object.
You must specify a device, the action to bind, a function, and an object to be called when the event happens. The function specified must be set to receive a single boolean value passed. Modifier flags may be specified to process dead zones, input inversion, and more.
Valid modifier flags:
R - Input is Ranged.
S - Input is Scaled.
I - Input is inverted.
D - Dead zone is present.
N - Input should be re-fit to a non-linear scale.
Parameters:
device | The input device, such as mouse or keyboard. |
action | The input event, such as space, button0, etc. |
flag | Modifier flag assigned during binding, letting event know there are additional parameters to consider. |
deadZone | [Required only when flag is set] Restricted region in which device motion will not be acknowledged. |
scale | [Required only when flag is set] Modifies the deadZone region. |
command | The function bound to the action. |
object | The object or class bound to the action. |
True if the binding was successful, false if the device was unknown or description failed.
// Bind the mouse's movement along the x-axis to the testInput function of the Player class // DSI is flagged, meaning input is inverted, has scale and has a deadzone %this.bindObj( mouse, "xaxis", "DSI", %deadZone, %scale, "testInput", %player );
getBinding(string command)
Gets the ActionMap binding for the specified command.
Use getField() on the return value to get the device and action of the binding. Parameters:
command | The function to search bindings for. |
The binding against the specified command. Returns an empty string("") if a binding wasn't found.
// Find what the function "jump()" is bound to in moveMap %bind = moveMap.getBinding( "jump" ); if ( %bind !$= "" ) { // Find out what device is used in the binding %device = getField( %bind, 0 ); // Find out what action (such as a key) is used in the binding %action = getField( %bind, 1 ); }
getCommand(string device, string action)
Gets ActionMap command for the device and action.
Parameters:
device | The device that was bound. Can be a keyboard, mouse, joystick or a gamepad. |
action | The device action that was bound. The action is dependant upon the device. Specify a key for keyboards. |
The command against the specified device and action.
// Find what function is bound to a device's action // In this example, "jump()" was assigned to the space key in another script %command = moveMap.getCommand("keyboard", "space"); // Should print "jump" in the console echo(%command)
getDeadZone(string device, string action)
Gets the Dead zone for the specified device and action.
Parameters:
device | The device that was bound. Can be a keyboard, mouse, joystick or a gamepad. |
action | The device action that was bound. The action is dependant upon the device. Specify a key for keyboards. |
The dead zone for the specified device and action. Returns "0 0" if there is no dead zone or an empty string("") if the mapping was not found.
%deadZone = moveMap.getDeadZone( "gamepad", "thumbrx");
getScale(string device, string action)
Get any scaling on the specified device and action.
Parameters:
device | The device that was bound. Can be keyboard, mouse, joystick or gamepad. |
action | The device action that was bound. The action is dependant upon the device. Specify a key for keyboards. |
Any scaling applied to the specified device and action.
%scale = %moveMap.getScale( "gamepad", "thumbrx");
isInverted(string device, string action)
Determines if the specified device and action is inverted.
Should only be used for scrolling devices or gamepad/joystick axes.Parameters:
device | The device that was bound. Can be a keyboard, mouse, joystick or a gamepad. |
action | The device action that was bound. The action is dependant upon the device. Specify a key for keyboards. |
True if the specified device and action is inverted.
%if ( moveMap.isInverted( "mouse", "xaxis")) echo("Mouse's xAxis is inverted");
pop()
Pop the ActionMap off the ActionMap stack.
Deactivates an ActionMap and removes it from the @ActionMap stack.
// Deactivate moveMap moveMap.pop();
push()
Push the ActionMap onto the ActionMap stack.
Activates an ActionMap and placees it at the top of the ActionMap stack.
// Make moveMap the active action map moveMap.push();
save(string fileName, bool append)
Saves the ActionMap to a file or dumps it to the console.
Parameters:
fileName | The file path to save the ActionMap to. If a filename is not specified the ActionMap will be dumped to the console. |
append | Whether to write the ActionMap at the end of the file or overwrite it. // Write out the actionmap into the config.cs file moveMap.save( "scripts/client/config.cs" ); |
unbind(string device, string action)
Removes the binding on an input device and action.
Parameters:
device | The device to unbind from. Can be a keyboard, mouse, joystick or a gamepad. |
action | The device action to unbind from. The action is dependant upon the device. Specify a key for keyboards. |
True if the unbind was successful, false if the device was unknown or description failed.
moveMap.unbind("keyboard", "space");
unbindObj(string device, string action, string obj)
Remove any object-binding on an input device and action.
Parameters:
device | The device to bind to. Can be keyboard, mouse, joystick or gamepad. |
action | The device action to unbind from. The action is dependant upon the device. Specify a key for keyboards. |
obj | The object to perform unbind against. |
True if the unbind was successful, false if the device was unknown or description failed.
moveMap.unbindObj("keyboard", "numpad1", "rangeChange", %player);