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 Logan

I'm trying to toggle my inventory with the (I) key, but I can't seem to get it working.. I'm really having trouble with determining if the Inventory is being shown or not?

Press (I) and the inventory displays but I can't figure out a way to get the inventory to kill or hide with the same key?

I've tried to set a boolean when the (I) key is pressed the first time to keep track when the Inventory is being shown and when it isn't but I still can't seem to get it right..

Any help would be greatly appreciated!

if( ig.input.state('inventory') && inv_disp == false ) {

	ig.game.spawnEntity(EntityInventory, 100, 50);

}

if ( !ig.input.state('inventory') && inv_disp == true ) {
			
	ig.game.removeEntity(EntityInventory);
										
}

1 decade ago by tobika

hi,
attention, on your second if you check if the input state is false, which is always when the key is not pressed, as soon as you remove or spawn your inventory you should change the value of you inv_disp variable
hope it works for you

UPDATE: ups wrong ;-) corrected after dominics post, but still not so good i guess ;-)

if( ig.input.pressed('inventory') ) {
    if( !inv_disp ) {
        ig.game.spawnEntity(EntityInventory, 100, 50);
        inv_disp = true;
     else {

        var inventory = ig.game.getEntityByName('nameofyourentity');
        // or if you only have one inventory entity
        var inventory = this.getEntitiesByType( EntityInventory )

        ig.game.removeEntity(inventory);
        inv_disp = false;
     }
}

btw, did you think about just putting a switch in your entity to disable the rendering of it? could be faster than always creating and removing an entity

1 decade ago by dominic

You probably want to use ig.input.pressed() instead of ig.input.state() in this case. See the documentation for the details.

Also, ig.game.removeEntity() takes an Entity instance as parameter, not an Entity class. Give your EntityInventory a .name (I assume there's only one of it anyway, so you could set this directly in the class definition) and remove it like this:

var inventory = ig.game.getEntityByName('something');
ig.game.removeEntity(inventory);

1 decade ago by Logan

Thanks guys, I don't know why I was checking if the key was not pressed? Ha, I must have worked it in when trying different things on accident.. I'll try out some of the above and see what I can work up, I never thought about using a switch with the Inventory. Thanks for clarifying the getEntityByName, and removeEntity!

Sincerly,
Logan

1 decade ago by Logan

Okay! I've got a working toggling Inventory! Here's how I did it:

if( ig.input.pressed('inventory') ) {
					
	if( !this.inv_disp ) { //If the Inventory is NOT being Displayed
						
		ig.game.spawnEntity(EntityInventory, 200, 50); //Shows the Inventory at set X & Y
		this.inv_disp = true; //Inventory Displayed set to true!
						
	} else {
						
		var inventory = ig.game.getEntityByName( 'inventory' ); //Declares the inventory var as EntityInventory
		ig.game.removeEntity( inventory ); //Removes the decalred inventory
		this.inv_disp = false; //Inventory Displayed set to false!
						
	}
				
}

Inside inventory.js

name: 'inventory'
Page 1 of 1
« first « previous next › last »