Loading States

Is there a way to call game.state.load without using a timer? Whenever I try to use it without a timer I get the following error:

"Cannot call when switching scene."
Has invited:

justinboyd5

Favor from: Morabaraba

In my case I have a preloader state that's loading. Once it's loaded I want to execute the next state (Menu). Currently this is my code which throws an error:
 
var Preloader = qc.defineBehaviour('qc.engine.Preloader', qc.Behaviour, function() {}, {});

Preloader.prototype.awake = function() {
var context = this;

/*
preloader scripts here...
*/

context.game.state.load('Menu', false, function(){

}, function(){

});
};

The code below here fixes the issue:
var Preloader = qc.defineBehaviour('qc.engine.Preloader', qc.Behaviour, function() {}, {});

Preloader.prototype.awake = function() {
var context = this;

/*
preloader scripts here...
*/

var load = function(){
context.game.state.load('Menu', false, function(){

}, function(){

});
};

context.game.timer.add(1, load);
};

I've noticed that you added a timer to your examples. Is there a node method that gets called once the current state has finished loading? If so, I could call the next state on state load completion.
 
 
 
 

eric

Favor from: justinboyd5

Just like weism has said 'awake' is called when the state is still loding, you can add listener on state.onEndLoad event(http://docs.qiciengine.com/api/state/index.html)  
game.state.onEndLoad.add(function() {
// game.state.load
})

btw, you can check the prefab loading doc http://docs.qiciengine.com/man ... .html

eric

Favor from:

It's not allow to switch scene when the game is in loading scene. But this kind of conflict seldom accours, you should check your code logical, whether there are cases that calling game.state.load multi times?

weism

Favor from:

The current state is still being loaded when the function 'awake' called. 

justinboyd5

Favor from:

Hi Eric - It looks like the logic  your provided creates some kind of recursive loop where it loads between the current state and the new state that is being loaded. 
 
Here is my code:
context.game.state.onEndLoad.add(function(){
context.game.state.load('Preloader');
});
 
 

justinboyd5

Favor from:

This logic runs at the end of every state load. This is why it's causing a recursive loop, because it keeps going back to the Preloader state at the end of every state load. I think using a condition to determine which state is loading should solve this. I'll reply with an update when I get time to test this.
 
context.game.state.onEndLoad.add(function(){
context.game.state.load('Preloader');
});

 
Thanks!

eric

Favor from:

you can use 'game.state.current' to know current loading state

To reply to a question, please Login or registered