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
  • bind Function
  • bindCmd Function
  • bindContext Function
Edit on GitHub
Export as PDF
  1. For Designers
  2. Inputs
  3. Inputs and Keybinds

Bind Functions

Introduction

This article aims to provide a more detailed understanding of the key functions used with the ActionMap class: bind, bindCmd, and bindContext. These functions are essential for setting up keybinds and managing user input in your game.

bind Function

The bind function is used to associate an input event with a specific action within the ActionMap. It allows you to define keybinds for various input devices such as keyboards, mice, or gamepads. The syntax for the bind function is as follows:

bind(device, action, command);
  • device: The input device for the keybind (e.g., keyboard, mouse, gamepad).

  • action: The specific input event that triggers the action (e.g., a key name, mouse button name).

  • command: The function or method that gets invoked when the input event occurs. In most cases, this is called twice: once when the key is pressed(make) and once when the key is released(break)

Example Usage of bind Function

Let's look at an example that demonstrates the usage of the bind function to set up a keybind for a specific action:

// Binding the "F" key to the "interact" action
moveMap.bind(keyboard, "f", "interact");

In this example, the bind function is used to map the "F" key press event to the "interact" action. When the "F" key is pressed, the function interact() will be invoked.

Because the command is called when the input is made AND broken, the command is called with an argument to convey the key state, like in this example:

function interact(%val)
{
    if(%val)
    {
        // Code for when the button is pressed down
    }
    else
    {
        // Code for when the button is releasedC
    }
}

bindCmd Function

The bindCmd function provides a more flexible way to define keybinds by allowing you to specify different actions for key press, and key release. This function is particularly useful for actions that require different behaviors depending on the state of the key, but requires better control than the simple make/break state tracking of bind(). The syntax for the bindCmd function is as follows:

bindCmd(device, action, pressCmd, releaseCmd);
  • device: The input device for the keybind (e.g., keyboard, mouse, gamepad).

  • action: The specific input event that triggers the action (e.g., a key name, mouse button name).

  • pressCmd: The function or method invoked when the input event is initially triggered (key press / make).

  • releaseCmd: The function or method invoked when the input event is released (key release / break).

Example Usage of bindCmd Function

Let's illustrate the usage of the bindCmd function with an example:

// Binding the "Space" key to different actions based on the key state
moveMap.bindCmd(keyboard, "space", "jump();", "stopJump();");

In this example, the bindCmd function is used to map the "Space" key to different actions depending on the key state. When the "Space" key is initially pressed, the function jump() will be invoked. When the key is released, the function stopJump() will be invoked.

You may also note that the formatting of the commands is different compared to bind(). Whereas bind()'s command was simply the function name that is called back internally and the argument is passed for the make and break events, bindCmd explicitly defines to entire functional code statement called for the make/break states.

This allows you to pack in additional information as arguments, call multiple functions in one event, or more. This allows much more fine-tuned control over behavior when an input is made or broken.

bindContext Function

The bindContext function allows you to define commands based on if the button is tapped, or held. The syntax for the bindContext function is as follows:

indContext(device, action, holdFunction, tapFunction, holdTime);
  • device: The input device for the keybind (e.g., keyboard, mouse, gamepad).

  • action: The specific input event that triggers the action (e.g., a key name, mouse button name).

  • holdFunction: The function or method invoked when the input event is held down for longer than the holdTime.

  • tapFunction: The function or method invoked when the input event is held down for less than the hold time.

  • holdTime: The amount of time in milliseconds to split between a tap and a hold.

Example Usage of bindContext Function

Let's consider an example to showcase the usage of the bindContext function:

// Create a new context called "MainMenu"
moveMap.bindContext(keyboard, "c", "holdCrouch();", "toggleCrouch();", 500);

function holdCrouch() {
    // Logic for only crouched as long as the button is held down
}

function toggleCrouch() {
    // Logic for toggling crouched on and off perpetually
}

In this example, we define a keybind context for the "c" key. If the key is pressed and then releaed for less than the defined 500 millisecond hold time, then the toggleCrouch() function will be called. If it is held down for longer than 500 milliseconds, then the hold function is called.

It is important to note that the break status - releasing the key - is what dictates the context. If the key is released before the holdTime, it must be a tap. If it is held down for ANY period of time longer than the holdTime, including never being released at all, then the hold function is callled after the holdTime is passed.

Next, we'll go over how to manage multiple ActionMaps via the stack.

PreviousActionMapNextActionMap Stack

Last updated 1 year ago