Results 1 to 10 of 26

Thread: State machine - requesting advice

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    State machine - requesting advice

    Time of implementing state machine in Super Heli Land is closer and closer, so I want to ask you for advice whether my idea of doing it is correct or not.

    All states would inherit from TState class which would look more or less like this:

    Code:
    TState = class
      public
        Constructor Create;
        Destructor Destroy;
        procedure update;
        procedure draw;
      private
        {...}
    end;
    There will be also PState, which would be pointer to TState:
    Code:
    PState = ^TState;
    All children classes of TState would overload update and draw methods (first would be ran 60 times per second using Allegro timer, last would be called in main loop as fast as it can).

    Main file would contain one PState declaration:
    Code:
    var CurrentState:PState;
    and also single instances of every possible state (which in case of SHL would be only 4: Menu, options, actual game and high scores, with best high score being displayed in main menu.

    Then after creation of each state, allegro's initialization would take place, then CurrentState would be set to main menu state:
    Code:
    CurrentState:=@MainMenuState;
    then timer callback and main loop would be started.

    The update (rendering) loop would look like that:
    Code:
    repeat
    CurrentState^.draw;
    until quit;
    (quit is boolean variable) and timer callback would look like this:
    Code:
    procedure update();CDECL;
      begin
        //frame update
        CurrentState^.update;
      end;
    States would be changed by setting CurrentState so it would point to other state.

    So there's my idea. How much of it is correct way of doing this?
    And what problems I'll face down the road (this will be my first time ever I'll implement state machine)?
    Last edited by Darkhog; 13-06-2013 at 01:47 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •