1 decade ago
by skel1
This might be a little more of a question about javascript than impactjs its self.
What would be the recommended approach to include external javascript files as variables in ig.game?
For example, I have
main.js
entities/player.js
what I would like is
main.js
conf/weapons.js
entities/player.js
where weapons.js would look something along the lines of
ig.game.weapons=
{
sword: { dmg: 1 },
pen: { dmg: 2 }
}
I realize I can just create a blank entity, but this doesn't feel intuitive to me because entities do not persist between map loads and they contain parent calls that aren't necessary for strictly configuration.
Thanks for any insight!
1 decade ago
by skel1
I see how it works now. I looked at _loadScript in impact.js to see how things were loaded.
I'm able to do what I wanted by creating a new file
conf/items.js
ig.module(
'game.conf.items'
)
.requires(
'impact.game'
)
.defines(function(){
ig.Game.prototype.ItemCatalog = {
item1: 1,
};
});
Then adding game.conf.items to the requires in main.js.
1 decade ago
by Jaha
the only problem i see with that is what happens when you change game states via
ig.system.setGame(MyDifferentGameState);
? Perhaps you could just extend
ig.Class
in your module and either define it as a global or attach it to
ig
.
ig.module(
'game.conf.items'
).requires(
'impact.game'
).defines(function(){
ig.global.ItemCatalog = ig.Class.extend({
item1: 1,
});
});
require the module in main then initialize it in your games init
this.itemCatalog = new ItemCatalog();
Im by no means an expert and also still new to Impact so take it with a large grain of salt. Just seemed a bit hacky to mess with the prototype. ;)
1 decade ago
by riceje7
I agree with Jaha, it does seem a little hacky messing with prototype, to have objects available in the
ig.game
namespace you don&
039;t need to mess with prototype, or even extend #ig.Class
. What you should be doing is
injecting
the code you need into
ig.Game
(capital G not lowercase).
Check out the docs here. Basically in reference to your original post, it would work like this: create a module that just injects into
ig.Game
any data you need. then require that in your
main.js
. Thats it. so for example
weapons.js
would look something like this:
##
ig.module('conf.weapons').requires('impact.game').defines(function(){
ig.Game.inject({
weapons: {
sword: 1,
pen: 2
} // and so on...
});
});
##
And Jaha theres no reason to worry about preserving data between game states you can use my handy dandy
PersistantData Plugin. it allows you to transfer data between instances of ig.Game.
Page 1 of 1
« first
« previous
next ›
last »