Jump to content

game data in group is null when access from different class /states?


farhan
 Share

Recommended Posts

Hi, I used the game states to add different states like preload, main menu, game and status screen etc
and in the global variable, I have a group called container, and in Preloader I create or add the group; and for some reason when I try to access the group created by thePreloader from other states/classes, the game value of the group is null, and I cannot use that group unless I recreate it in the that state/class.

Is there a way to create a global group that I can share with the rest of states? Can I define/add this group in other state and share with other states?

Thanks.

Global = {
   container:null,
}

Game.Preloader = function (game) {
};

Game.Preloader.prototype = {
	preload: function () {
	},

	create: function () {
       Global.container = this.add.group();
       console.log(Global.container);// the game data is NOT null
	},
};

Game.MainMenu = function (game) {
};

Game.MainMenu.prototype = {
	preload: function () {
	},

	create: function () {
      console.log(Global.container);// the game data is null
	},
};

 

Link to comment
Share on other sites

Yep, there is a way to pass parameters to other states.

https://photonstorm.github.io/phaser-ce/Phaser.StateManager.html

When you call start() method you can pass params to the other state, which you will get in the other's init() method.

 

E.g.:

create: function () {

    var tmp = this.rnd.between (-1, 1);
    this.state.start('StateB', true, false, tmp);

}

App.StateB.prototype = {

    init: function (number) {

        console.log(number);

    }

};

 

But I do not know why a global var doesn't save any value. I created global objects before creating a game window and adding states. It works for me.

Link to comment
Share on other sites

@farhan the issue you face is related to how you switch between states. The first option sets the state you want to start but the rest is used to tell Phaser if all objected created in current state should bedestoyed (default). Since you created group this.add.group() it will be automatically destroyed (to clear memory from junk). The only solution is to tell state manager not yo clear BUT I would not suggest todo that because in that case you will have new state over old one, plus you have to clear memory from unnassasary objects yourself. That's way complicated... so simply recreate the group. Unity for example has option to set one particular object to be preserved, Phaser so far does not have (at least I have not found it yet). As for @dude78 suggestion - that's also very handy, if you need to pass some local parameters for state but user profile or something bigger should be stored as global variable just like you did it.

Link to comment
Share on other sites

Thanks everyone for the info; yeah, i figured out that in default, it destroys group object which is why the group was null; when I set not to destroy, it works fine, but like you said this may cause garbage issue. We can also recreate all groups, but to me, that is pointless since why do we need to destroy and then recreate again, when we don't really need to destroy it in the first place.

Anyway, I ended up not using the statemanager and just go back to the basic way where we set the preload, create, update and/or render functions when creating the game; and I just create my own simple state manager and clean up the group manually :D The only downside, all game states are clumped together, but at least it is simpler and I don't have to destroy and recreate unnecessary stuff :)

I was just trying out statemanager so that I'm able to organize the code by states; but I guess this is not a good option for me. Specially when I am just making a tiny simple short game.

Link to comment
Share on other sites

@farhan out of curioucity, when you mentioned about custom state manager, I decided to check how Phaser is doing that and is there a way to tell it to skip some? So basically Phaser with clearWorld parameter enabled is dong two things: resets scale and shutdowns the world... and here is what shutdown of the world means:

https://github.com/photonstorm/phaser/blob/v2.6.2/src/core/World.js#L158

// World is a Group, so run a soft destruction on this and all children.

Awesome, did you got the point? So basically set resetWorld to false and once new state will be started, in its init/preload even you can clean up (destroy) world (the children of previous state) which you do not need by simply calling game.world.remove(child_name, true).

I have not tested this solution yet but I will give it a try with one of the next game's update.

Link to comment
Share on other sites

15 hours ago, Yehuda Katz said:

@farhan out of curioucity, when you mentioned about custom state manager, I decided to check how Phaser is doing that and is there a way to tell it to skip some? So basically Phaser with clearWorld parameter enabled is dong two things: resets scale and shutdowns the world... and here is what shutdown of the world means:

https://github.com/photonstorm/phaser/blob/v2.6.2/src/core/World.js#L158

// World is a Group, so run a soft destruction on this and all children.

Awesome, did you got the point? So basically set resetWorld to false and once new state will be started, in its init/preload even you can clean up (destroy) world (the children of previous state) which you do not need by simply calling game.world.remove(child_name, true).

I have not tested this solution yet but I will give it a try with one of the next game's update.

Cool, that is exactly what I did to clean it, destroy the children. Sometimes it is faster to do it myself based on what I know than figuring out what Phaser already provided ? I am still new to Phaser, and don't have much time to figure out everything lol But thanks for the tips, will look into that as I work on more Phaser projects.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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