Jump to content

Trouble with variables


xkorikx
 Share

Recommended Posts

Hi. I recently found an example of global variable while using states(example: game.variable == 1), they works fine in GameOver state, but in MainMenu state they got broken (TypeError: this.game is undefined). What I doing wrong?

 

You can see example here: game (added console.log of variable for easy debug).

Link to comment
Share on other sites

I had trouble with this myself a while ago. I spent quite a long time writing my level change code because i didnt want multiple files per level and thought it would be better sticking all the logic in a single function. To do this i need a variable that could be used between states because i had cutscenes in different states and was returning to the main game states to transfer the level i had to modify a lot of variables to give me the right logic for the right level.

 

I originally toyyed with a few ideas but realised it was a bit more simpler than i had first thought, 

what i did was declare the most important variable i needed to use across states in my boot.js because that comes before any game/menu/preload so its at the top of global visioning being loaded first.

 

I didnt have it linked to the game so i didnt have it as example:

var game.mapNumber = 0;//instead it was a simple number variablevar mapNumber = 0;

now when my game state is loaded it sets that variable for the first level required in the logic, so it loads the map in the manner required for my purpose. 

So if you need a basic number set up for cross state modification, put the variable in boot (might not need to but worked in my instance) and keep it as simple as possible. Basica variable for true/false or numbers/strings etc.

 

Hopefully this is helpful and you can get it running 

 

:)

Link to comment
Share on other sites

I had trouble with this myself a while ago. I spent quite a long time writing my level change code because i didnt want multiple files per level and thought it would be better sticking all the logic in a single function. To do this i need a variable that could be used between states because i had cutscenes in different states and was returning to the main game states to transfer the level i had to modify a lot of variables to give me the right logic for the right level.

 

I originally toyyed with a few ideas but realised it was a bit more simpler than i had first thought, 

what i did was declare the most important variable i needed to use across states in my boot.js because that comes before any game/menu/preload so its at the top of global visioning being loaded first.

 

I didnt have it linked to the game so i didnt have it as example:

var game.mapNumber = 0;//instead it was a simple number variablevar mapNumber = 0;

now when my game state is loaded it sets that variable for the first level required in the logic, so it loads the map in the manner required for my purpose. 

So if you need a basic number set up for cross state modification, put the variable in boot (might not need to but worked in my instance) and keep it as simple as possible. Basica variable for true/false or numbers/strings etc.

 

Hopefully this is helpful and you can get it running 

 

:)

 

Just test it. It works fine, thanks for help. Now I'm gonna rewrite code a little)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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