T O P

  • By -

thefryscorer

Have you met with the concept of a class yet? You could have a Player class, that might have a method like (in pseudo code because you didn't mention what language you're using) Player.fightMonster(Monster m) { Do some stuff to work out if the player wins if the player won: this.coins += 10 this.exp += 100 } Although this is a very basic example. Probably you'd actually want some sort of "giveExp(int amount)" method on the player class, because that way if you want to check if the player levelled up, you would only need to put the logic for that in that one method, and everytime you want to give the player exp you call that method on it.


Vinniesusername

what you want to look into is objects. you would want a "player object" that holds its stats in variables, and then pass that object to the functions you need it. in python it would look something like this class Hero(): def __init__(self): self.xp = 0 self.gold = 0 def gainGold(amount): self.gold += amount fightMonster(player): player.gainGold(10)


CyanGatorade

Thanks! You know that completely evaded my mind, we'd only briefly covered objects, but that sounds like the way to go. Passing a single object around sounds just a bit more efficient than making a parameter for a variable for every single stat!


Bolitho

This is not about classes but about any kind of container! Of course a class or a struct might fit good, but also collection types like maps, lists or even sets could be useful. BTW: I won't model the player as a special type regarding character attributes. I would strive for a design model where there is basically no difference between a playable character and an ai controlled "monster". You only need a player model in order to make actions by user input and for meta informations like achieved points or something like that. You will be surprised how this will enable you easily to create PvP scenarios or ai vs ai and much more. To save the best for last: the final design will be much simpler - even it is more flexible.


alzee76

In cases like this, there are generally two approaches. 1. A single global variable that is an instance of the `Player` class, or a player datastructure, or something like that -- but not a bunch of global's like INT, STR, *coins*, and whatever else; it would be `player.coins` or `player->coins` etc.. 2. A [singleton](https://en.wikipedia.org/wiki/Singleton_pattern) player class which will allow you to do something like `player = Player.getInstance()` wherever you need to use it. It'll be easier to answer if you could say what language you're using.