Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by gxxaxx

I have a short list of vars (mostly config parameters) that I want to store and make available globally.

My first inclination was to use parameters in "main" for this purpose.
But my entities are instantiated before main. Or at least "ig.game.myparam" is undefined when the entities are being instantiated.

To handle this I have been using a post init so that certain things are accomplished during the first update cycle rather than in init.

So here's my bright (???) idea.

Create a singleton-ish class

ig.module(
	'game.entities.special.vault'
)
.requires(
	'impact.impact'
)
.defines(function(){

ig.MyVault = ig.Class.extend({
	player: null,
	world: 'flat',
	param2: '',
	etc: true,
	
    init: function(theGame){
		this.kill();
    }

});

});

Then I can reference parameters in this class using something like the following:

		ig.MyVault.prototype.world = 'side';

So the question is, what do you think?

Is this workable?

Or, is there something about this approach that will blow up in my face?

I would really like to have a place to put parameters that exists before my entities "init".

Thanks for any clues,

1 decade ago by dominic

If I understand correctly, you just need global variables? You can use ig.global.someVar (which is just an alias for window.someVar) for that.

If you want to have some more semantic meaning, you can also attach these globals to a Class (not a class instance). E.g.: for config parameters that are specific to your game:

MyAwesomeGame = ig.Game.extend({
	/* ... your game class ... */
});

MyAwesomeGame.someVar = 42;


// Anywhere else:
MyAwesomeGame.someVar; // 42

Some of Impact's own classes (e.g. ig.Sound) do this as well.

1 decade ago by gxxaxx

Thanks for the quick answer dominic.

I would very much like to attach these globals to a class.

Alas, my fledgling efforts are not bearing fruit.

So far everything I try shows the parameters defined inside the class as not existing when player or main are being init-ed.

Where specifically should I put this code?

And would I extend Game or Class as in:
ig.Sound = ig.Class.extend({
	path: '',
	volume: 1,
	currentClip: null,
	multiChannel: true,

Thanks,

1 decade ago by dominic

Quote from gxxaxx
So far everything I try shows the parameters defined inside the class as not existing when player or main are being init-ed.
Yes, because if you define it inside your class, it&039;s an instance variable. Look closer at my first example: #someVar is not inside the class definition.

And you should already have a game class. Usually it&039;s in your main.js and called #MyGame.

Maybe this helps:
MyGame = ig.Game.extend({
	someInstanceVar: 'foo',
	
	/* ...lot's of other stuff here... */
});

MyGame.someClassVar = 'bar';

// This creates an instance of the MyGame class. 
// Don't do this by hand, your call to ig.main() already does 
// this for you once the preloader finishes.
ig.game = new MyGame(); 


console.log( MyGame.someClassVar ); // => bar
console.log( ig.game.someClassVar ); // => undefined

console.log( MyGame.someInstanceVar ); // => undefined
console.log( ig.game.someInstanceVar ); // => foo

1 decade ago by gxxaxx

Yes, that makes it more clear.

Thanks for being persistent. Just didn't get it the first time.

1 decade ago by paulh

You can also just spawn an invisible entity and make the variables in its init global?


ig.game.variable = x;


then i can access these variables from anywhere?

Is the entity method worse than using a class?
Page 1 of 1
« first « previous next › last »