# How could I store different abilities in the player class?

So, I have a player class with basic actions. During the gameplay he unlocks new abilities. I don't know how to store new actions within the player class. To be precise - I know how, but my method is too messy and i want to clean up code. Also, I want to add to the enemies some of those abilities. In other words, every game entity(expect walls, perhaps :D) should have a collection to which i can add methods that extend entity functionality. How do i make this?

• It could help if you post the code you already have – Thomas Jan 27 '13 at 15:21
• @Thomas has a point, even if it's just some basic pseudo-code so we have a better idea of what path you're on. – user24851 Jan 27 '13 at 16:42
• @Thomas well, i posted it below :D – edwing Jan 27 '13 at 19:08

I recommend you approach used in the Artemis Entity System Framework. You can think about your player, enemy, wall etc. like about container (entity) for components. Then you precise actions (systems) for entities with particular set of components.
For example you can create system that allows player to fly when the X key is pressed (in-game condition) and when entity represented player has component flying (in-engine condition). Assume that player has ability for fly only if he has some wings in inventory. If this happen just add component flying to player entity and this will unlock flying for player.

• This is my first game and I want to make everything(within reasonable limits) by myself, but thanks! – edwing Jan 27 '13 at 18:38
• @edwing, I think it would be a good idea to familiarize yourself with the concept of an entity system framework like Artemis. This way you wouldn't have to discover things the hard (and long) way. – Wabbitseason Jun 22 '17 at 10:14

I've received a great reply on StackOverflow This problem can be solved with dictionary and lambda functions

public Dictionary<string,Action> Powers = new Dictionary<string,Action>();


And add new powers to this dictionary:

Powers["Double Jump"] = () => { //jump code };


Then just call whenever you want Powers["Double Jump"]();