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 lazer

I'm curious as to what you all consider best practice or your preferred method of referring to the player entity (or any other entity, really) frequently throughout the game.

For example, my player entity has to interact with may other objects and in-game elements that all do different things. As a result I have to refer to the player a lot from different entity files and custom classes. Currently my player entity is set up as a global variable, so I've been using "ig.game.player".

However, I'm aware that we should really try to stick away from global variables as much as possible - is there a better method I can implement consistently to refer to the player entity on a regular basis throughout my code? Creating a variable separately in every single function with getEntitiesByType seems messy, but is this a better way?

1 decade ago by Graphikos

What's wrong with a global variable? I would think that would be much better than looping through all the entities every time (with getEntitiesByType) to get the player. Are you spawning it or is it being created from WM?

1 decade ago by lazer

It's being created from WM. From everything I've heard global variables are discouraged because using them throughout the code can cause conflicts and they can get difficult/messy to keep track of.

1 decade ago by Donzo

Hi.

From what I understand
(as read on here from prior posts),

getEntitiesByType can be cumbersome because it cycles through all entities each time it is called.

Rather, I name my entity and then use getEntityByName.

1 decade ago by flashingpumpkin

Hey

I&039;ve got the player set up as a distinctive subclass of #ig.Entity. I think it&039;s perfectly fine to then spawn it and assign it to #ig.game.player, or window.player if you dare. ;)

Nah, seriously. JS people stick away from global variables so they don&039;t pollute the namespace. An easy way out though is to create your own namespace like Impact does with #ig and then have all your frequently accessed game entities in there. Such as

Coffee Script
foo = models: player: ig.Entity 
foo.player = ig.game.spawnEntity foo.models.Player 0, 0, name: 'player'
Page 1 of 1
« first « previous next › last »