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 Stack Overview
  • Pushing and Popping ActionMaps
Edit on GitHub
Export as PDF
  1. For Designers
  2. Inputs
  3. Inputs and Keybinds

ActionMap Stack

Introduction

The ActionMap stack is a powerful mechanism in the Torque3D game engine that allows for the management of multiple ActionMaps. It provides a way to handle input events and callback functions based on the active ActionMap on the stack. This documentation article will guide you through the process of pushing and popping ActionMaps onto the stack and explain how the stack manages the engine's handling of input events and callback functions.

ActionMap Stack Overview

The ActionMap stack is a Last-In-First-Out (LIFO) stack that holds multiple ActionMaps. The active ActionMap on the top of the stack determines how input events are processed and which callback functions are invoked. When an input event occurs, the stack is traversed from top to bottom, allowing the topmost ActionMap to handle the event first. If the event is not handled by the topmost ActionMap, it is passed down to the next ActionMap on the stack.

Stack Functions

To manage the ActionMap stack, the Torque3D engine provides the following functions:

  • push(): Pushes the current ActionMap onto the stack, making it the active ActionMap.

  • pop(): Removes the topmost ActionMap from the stack, reverting to the previous active ActionMap.

Pushing and Popping ActionMaps

To manage multiple ActionMaps and switch between them, you can use the push() and pop() functions. These functions allow you to push a new ActionMap onto the stack or remove the topmost ActionMap from the stack, respectively.

Example Usage

Let's consider an example that demonstrates how to push and pop ActionMaps onto the stack:

// Create two ActionMaps
new ActionMap(map1);
new ActionMap(map2);

// Set up keybinds for map1
map1.bind(keyboard, "w", "moveForward");
map1.bind(keyboard, "s", "moveBackward");

// Set up keybinds for map2
map2.bind(keyboard, "w", "strafeLeft");
map2.bind(keyboard, "d", "strafeRight");

// Push map1 onto the stack
map1.push();

// Handle input events and callbacks for map1
// ...

// Push map2 onto the stack
map2.push();

// Handle input events and callbacks for map2
// ...

// Pop map2 from the stack
map2.pop();

// Handle input events and callbacks for map1 again
// ...

// Pop map1 from the stack
map1.pop();

In this example, we first create two ActionMap instances: map1 and map2. We set up keybinds for each ActionMap accordingly.

We then push map1 onto the stack using the push() function, making it the active ActionMap. The engine will handle input events and invoke the appropriate callback functions defined in map1.

Next, we push map2 onto the stack using push(), replacing map1 as the active ActionMap. Now, the engine will handle input events and invoke the callbacks defined in map2.

In the example, map1 and map2 both define binds on "w". Because inputs are processed top-down, this means that when map2 is on the stack, it's bind is processed first, and strafeLeft() will be called.

However, because map2 doesn't have a keybind for "s", map1's bind will continue to be in effect when the "s" key is pressed.

Later, we pop map2 from the stack using the pop() function. This restores map1 as the active ActionMap, and the engine will handle input events and callbacks defined in map1 again.

Because map2 is no longer on the top of the stack, map1's "w" bind is in effect once more.

Finally, we pop map1 from the stack, removing it from the stack entirely. At this point, there are no ActionMaps on the stack, and the engine will not process any further input events or invoke corresponding callbacks.

For a more practical example, below shows how you would have an ActionMap be pushed and popped from the stack based on a certain specific event. In the example, when a client connects or disconnects from the server, respectively:

// Add the following code to your client-side Gamemode script

//This is called when a client connects to a server
function MyGameMode::onCreateClientConnection(%this)
{
   // Push the client's ActionMap onto the stack
   MyGameMoveMap.push();
}

//This is called when a client disconnects from a server
function MyGameMode::onDestroyClientConnection(%this)
{
   // Pop the client's ActionMap from the stack
   MyGameMoveMap.pop();
}

In this example, we have two event handlers for our GameMode object: onCreateClientConnection and onDestroyClientConnection.

When a client connects (onCreateClientConnection ), we push the MyGameMoveMap ActionMap, which is defined in a different script file, onto the stack using the push() function, making it the active ActionMap for the client.

When a client disconnects (onDestroyClientConnection), we pop the client's ActionMap from the stack using the pop() function, reverting to the previous active ActionMap, if any. While we could also delete the ActionMap, it is normal to leave it in memory as we then don't need to recreate or or re-execute scripts in the event a player immediately joins another game.

By using these event handlers, you can ensure that each client has its ActionMap with appropriate keybinds, and the ActionMaps are properly managed when clients connect or disconnect from the server.

PreviousBind FunctionsNextLocalization

Last updated 1 year ago