Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

State Global Variables?


megmut
 Share

Recommended Posts

Hey guys, a quick question.. I have a feeling I'm doing things the old school, noob way in phaser for using variables across multiple methods inside a game state prototype. 

 

Here's an example code:

"use strict";var screenLocation;var lang;var json;var garageScreenGroup;App.Game = function(game) {	};App.Game.prototype = {	init: function() {		// Setting up variables for game		screenLocation = "game";	},	preload: function() {		lang = this.game.global.user.language;		json = this.game.cache.getJSON('gameLayout');	},

So my question is, do I have to declare my variables outside of the prototype to use them? My gut feeling is the answer is no, and that I should be putting them inside the prototype to avoid variables being re-used despite the state being killed and re-initiated. (I've had this issue a few times).

 

Perhaps, more a Javascript question that phaser specific, but any input would be greatly accepted! Thanks :)

Link to comment
Share on other sites

a lot of people add stuff onto this.game from what i've seen (this.game.global as per your code). I've only found that an issue when it's in a Plugin and people are pulling that into TypeScript, because it messes with the phaser.d.ts definitions which define "game"

 

if you put it at the very beginning of all your code though it'll be global as well won't it? (screenLocation etc)

 

according to the optimisation docs, you shouldn't use prototype anyway ;) http://www.html5gamedevs.com/topic/9931-mobile-performance-tips-tricks/

Link to comment
Share on other sites

Thanks jpm909! Hadn't seen these optimisation docs before! I always thought the better the JS, the more efficient the game.. guess not!

 

Thinking about it, maybe I should put them inside a state object i.e:

 

this.game.global.preload = {};

this.game.global.menu = {};

this.game.global.map = {};

 

My biggest issue to be honest, is sometimes.. when players re-play the game, they start with the score they finished with, even though I am clearly calling to destroy the objects that hold the score in the shutdown method.. ASWELL as nulling / undefining them in the init method when that state opens back up again.

 

Thanks for the tips :)

Link to comment
Share on other sites

I don't know how your code works or Phaser's using internal functions to destroy objects in phaser may only remove the elements it knows about...

 

(theoretical here)

 

eg if you had:

this.game.cache // (internal)this.game.score // (your variable)

and then called some destroy function, it might only reset/remove "cache" as that's the only one it knows to clear up.. it doesn't know about the existence of your "score" and it might not just be nulling "this.game"

 

either that or you're not destroying the object that holds score, you're just destroying the reference to the object that holds score

 

(non-phaser basic JS example)

var game = {id: 'myGame'}game.score = 100function destroy() {    var _score = game.score;   _score = null}function betterDestroy() {   var _game = game;   _game.score = null // that's going to set our score to null ... BUT....   _game=null // that's just going to destroy the local reference}function reallyDestroy() {    var _game = game;    _game = null;    game = _game; // assign local var back to original}destroy();console.log(game, game.score) // => score still 100betterDestroy();console.log(game, game.score) // => score null, but game still existsreallyDestroy();console.log(game, game.score) // terminal error.. score no longer exists since game is now null
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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