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 Eddie

Okay, so we're building a game where the character's stats are based on an array of information we're calling from an external site (for the purposes of what I'm doing, the external site is unnecessary).

So we have this object in Javascript:

var stats = {"nike_runs":[{"start_time":"2011-03-11T19:14:44Z","calories":12.0,"distance_miles":"0.10","total_seconds":288.0,"average_pace":"50.47"},
{"start_time":"2011-03-11T19:41:25Z","calories":7.0,"distance_miles":"0.06","total_seconds":559.0,"average_pace":"165.19"},
{"start_time":"2011-03-11T20:27:45Z","calories":197.0,"distance_miles":"1.63","total_seconds":8434.0,"average_pace":"86.22"}]}

And this piece of code that should retrieve the calories from each array item and add them up:

function getExp (somestats) {
var exp = 0;
for (var i = 0, l = somestats.nike_runs.length; i < l; i++) {
exp += somestats.nike_runs[i].calories;
}
return exp;
}

My problems so far are the following:
1) Clearly Impact uses a different syntax so me adding this code into it isn't working due to syntax (I think).
2) Ultimately I'd like to have that function in a separate file altogether instead of inside my entity file, so is there a way my entity can call a function from an external JS file?

Thanks guys! :D

1 decade ago by Jerczu

Ad1 - No it doesn't... you can easily set a global variable holding this array and you can access it anywhere in the Impact code.

Ad2 - why not set it as a part of main.js?

1 decade ago by Eddie

Re-Ad1: Okay, how? From what I can see, Impact variables are declared as

var: (<- semicolon) properties, (<- comma)

Whereas my code is in the Javascript format

var = (<- equal sign) properties; (<- semicolon)

So how/where would I declare it? In addition, what about the function?

Re-Ad2) The same problem applies, even if I put it in main, the functions seem to be syntaxed

var: function(parameters) {
code
}

As opposed to my

function var(parameters) {
code
}

So how would I create this custom function in the main? I tried looking for Impact documentation on how to create custom functions and such, but there doesn't seem to be one... My guess is this is a lesson in Javascript I haven't had, and so Impact expects you to know how.

Does my confusion make sense?

1 decade ago by Jerczu

Ok -

