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
Edit on GitHub
Export as PDF
  1. Getting Started
  2. Getting Familiar
  3. Your First Game

Adding a Player

In Torque3D we call the object that you are moving when you press keys the ControlObject and the Player is one instance of such a ControlObject. So, let's define a PlayerData datablock, place the following in the file data/CoinCollection/datablocks/player.tscript:

datablock PlayerData( CoinCollectorPlayer ) {
    // Third person shape
    ShapeAsset = "Prototyping:Playerbot_shape";

    // Set distance from camera to player
    cameraMaxDist = 3.0;
};

datablock this is very similar to a struct, basically in a datablock you define a set of default values which will get referenced by the spawned objects upon creation.

PlayerData( CoinCollectorPlayer ) here we state that we want to create a datablock of the type PlayerData, for creating new Players. We call this new datablock CoinCollectorPlayer .

ShapeAsset here it is a reference to the asset Playerbot_shapein the Prototyping module. But it could be set to any ShapeAsset in any module. We'll cover assets later in the tutorial.

cameraMaxDist defines the maximum distance from the Player shape to the Camera this will essentially allow us to zoom out a bit.

And we need to register this datablock, do this in the onCreateGameServer function in data/CoinCollection/CoinCollection.tscript:

function CoinCollection::onCreateGameServer(%this) {
    %this.registerDatablock("./datablocks/player.tscript");
}

We use the method registerDatablock because the coremodule has a lot of clever logic about loading/unloading/updating datablocks which we can't take advantage of if we just use the exec function.

Now we have a PlayerData that we can use, but in order to actually use it, we have to create a Player instance whenever a player enters the game. We do that in data/CoinCollection/server/gamemode.tscript, first let's create a helper method that creates the Player using the CoinCollectorPlayer datablock and assigns it as the client's Control Object:

function CoinCollectionGameMode::spawnControlObject(%this, %client) {
    // First spawn the actual Player object
    %player = spawnObject(Player, CoinCollectorPlayer);
    if (!isObject(%player)) {
        return;
    }
    MissionCleanup.add(%player);

    // Place it at the center of the world with a default rotation, 
    // this is a pretty simple "spawn placement"
    %spawnTransform = "0 0 1 0 0 0 0 0";
    %player.setTransform(%spawnTransform);
    
    // Couple the player to the client and set it as the control object
    %player.client = %client;
    %client.setControlObject(%player);
    %client.player = %player;

    // Tell the client that we expect it to be in third person
    %client.setFirstPerson(false);
}

Now that's just a helper method, in order to actually activate it we should call it from the onClientEnterGame function that we will add in that same file:

function CoinCollectionGameMode::onClientEnterGame(%this, %client) {
    // Set the player name based on the client's connection data
    %client.setPlayerName(%client.connectData);

    // Call the helper function
    %this.spawnControlObject(%client);
}

And let's make sure to delete the player object when they leave:

function CoinCollectionGameMode::onClientLeaveGame(%this, %client) {
    // Remove the player object
    %client.player.delete();
}

Now, run the level again and see that you can move around and jump with your newly created Player object!

PreviousCreating an empty gamemodeNextAdding a Coin

Last updated 1 year ago