I have to create a game for my software development class and i’m trying to figure out how can I link my classes together to update for instance the health of the player when I go to a different screen.
I created a class player that contains all the methods and variables needed for a player for instance, health, ship, name, but say i get in a fight in the game, how can i keep updating the health because everytime I have to go to a new screen where I have to keep creating a new instance of player.
What is another way around this so its more OO oriented?
Thanks!!
Use events for things like this. Imagine there is a central place where all events are sent for distribution. Components of your game can then register listeners for certain events. Examples of events:
When someone updates the player health, the player instance sends an event “Health changed” with the new value.
The “display health” component could listen for this kind of event and update itself accordingly.
The beauty of this approach is that the health component need not know anything about the player. It just responds to a “Health(int)” event.