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

6 years ago by dungeonmaster

I went all over the forum last 2 hours and did a lot of hair pulling but no good. So;

I want to define a global variable/property and then use this everywhere in my code, from property definitions of other classes and such.

I tried this in main.js just before calling ig.main(....):
ig.Game.tilesize = 64;
ig.main( '#canvas', MyGame, 60, 1024, 768, 1 );

Then I wanted to use it in an entity definition like:
EntityGun = ig.Entity.extend({
	range : 4*ig.Game.tilesize,
...

And I got this error:

TypeError: 'undefined' is not an object (evaluating 'ig.Game.tilesize')

So how can I create a variable that I can freely use anywhere within game code, without requiring any module etc..?

6 years ago by Graphikos

Anything with the window scope would be global. You can use ig.global which is an alias to window.

ig.global.tilesize = 64;

Your variable should work also but you should use ig.game not ig.Game.

6 years ago by dungeonmaster

Very well, this worked for the Entity. Thanks alot :)

However for the game instance it's kinda not working:

MyGame = ig.Game.extend({
	tilesize : ig.global.tilesize,
	init: function() {
		console.log("MyGame online with tilesize:"+this.tilesize);
		this.tilesize = ig.global.tilesize;
		console.log("MyGame online with tilesize NOW:"+this.tilesize);
..

I don't get an error, but when I look the console I see:

MyGame online with tilesize:undefined
MyGame online with tilesize NOW:64

Any ideas?

6 years ago by vincentpiel

We might call what you seek a 'namespace' : ig is such a namespace. Do not hesitate to create your own, so since your pseudo is dungeonmaster, it is as simple as writing :

  window.dm = {} ;

so put this definition inside a module, and then you can add any properties you want to within this module , like :

  dm.tileSize         = 25    ;
  dm.flowerCount  = 8      ;
  dm.flowerPower  = true ; 

Once you done that, you have to 'require' this module anywhere you need it, and you're done.

6 years ago by dungeonmaster

Hi again,

I thought the solutions worked but they didn't.

The problem is, the ig.global.tilesize only becomes available AFTER the preloader is finished.

This means, when I tried to use that in preloader, it is undefined. Creating a namespace as vincentpiel suggested also hits the same wall.

I want to use a variable that is available in preloader-time, like:
EntityTower = ig.Entity.extend({
	centerOffset : ig.global.tilesize/2,
	animSheet: new ig.AnimationSheet( 'media/laser-canon.png', ig.global.tilesize, ig.global.tilesize ),

as a reminder, this didn't worked:
ig.global.tilesize = 64;
ig.main( '#canvas', MyGame, 60, 1024, 768, 1 );

I edited impact.js like:
window.tilesize = 64,

Now it worked. But this approach is not upgrade-safe, I need to modify the file every time I upgrade impact.

So I still need some insight :(

6 years ago by drhayes

Did you do this?

ig.module(
	'dm'
)
.defines(function() {
  window.dm = {
    catpants: true
  };
});

And then, in your entity, do this:

ig.module(
  'game.entities.player'
)
.requires(
  'impact.entity',
  'impact.timer',

  'dm'
).defines(function() {
    EntityPlayer = ig.Entity.extend({
      cat: dm.catpants,
      // ...
    });
});

Just checking because that will force your dm module to get loaded before the entity so that the properties become available as dm. If that's not working for you, then there could be deeper problems.

If none of this is still working, you can of course declare the variable in your index.html in a <script> tag. Not ideal, but at least it unblocks you while you investigate.
Page 1 of 1
« first « previous next › last »