Table of Contents

Having already seen spaces in a previous tutorial, we will now talk about a component that only appears on space objects: the TimeSpace.

Learning Objectives

  • TimeScale
  • Pausing

Level Setup

What is the TimeSpace?

The TimeSpace component allows for developer control over time in the space it is attached to. Time in that space can be slowed or even stopped. Let's see what the demo looks like right now to get a frame of reference.

control

The ball falls, bounces, and rolls at a normal pace for the values it was initialized with.

TimeScale

A common dramatic effect is to slow down time during some in-game event. Slow motion effects are typically implemented via the TimeScale property, which is a scalar for the rate at which time passes. There are also some times where running your project at a slower speed will allow you to debug issues more easily. Let's take a look at how to change TimeScale using the UI.

(NOTE)Space Selection: The Space can be selected via the Select menu, by clicking in the viewport and then pressing Shift + S, by pressing Ctrl + Shift + S with any window in focus, or by using the SelectSpace command. image

halfspeed

Demo running half speed with 0.5 TimeScale

Now we can see that the time scale directly affects the rate of time without affecting the framerate.

Adjusting TimeScale In Script

  • Command : Add Resource
  • Create a NadaScript resource using the Component template template and name it TimeControl
  • Update the TimeControl script to the following:
class TimeControl : NadaComponent
{
  [Property]
  var IncreaseRateKey : Keys = Keys.Up;
  
  [Property]
  var DecreaseRateKey : Keys = Keys.Down;
  
  [Property]
  var TimeScaleRateOfChange : Real = 0.1;
  
  function Initialize(init : CogInitializer)
  {
    Zilch.Connect(this.Space, Events.LogicUpdate, this.OnLogicUpdate);
  }

  function OnLogicUpdate(event : UpdateEvent)
  {
    if(Zilch.Keyboard.KeyIsPressed(this.IncreaseRateKey))
      this.ScaleTime(this.TimeScaleRateOfChange);
      
    if(Zilch.Keyboard.KeyIsPressed(this.DecreaseRateKey))
      this.ScaleTime(-this.TimeScaleRateOfChange);
  }
  
  function ScaleTime(rateChange : Real)
  {
    var newTimeScale = this.Space.TimeSpace.TimeScale + rateChange;
    this.Space.TimeSpace.TimeScale = Math.Clamp(newTimeScale, 0.0, 2.0);
    Console.WriteLine("TimeScale: `this.Space.TimeSpace.TimeScale`");
  }
}

controlled

TimeScale being adjusted up and down

Above we can see the demo progressing slower and then faster as the TimeScale is adjusted using the keyboard.

Pausing

  • Add the following property to the TimeControl script:
  [Property]
  var PauseKey : Keys = Keys.Space;
  • Add the following function to the TimeControl script:
  function TogglePause()
  {
    if(this.Space.TimeSpace.Paused)
    {
      Console.WriteLine("Game was paused, unpausing now");
      this.Space.TimeSpace.Paused = false;
    }
    else
    {
      Console.WriteLine("Game was not paused, pausing now");
      this.Space.TimeSpace.Paused = true;
    }
  }
  • Add the following block to the end of the OnLogicUpdate function in the TimeControl script:
    if(Zilch.Keyboard.KeyIsPressed(this.PauseKey))
      this.TogglePause();
  • Command : PlayGame
  • Press Space to pause the game and then press Space again to attempt to unpause the game

stopped

You probably noticed that the game will not unpause. This is because when the TimeSpace is paused, Keyboard, listening for the KeyDown event, or to poll the keyboard input on FrameUpdate.

  • Update the Initialize function in TimeControl to the following:
  function Initialize(init : CogInitializer)
  {
    Zilch.Connect(Zilch.Keyboard, Events.KeyDown, this.OnKeyDown);
  }
  • Replace the OnLogicUpdate function in TimeControl with the following:
  function OnKeyDown(event : KeyboardEvent)
  {
    if(event.Key == this.IncreaseRateKey)
      this.ScaleTime(this.TimeScaleRateOfChange);
      
    if(event.Key == this.DecreaseRateKey)
      this.ScaleTime(-this.TimeScaleRateOfChange);
    
    if(event.Key == this.PauseKey)
      this.TogglePause();
  }
  • Command : PlayGame
  • Press Space to pause the game and then press Space again to attempt to unpause the game

pausing

Pausing with and unpausing in reaction to the KeyDown event

Now we can see the game being paused and unpaused successfully.

Related Materials

Manual

Reference

Commands

Classes

Events

Development Tasks

  • {T1178}