Global object between states

How can I create and use a global object between states without losing it's data?

My goal is to create a config object that can contain properties the user picks during in the menu state. Then these properties can be used during the game state.

Thank you!
Has invited:

eric

Favor from: justinboyd5

There are several issues going on:
1:  "qc.defineBehaviour" is for defining component script to be add on node, if you just want to define a normal Class that is nothing to do with specified node, then you should do it as normal JavaScript way:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
var Person = function (firstName) {
this.firstName = firstName;
};

Person.prototype.sayHello = function() {
console.log("Hello, I'm " + this.firstName);
};

var person1 = new Person("Alice");
var person2 = new Person("Bob");

// call the Person sayHello method.
person1.sayHello(); // logs "Hello, I'm Alice"
person2.sayHello(); // logs "Hello, I'm Bob"
2: Even you use "qc.defineBehaviour" to define a normal Class still works, but the code "window.GameConfig = new GameConfig();" have override the Class as a object instance, so when publishing, the UnglifyJS tool rename GameConfig Class to u, then GameConfig.myInitializer(); become u.myInitializer(); but u is a Class, it's not the instance object, so you got "Uncaught TypeError: u.myInitializer is not a function"
 
In short:
1: use the normal way to define normal JavaScript class
2: DONOT make instance object variable the same name as Class, at least you should lowercase first character for variable: window.gameConfig = new GameConfig();

eric

Favor from: justinboyd5

http://docs.qiciengine.com/manual/LoadingPrefab/index.html
node.ignoreDestroy = true;

From the source code of HTML page that run game, you can see the Prefab instance's ignoreDestroy property is set as true, so even after switching scene, the Prefab instance is still in the scene, just being displayed when swithing scene, after that it's hidden.

eric

Favor from:

Just save the global object in window.myGlobalObject, then you can get it whenever you want.
For example, in QICI Editor we use window.G to save all global varibles, so you can use window.G.** to debug in console 
G.hierarchy.activeNode.name = '青瓷引擎 Rock!'

Screen_Shot_2016-01-21_at_11.11_.31_AM_.png

 

justinboyd5

Favor from:

This works great, but it looks like it breaks my game when publishing it.
 
I get the following error:
Uncaught TypeError: u.myInitializer is not a function
 
Here is my code:
var GameConfig = qc.defineBehaviour('qc.engine.GameConfig', qc.Behaviour, function() {
}, {
});

GameConfig.prototype.myInitializer = function(){
console.log('hello config!');
};


window.GameConfig = new GameConfig();
GameConfig.myInitializer();

justinboyd5

Favor from:

Thanks Eric, you've been a big help with this!
 
I ended up using a normal Class definition to store config values. Is it possible to have a consistant global node that doesn't refresh between states?
 
It would be nice to be able to see these values rendered into the editor.

To reply to a question, please Login or registered