Menu

[r73]: / trunk / src / docs / gameflow.pds  Maximize  Restore  History

Download this file

52 lines (33 with data), 2.7 kB

@section(1 gameflow Game states)

@section(2 gameflow_basics Basics)
Your game flow is based in @italic(game states).  You must extend @link(TGameState) implementing its @code(Update) and @code(Render) methods, create an object and assign it the game's state manager.

In most games you'll have to create more than one state.  For example, most games have an introduction or title sequence, a configuration dialog, the gameplay and a game over screen.  Each of these can be implemented as different states and move from one to other using @Code(@link(Application).GameState.Current) property.

You have a simple example showing a basic gameflow in @code(~/src/examples/ex_gameflow.pp).  It shows how to extend @link(TGameState) to create the different states, how to add them to the @link(a3dge.Application) object and how to change game state when needed.

Note that @code(TGameState) extends @link(TState) so it inherits most of its functionality.



@section(2 gameflow_state_initialization State initialization and finalization)
To initialize the game state, use its @code(Enter) method;  it will be called when the state becames the @code(Current) one.

If you decide to load (or create) data in the @code(Enter) method you must know that the game won't dispatch events until it exits this method.  So when the method ends, A3DGE will dispatch all events queued.  That may include update events (@italic(game ticks)) that will call @code(Update) several times, maybe a lot of times depending how much time it needed to load data.

There are ways to deal with this.  The most direct and aggressive (yet effective) one is this one:

@longcode(?
(* Initialize the state, loading all data. *)
  procedure TMyGamePlayState.Enter (aEntity: TObject);
  begin
    inherited Enter (aEntity); { Play nice with the engine. }
    try
    { Avoids event handling. }
      (aEntity as TA3DGEApplication).PauseUpdates;

    {
      Put here the code to load your data.
    }

    finally
    { Restore the event handling system. }
      (aEntity as TA3DGEApplication).ResumeUpdates
    end
  end;
?)

You may use a similar code in the @code(Leave) method too as releasing resources may be also time consuming.

@bold(See also:)

  @link(TState.Enter) @link(TState.Update) @link(TGameState.Render) @link(TState.Leave)

  @link(TA3DGEApplication.GameState)

@section(2 gameflow_state_update State update)

Calling @code(a3dge.Application.Run) will start a loop that will call the current state's @code(Update) method in a regular pace.  This pace is defined by the @link(TA3DGEApplication.TicksPerSecond) property.  Default value is @link(a3dge.FPS).  @code(Run) will also call the current state's @code(Render) method @italic(when it's possible).
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.