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

1 decade ago by rootbeerking

Hello, this is probably gonna be a quick and easy question: I'm wondering where exactly can I put global variables in Impact? Right now I'm putting them in the index.html, but this doesn't seem like a good idea in the long run. Is there an other place some where inside my actual Impact code where I can put my global variables?

I've tried putting them in the update function of my main.js, but that didn't work.

1 decade ago by dominic is an alias for the global scope - i.e. the window object. You can put your variables there: = 'someValue';

However, if your variable "belongs" to the game, it's probably better to attach it to your game class:
MyGame = ig.Game.extend({
	myVar: 'someValue',	
	init: function() {
		this.myVar = 'someValue' // same as above

You can then globally access the current game instance via - so for your variable:

Edit: You might also want to read about Variable Scope and the var keyword.

1 decade ago by rootbeerking

Thank you very much Dominic, that's all very helpful information. As always, I really appreciate the help.

1 decade ago by Kxen

What's the benefit of using instead of just a normal (global) variable? To be honest I've never seen a "normal" variable in any ImpactJS code anywhere so I'm starting to think there's something wrong with doing that.

Simple example of how I use them to limit the amount of shots a player can fire at a time:

In the player entity:

init: function( x, y, settings ) {
	this.parent( x, y, settings );
	bullets = 0;

In the player entity's update function:

if( ig.input.pressed('shoot') ) {
	if ( bullets < 3 ) {
		bullets++; EntityBullet, this.pos.x, this.pos.y + 7, {flip:this.flip} );

In the bullet entity:

	handleMovementTrace: function( res ) {
		this.parent( res );
		if( res.collision.x || res.collision.y ) {

Is this wrong?


1 decade ago by dominic

It's not wrong, no.

The advantage of using is that it's more expressive. You instantly know it's a global variable, without having to read the whole function to see if the variable is just defined locally.

1 decade ago by Kxen

Thanks for the swift reply. I ended up making the bullets variable a property of the player entity instead and added the following in the Game class (main.js):

	loadLevel: function( data ) {
		this.parent( data );
		// Find the player once at level load
		this.player = this.getEntitiesByType( EntityPlayer )[0];

I can then change the bullets property of the player within the bullet entity using Makes more sense I think...

Thanks again.

1 decade ago by mmaxwell

I know this is old, but another immediate benefit to using vs window.variable is that you don't clutter the global namespace. It allows for a much less chance that something accidentally collides. Namespace collision errors in JavaScript can sometimes take a long time to debug and can often be frustrating.

Also, to follow up on this, Dominic, will you be adding an option to make Weltmeister save levels under the namespace? Currently, I'm having to go in and change the saved JS files.

1 decade ago by Ant101

@mmaxwell - but is just an alias of window, so it wont help with name collisions.
Page 1 of 1
« first « previous next › last »