1

I work on an online game server. For regeneration of player's hitpoints, I consider using timers. That is, if the player is online and his HPs or mana is lower than maximum, every minute these stats are incremented. Normally, there is no more than 50-80 players online at the same time. Is a wise approach to create a timer per class (an instance of Player )that would trigger an HP++ and Mana++ events?

Another idea I had was to have a timer on the main server thread, firing every minute, and having the players to subscribe for events.

Is there something wrong with this approach and is it reasonble, performance-wise?

2

Its a bad idea.

Say you have these 50 players all shooting off spells and replenishing thier mana all the time. Almost every second one of those timers will fire an event and thats just for mana. Add Hitpoints, monsters, ability cool downs etc and you'll soon have millions of timers eating up memory, bandwidth and threads.

A much better approach is to have a single timer a 'game loop' which runs say every second and checks to see what if anything needs to be sent out.

You needn't keep track of the time of each event either as they can often be calculated.

When a player is spawned you can keep track of that time and every game loop just take that off the current time and see if the remainder is a multiple of thier mana regeneration interval.

3

It depends on what timer you're using. Some timers will spin up a thread to signal when it's time to go, which would not be viable if you were making one per class.

Another common approach you might consider is just having a "time elapsed" sort of concept for each tick of your game loop. Instead of firing an event when a minute passes, you send along the time elapsed and the code for HP regeneration accumulates elapsed time and adds X HP per minute accumulated (with some remainder).

This tends to be more performant if your server hits a lag spike, or for events that fire more frequently than once per minute.

  • Well, there are is no "game loop", it is an online game when server reacts on messages. There is a thread handling NPC movement, but that is completely separate and would not like adding this kind of stuff to thread handling NPCs. – John V Oct 13 '17 at 13:08
0

Always try to minimize updates:

Does the amount of mana / hp have any implications other than you might have too little to survive (dead on hit) respectively use an ability (inability to execute it)?

In that case, consider saving two items: Status at last update, and time of last update, and defer the update to the next time you have to make one.

If other important attributes are influenced by a threshold, you can elaborate on that, if on the exact amount / percentage, you actually cannot evade the need for continuous updates.
Even then, consider whether you can get away with updating everything which must be updated using only a single timer, though maybe staggered to avoid freezing the whole world for too long.

  • The mana is checked anytime the spell is to be casted, the HP is modified anytime a player gets hit. My problem is that I want the HP and Mana to be regenerated, i.e. the client needs to receive an update every minute that the HP was increased. – John V Oct 13 '17 at 13:10
  • Looks like a prime candidate for interpolation from status/time of last event, both on the server and for any real-time client-side display. – Deduplicator Oct 13 '17 at 13:11
  • An example: After the fight, the player wants to regenerate, so he hides and wait. In that case, his HP should increase, minute by minute. Basically, whenever HP<HPMax, it should increase minute by minute. – John V Oct 13 '17 at 13:12
  • Sure, but there's no need to artificially chop it up into pieces. Inform the client that he has X hp, Xm hp_max, Xr hp/min, server-time, and you only need to send an update for unforseen interruptions. If you want to spam N hp healed messages for a log / animation / whatever, let the client do so locally. – Deduplicator Oct 13 '17 at 13:15
  • But then I still need to update the HP on the server every minute, so there is anyway needed some sort of timer. – John V Oct 13 '17 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.