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 Nennad

How to preserve some player properties (for example, health) when moving from one level to another ? (im using levelchange.js).

For now it seems to me stats are reset every time player switches between levels.

I guess i must somehow prevent entity being reset, (exept x,y), or transfer health status to the entity in new level. Any experiences ?

1 decade ago by davidx

See playercontroller in this post.

http://impactjs.com/forums/help/inventory-system

1 decade ago by fugufish

to bring it down to basics, all you need is a global object which contains variables


// define the controller when game initializes
var playerController = {
   "score":0,
   "health":100,
   "armor":100,
   "kills":0
}

// when needed, just call...
ig.game.playerController.score+=scoreToAdd;

1 decade ago by Nennad

Was not easy for me, Impact beginner. Also, i am getting initial health variable with php. Code solution maybe not smartest, but working.

This is what i did main.js (in fact embedded in index.php)
PlayerProperties = ({

  realhealth: <?php echo $life; ?>
  
});

I am tracking and updating player health by watching at main.js update, if he is to low, game over level is called:

this.parent();
	PlayerProperties.realhealth = ig.game.getEntityByName('Puffpock').health;
	
		if( PlayerProperties.realhealth < 1  ) {
				this.loadLevel( LevelGameover );

			};

In player.js, im updating health property in init, so when he enters another level, health will be inherited

this.health = PlayerProperties.realhealth;

When player wants to save, realhealth will be dumped back to database via php/mySQL.

Hope this make sense :) Please Critique.

1 decade ago by Xander

I wanted to reopen the discussion on this topic.

I was easily able to use global variables to preserve my player entities properties, but I would have to spawn a new player entity with every level change. I don't really want to do this since I want to keep the player, health, map data, etc, preserved without reloading them with each level change.

I am attempting to use a variable called .global inside each entity that I want to preserve when I call loadlevel.

In game.js, I modified the loadlevel function so it now looks like this:

loadLevel: function( data ) {
		this.screen = {x: 0, y: 0};

		// Entities
		//this.entities = []; // This was the line that clears the enitities
		
		// My code to clear only entities that have not set .global to true
		for (var i = 0; i < this.entities.length; i++) {
			if (!this.entities[i].global )	{
				this.entities.splice(i,1); 
			}
		}
		this.namedEntities = {}; // I haven't used named entities, so I am ignoring this for now.
		for( var i = 0; i < data.entities.length; i++ ) {
			var ent = data.entities[i];
			this.spawnEntity( ent.type, ent.x, ent.y, ent.settings );
		}
		this.sortEntities();
		
		// Map Layer
		this.collisionMap = ig.CollisionMap.staticNoCollision;
		this.backgroundMaps = [];
		for( var i = 0; i < data.layer.length; i++ ) {
			var ld = data.layer[i];
			if( ld.name == 'collision' ) {
				this.collisionMap = new ig.CollisionMap(ld.tilesize, ld.data );
			}
			else {
				var newMap = new ig.BackgroundMap(ld.tilesize, ld.data, ld.tilesetName);
				newMap.anims = this.backgroundAnims[ld.tilesetName] || {};
				newMap.repeat = ld.repeat;
				newMap.distance = ld.distance;
				newMap.foreground = !!ld.foreground;
				newMap.preRender = !!ld.preRender;
				this.backgroundMaps.push( newMap );
			}
		}
		
		// Call post-init ready function on all entities
		for( var i = 0; i < this.entities.length; i++ ) {
			this.entities[i].ready();
		}
	},

Now, when I debug this in Chrome, it seems to work, my player entity which has .global set to true is not deleted. However, the entity I preserved does not get rendered once my new level has loaded!

What am I missing? Do I need to bind the preserved entities to the new level somehow?

EDIT: In addition to not rendering, the player entity I preserved also seems to be free-falling due to gravity (and, I assume, not interacting with the collision layer).

1 decade ago by alexandre

An alternative I would consider using first, were I to want the functionality you discuss, would be the local storage plugin. Have you considered using it?

1 decade ago by Xander

Thanks for the reply. I will be using local storage for game saves. But I am not interested in rebooting my entities with each level change, I want them to be persistent throughout the game.

I'd love if someone could figure out why my preserved entities don't seem to be bound to the new level! Thanks!

1 decade ago by bitmapshades

I am also struggling with making stats and items persistent across level exits. This is particularly annoying if like me you've coded saving stats to a game controller class so you can reload the player's current stats back.

1 decade ago by bitmapshades

I'm also unclear on how to save the player's last exit point so they return there next time they spawn on that level. Instead of the default position set by WM.

1 decade ago by bitmapshades

I've managed to solve my first problem by calling save and load functions in my game director class before and after each call to this.levelExit.nextLevel().

Main.js
//in update function:
if(ig.input.state('continue')){
    this.showstats = false;
    this.director.saveGameData();
    this.levelExit.nextLevel();
    this.director.loadGameData();
    $('.tip').hide();
    this.director.loadPlayerStats(this.getEntityByName('player'));
    this.parent();
}
Page 1 of 1
« first « previous next › last »