In your main.js
.defines(function(){

MyGame = ig.Game.extend({

mySpecialVar:{"nike_runs":[{"start_time":"2011-03-11T19:14:44Z","calories":12.0,
"distance_miles":"0.10","total_seconds":288.0,"average_pace":"50.47"},
{"start_time":"2011-03-11T19:41:25Z","calories":7.0,
"distance_miles":"0.06","total_seconds":559.0,"average_pace":"165.19"},
{"start_time":"2011-03-11T20:27:45Z","calories":197.0,
"distance_miles":"1.63","total_seconds":8434.0,"average_pace":"86.22"}]},

//from now on your special var should be available 
//to any entity and can be called as 
//ig.game.mySpecialVar

//the same way you can define your function 

getExp:function (somestats) {
var exp = 0;
for (var i = 0, l = somestats.nike_runs.length; i < l; i++) {
exp += somestats.nike_runs[i].calories;
}
return exp;
}

now ig.game.getExp(ig.game.mySpecialVar) should run your function and return the value ( I didnt check your function code)

also

In your index.html you can always add extra script file that will hold your code

<script type="text/javascript" src="myscriptfile.js"></script>

and as long as you define your variables without var in front of them you will be able to acces it (i just tested it in my game and it works).

1 decade ago by Eddie

Thanks a bunch, dude, you're the man.

Last question, to generalize the example:
If I want to define a global variable within a class (main in this case, but say in an entity or a level or whatever), I just have to write it under the defines function? And the same for global functions?

Again, much appreciated, you have just imparted incredibly useful knowledge on me.

1 decade ago by Jerczu

Or simply in your index.html drop it as inline code - Also tested -

<script type="text/javascript">
		myVar = "whatever";
		function myFunction(){
			console.log(myVar);
		}
	</script>

if you call myFunction() from any point in the game it will run the function.

1 decade ago by Jerczu

Quote from Eddie
Thanks a bunch, dude, you're the man.

Last question, to generalize the example:
If I want to define a global variable within a class (main in this case, but say in an entity or a level or whatever), I just have to write it under the defines function? And the same for global functions?

Again, much appreciated, you have just imparted incredibly useful knowledge on me.


Yes but you will have to get the entity reference in any other point of your code so lets say your EntityPlayer has a function or variable you want to access from EntityEnemy...

in your entity enemy you will have to reference your player this way

var player = ig.game.getEntitiesByType(EntityPlayer)[0]; - this will return the first entity in an array of entities type EntityPlayer.

from now on you can access your function call or attribute simply calling
player.myFunction() or player.myVar as long as function/var was defined as a property

1 decade ago by Jerczu

or if you define a name property of your entities you can always use ig.game.getEntityByName("entityname") - bear in mind name is not the same as type.

1 decade ago by Eddie

I did what you said and it didn't work. Allow me to show you:
MyGame = ig.Game.extend({
	
	statsArray:
	{"nike_runs":
		[{"start_time":"2011-03-11T19:14:44Z","calories":12.0,"distance_miles":"0.10","total_seconds":288.0,"average_pace":"50.47"},
		 {"start_time":"2011-03-11T19:41:25Z","calories":7.0,"distance_miles":"0.06","total_seconds":559.0,"average_pace":"165.19"},
		 {"start_time":"2011-03-11T20:27:45Z","calories":197.0,"distance_miles":"1.63","total_seconds":8434.0,"average_pace":"86.22"},
		 {"start_time":"2011-03-12T03:00:20Z","calories":31.0,"distance_miles":"0.25","total_seconds":1906.0,"average_pace":"125.11"},
		 {"start_time":"2011-03-12T17:11:37Z","calories":492.0,"distance_miles":"4.06","total_seconds":4733.0,"average_pace":"19.41"},
		 {"start_time":"2011-03-18T17:36:33Z","calories":276.0,"distance_miles":"2.28","total_seconds":1716.0,"average_pace":"12.55"}],
	"fitbit_days":
		[{"date":"2011-04-25","calories":2311.0,"distance_miles":null,"steps":null,"finalized_data":false,"pct_day_sedentary":"100","pct_day_light_activity":null,"pct_day_middle_activity":null,"pct_day_heavy_activity":null},
		 {"date":"2011-04-26","calories":2311.0,"distance_miles":null,"steps":null,"finalized_data":false,"pct_day_sedentary":"100","pct_day_light_activity":null,"pct_day_middle_activity":null,"pct_day_heavy_activity":null},
		 {"date":"2011-04-27","calories":2311.0,"distance_miles":null,"steps":null,"finalized_data":true,"pct_day_sedentary":"99","pct_day_light_activity":"0","pct_day_middle_activity":"0","pct_day_heavy_activity":null},
		 {"date":"2011-04-28","calories":2289.0,"distance_miles":null,"steps":1195.0,"finalized_data":true,"pct_day_sedentary":"96","pct_day_light_activity":"3","pct_day_middle_activity":"1","pct_day_heavy_activity":null},
		 {"date":"2011-04-29","calories":2140.0,"distance_miles":"3.53","steps":5615.0,"finalized_data":true,"pct_day_sedentary":"57","pct_day_light_activity":"10","pct_day_middle_activity":"5","pct_day_heavy_activity":"1"},
		 {"date":"2011-04-30","calories":2551.0,"distance_miles":"5.11","steps":11480.0,"finalized_data":true,"pct_day_sedentary":"70","pct_day_light_activity":"19","pct_day_middle_activity":"11","pct_day_heavy_activity":"0"},
		 {"date":"2011-05-01","calories":2308.0,"distance_miles":"3.27","steps":7329.0,"finalized_data":true,"pct_day_sedentary":"31","pct_day_light_activity":"15","pct_day_middle_activity":"7","pct_day_heavy_activity":"1"}]
	},
	
	getExp: function(someStats) {
		var exp = 0;
		for (var i = 0, l = someStats.nike_runs.length; i < l; i++) {
			exp += someStats.nike_runs[i].calories;
		}
		return exp;
	},

        ...

And

EntityPlayer = ig.Entity.extend({
           
        // Set properties here
        size: {x:24, y:26},
        
        maxVel: {x: 100, y: 200},
        friction: {x: 600, y: 0},
        
        type: ig.Entity.TYPE.A,
        checkAgainst: ig.Entity.TYPE.NONE,
        collides: ig.Entity.COLLIDES.PASSIVE,
        
        animSheet: new ig.AnimationSheet( 'media/images/sprites/trial/pikasheet.png', 28, 26),
        
        flip: false,
        accelGround: 400,
        accelAir: 200,
        jump: 200,
	
	stats: ig.Game.statsArray,
	
	exp: ig.Game.getExp(stats),

       ...

And I got an error:

Uncaught TypeError: Cannot read property 'statsArray' of undefined.

Seems like there's an issue with calling ig.Game.statsArray... Fml.

1 decade ago by Jerczu

hold on.

1 decade ago by Eddie

Nope, didn't work :/ Still getting the same TypeError.

1 decade ago by Eddie

Although that made a lot of sense... That was a dumb mistake, my apologies.

1 decade ago by Jerczu

So it works?

1 decade ago by Eddie

The error comes from line 37, aka

stats: ig.game.statsArray,

Not the line you corrected. Although I'm sure that would've come up sooner or later, so thanks.

1 decade ago by Jerczu

EntityPlayer = ig.Entity.extend({
           
        // Set properties here
        size: {x:24, y:26},
        
        maxVel: {x: 100, y: 200},
        friction: {x: 600, y: 0},
        
        type: ig.Entity.TYPE.A,
        checkAgainst: ig.Entity.TYPE.NONE,
        collides: ig.Entity.COLLIDES.PASSIVE,
        
        animSheet: new ig.AnimationSheet( 'media/images/sprites/trial/pikasheet.png', 28, 26),
        
        flip: false,
        accelGround: 400,
        accelAir: 200,
        jump: 200,
    
    stats:null,
    
    exp:null,

 init: function( x, y, settings ) {
        this.parent( x, y, settings );
     this.stats =  ig.game.statsArray;
    
	this.exp =  ig.game.getExp(this.stats);

Did that in my game and it works like a charm

1 decade ago by Jerczu

try it it does work

1 decade ago by Eddie

HAHAHA! Look what I got:

Uncaught TypeError: Object function Class() {
if( !initializing ) {

// If this class has a staticInstantiate method, invoke it
// and check if we got something back. If not, the normal
// constructor (init) is called.
if( this.staticInstantiate ) {
var obj = this.staticInstantiate.apply(this, arguments);
if( obj ) {
return obj;
}
}
for( var p in this ) {
if( typeof(this[p]) == 'object' ) {
this[p] = ig.copy(this[p]); // deep copy!
}
}
if( this.init ) {
this.init.apply(this, arguments);
}
}
return this;
} has no method 'getExp'

-sigh- This is killing me.

1 decade ago by Eddie

I removed the init calls and the game launched, but now I get some error saying

Uncaught TypeError: Cannot call method 'toString' of null

which leads me to believe that exp isn't being set to anything since that's what the error suggests.

1 decade ago by Jerczu

copy my code it will work plus use lower case game

1 decade ago by Eddie

-GASP!- It worked o_o

1 decade ago by Eddie

Or at least it seems like it did, give me just a moment to double check the values.

1 decade ago by Jerczu

I really dont understand where your errors come from I basically copy and paste the code you gave here - set the elems stats and exp to null and assign ig.game.stats and ig.game.getExp(stats) inside the init function of player entity and it works perfect i get result when i console out the this.exp.

1 decade ago by Eddie

YUUUP! Worked like a charm.

My friend, I cannot thank you enough, you have taught me more about Impact than any documentation has thus far.

I will remember this random day where a stranger in the Impact forums solved the biggest obstacle to my game yet.

1 decade ago by Jerczu

Heh glad to be of help you can contact me on Twitter @CDGN if you require any more help
Page 1 of 1
« first « previous next › last »