Jump to content

Rapidly change state without repeated calls to create()?


Recommended Posts




I'm programming a roguelike and I've divided the main game into three distinct states:  PlayerTurn, Animating, EnemyTurn. I do this to separate game logic and not have a humongous God class. Each state calls the other in a loop:  PlayerTurn -> Animating -> EnemyTurn -> Animating -> PlayerTurn



        update() {            var keyboard = this.game.input.keyboard;                            for (var inputCommand in this.actionMap) {                var keyCode = toKeyCode(inputCommand);                if (keyboard.isDown(keyCode)) {                    this.actionMap[inputCommand]();                    this.switchToAnimatingState();                }            }        }


        init(args: any) {            console.log("In AnimatingState.");            this.view = args[0];            this.map = args[1];            this.player = args[2];            this.nextState = args[3];            this.view.onTweensFinished.addOnce(() => this.switchToNextState(), this);            this.view.play();        }


        update() {            // No logic yet.            this.game.state.start(State.AnimatingState, false, false, [this.view, this.map, this.player, State.PlayerState]);        }

Anyway, all three States have the init(args) function. What I also noticed is that each game.state.start() call destroys, preloads, and creates every state new again. I don't really need, nor do I want, to perform my initialization logic twice. I have my fully constructed view, tilemap, and player class that I'm passing around.


In order to avoid creating my class twice and returning to the beginning game state, I have a static (global in js) variable:

        create() {            if (!PlayerState.hasBeenCreated) {                this.game.stage.backgroundColor = "#000000";                this.initializeView();                this.initializeInputBindings();                this.initializeMap();                this.initializePlayer();                PlayerState.hasBeenCreated = true;            }        }

That's pretty ugly and I wish it'd go away. Is it possible to switch states without destruction/update? My other solution was to have one GameState, and it controls which substate to process at the moment.

Link to comment
Share on other sites

This isn't really how States have been designed. You could still do this but I wouldn't use any of the pre-defined methods (create, preload, etc) and instead would just call my own custom functions as needed, keeping track of it myself. Which isn't far off what you've got. Also you could just do:

create() {  if (PlayerState.hasBeenCreated) { return; }  ....}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...