Torque3D
  • General
    • Welcome!
    • Features
    • Release Notes
      • Version 4.0.3
      • Version 4.0.2
      • Version 4.0.1
      • Version 4.0
      • Version 3.10.1
      • Version 3.10
      • Version 3.9
      • Version 3.8
      • Version 3.7
      • Version 3.6.2
      • Version 3.6.1
      • Version 3.6
  • Getting Started
    • Introduction
      • What's the Torque3D Engine?
    • Getting Familiar
      • Getting a Copy
        • Torque Project Manager
        • Downloading it Yourself
      • Getting Ready for Launch
        • Running Pre-Built Binaries
        • Building the Engine Yourself
      • Launching the Game
        • Opening the Example Level
        • Launching the Editors
      • Your First Game
        • Introduction To The Engine
        • The Module System
        • Creating an empty gamemode
        • Adding a Player
        • Adding a Coin
        • Adding a win-condition
        • Custom coin asset
        • Counting coins
        • Adding some effects
        • Supporting multiplayer
        • Adding a Scoreboard GUI
        • Keeping the scoreboard up-to-date
      • Deep Dive: BaseGame Directory Structure
    • Best Practices
    • Porting a Legacy Project
  • For Artists
    • Assets
      • What are Assets?
      • How to Create a New Asset
      • Working With Assets
      • Deep Dive: Creating a New Asset Type
    • Art
      • File Formats
      • 3D Art
        • Shape Specifications
        • Coordinates System
        • Mounting Shapes
        • Animation
        • Player Setup
        • Blender -> Torque3D Pipeline
      • 2D Art
        • Working with Adobe Substance
    • Animation
    • GUI
      • Loading and Initializing a GUI
      • Expanding a GUI via Script
      • How to Network GUIs
    • Materials
      • Material Mapping
      • Material Animation
    • Terrain
    • Shaders
    • Lighting
    • Audio
  • For Designers
    • Base Classes
      • SimObject
      • SimGroup
      • SceneObject
      • Scene
      • Datablocks
    • Game Classes
      • Creating an Object
      • Destroying an Object
      • Gameplay Scripting
        • Spawning an Object from Gameplay Code
    • Modules
      • What are Modules?
      • How to Create a New Module?
      • Making a Module do Things
      • Installing Existing Modules
        • Where to Get More Modules
    • Scenes and Levels
      • How to Create a New Level
      • How to Load a Level
        • Deep Dive: Level Loading Scripts
      • How to Edit Levels
        • Opening a Level in the Editor
        • Spawning Objects from the Asset Browser
        • Working with Scenes
        • Using SimGroups
        • Changing a Level's PostEffects
        • Deep Dive: LevelAsset Companion Files
    • Game Modes
      • Creating a New GameMode
      • Making a Level Use Your GameMode
      • Adding Gameplay Code to Your GameMode
    • AI
      • Navmesh
      • Objects
      • Scripting
    • Inputs
      • Inputs and Keybinds
        • ActionMap
        • Bind Functions
        • ActionMap Stack
    • Localization
    • Editors
      • Changing Editor Settings
      • World Editor
        • Scene Editor
        • ConvexShape Editor
        • Terrain Editor
        • Terrain Painter
        • Material Editor
        • Spline-Based Tools
          • Mesh Road Editor
          • River Editor
          • Decal Road Editor
        • Datablock Editor
        • Particle Editor
        • Decal Editor
        • Forest Editor
        • Navmesh Editor
        • Deep Dive: Creating Your Own Editor
        • Shape Editor
      • GUI Editor
        • Interface Details
  • For Programmers
    • Compiling the Engine
      • Setup Development Environment
        • SDK and Library Installation
        • Git
        • Cmake
        • Creating a Fork on Github
      • Create a Project
        • Creating a Project With CMake
        • Creating a Project With the Project Manager
      • Compiling
        • Compiling in Windows
        • Compiling in Linux
        • Compiling in MacOS
      • Building the Project Manager
    • Introduction
    • Code Style Guidelines
    • Expanding the Engine
      • Creating a New Object Class
      • Exposing Object Classes to Script
        • addProtectedField
      • Adding a New Library to the Engine
    • Major Components of the Engine
      • Core
        • Console
        • Platform
      • Audio
        • SFX
      • Rendering
        • GFX
        • Render Bins
      • Physics
        • Stock T3D Physics
        • Physics Wrapper
          • PhysX
          • Bullet
        • Classes
    • Rendering
    • Math
    • Networking
      • Client and Server Commands
    • Physics
    • Collision
    • Scripting
      • TorqueScript
        • What is TorqueScript?
        • Basic Syntax
        • Variables
        • Types
        • Operators
        • Control Structures
        • Functions
        • Objects
        • Module Interop
          • QueueExec
          • RegisterDatablock
          • CallOnModules
          • ModuleExec
        • API Reference
      • Other Languages
        • C-Interface
    • File Inputs/Outputs(I/O)
    • API Reference
Powered by GitBook
On this page
  • Introduction
  • ActionMap Class Overview
  • Setting up Keybinds with ActionMap
Edit on GitHub
Export as PDF
  1. For Designers
  2. Inputs
  3. Inputs and Keybinds

ActionMap

Introduction

The ActionMap class in Torque3D is a powerful tool that allows developers to manage and handle user input in their games. It provides a simple and flexible way to define keybinds and associate them with specific actions or functions within the game.

This documentation article will guide you through the various aspects of the ActionMap class, including its definition, setup, and usage for creating keybinds in your Torque3D game.

ActionMap Class Overview

The ActionMap class is responsible for handling input events and mapping them to specific actions in the game. It acts as a repository for all the keybinds defined in the game and provides a straightforward interface to manage and process user input.

Key Terms

Before diving into the usage of the ActionMap class, let's familiarize ourselves with a few key terms:

  1. Command: A command is a specific game function triggered by user input. It can be associated with one or more keybinds.

  2. Keybind: A keybind maps a specific input event, such as a keyboard key or mouse button, to an action.

  3. Device: A device refers to the input source, such as a keyboard or mouse, from which the input events originate.

Setting up Keybinds with ActionMap

To create keybinds using the ActionMap class, you'll typically follow these steps:

  1. Create an ActionMap instance: Instantiate an ActionMap object to define a new keybind mapping.

  2. Define commands: Define the commands you want to associate with specific keybinds.

  3. Add keybinds: Associate keybinds with commands by specifying the input events (e.g., keyboard keys, mouse buttons) and the associated commands.

  4. Enable the ActionMap: Push the ActionMap onto the stack to start processing input events and triggering the associated commands.

Example Usage

Let's walk through an example of how to use the ActionMap class to create keybinds for movement in a game:

// Step 1: Create an ActionMap instance
if(!isObject(moveMap))
{
   new ActionMap(moveMap);
}

// Step 2: Define key commands
moveMap.bindCmd(keyboard, "w", "moveForward();", "moveStop();");
moveMap.bindCmd(keyboard, "a", "moveLeft();", "moveStop();");
moveMap.bindCmd(keyboard, "s", "moveBackward();", "moveStop();");
moveMap.bindCmd(keyboard, "d", "moveRight();", "moveStop();");

// Step 3: Add keybinds

// Binding the arrow keys for movement
moveMap.bind(keyboard, "up", "moveForward();");
moveMap.bind(keyboard, "down", "moveBackward();");
moveMap.bind(keyboard, "left", "moveLeft();");
moveMap.bind(keyboard, "right", "moveRight();");

// Step 4: Enable the ActionMap
moveMap.push();

// Game functions called as commands by the keybinds
function moveForward(%val) {
   // Logic to move the player forward
}

function moveBackward(%val) {
   // Logic to move the player backward
}

function moveLeft(%val) {
   // Logic to move the player left
}

function moveRight(%val) {
   // Logic to move the player right
}

function moveStop(%val) {
   // Logic to stop the player's movement
}

In the example above, we first create an instance of the ActionMap class called moveMap. Then, we define commands using the bindCmd function, which associates the commands with specific input events. The bindCmd function takes the following parameters: the device (e.g., keyboard), the input event (e.g., "w" for pressing the 'W' key), and the command associated with the key press, and key release.

Important terminology: When a key is pressed down, it is "made", and when it is released, it is "broken". Handling the make and break states give more full control over your game inputs.

Next, we add additional keybinds using the bind function. In this case, we bind the arrow keys to the corresponding movement actions.

Once all the keybinds are set up, we enable the moveMap by calling the push method. This activates the ActionMap and allows it to start processing input events and triggering the associated commands.

The example also includes placeholder functions (moveForward, moveBackward, moveLeft, moveRight, and moveStop) that represent the game functions or behaviors called when the inputs are made or broken. These functions can be implemented with the specific logic needed for player movement in your game.

It's also important to note that modifier keys ( shift, alt, ctrl, etc) can be used in conjunction with a normal input event, and is treated as a separate input.

moveMap.bind(keyboard, "shift w", "sprintForward();");

// This function is only called if shift + w is pressed. 
// If only w is pressed, then the above moveForward function is 
// still called as normal
function sprintForward(%val) {
    // Logic to move the player at a sprint
}

Next, we'll go into more detail about the different bind functions and how to utilize them.

PreviousInputs and KeybindsNextBind Functions

Last updated 1 year ago