Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Most efficient way to update Target HP bar??

Discussion in 'Scripting' started by DemeDev, Feb 9, 2019.

  1. DemeDev

    DemeDev

    Joined:
    Oct 18, 2017
    Posts:
    7
    Hello, sorry if my english is bad. Also, I haven't networking knowledge but I'm trying to design my game for multiplayer, so I don't know if my question will be correct.

    I have this problem:
    I have an HP bar for the target in my GUI. So everytime I do damage or SOMEONE ELSE does damage to the same target (enemy) I want this HP bar to update in the GUI of every damage dealer (player).

    The question is, what is more efficient??
    Use a coroutine for every damage dealer that updates this HP bar every frame?? or... make the target manage a list with every damage dealer (players) and update their target HP bar everytime its HP changes???

    I hope my question has sense and you understood it.
    Greetings.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Since the bar is only drawn once at the end of the frame (perhaps during LateUpdate()), accumulate the damage dealt during Update, and apply it only at the end.

    Since this is a multiplayer game, you should seriously consider changing the way you deal damage: if any peer can change the damage, you open your game up for exploits, and (worse) inconsistency (if a peer determines teh hit, but the update gets lost in the network). So update your damage code so that one one (the 'game master') decides if something hits, and then update the damage centrally, then pass the new health value to all connected clients.That wayy, even if a player drops one update, they'll automatically resync the correct health with the next update. Do the same to determine if a player dies: decide centrally.
     
    DemeDev likes this.
  3. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    533
    Updating a health bar in any Update method is bad, you should use a event and only update the health bar when the value actually changes. And since this is a network game you will most likely need to send some "health changed" message/command to the other players anyways so you can update the health bar there.
     
    DemeDev likes this.
  4. DemeDev

    DemeDev

    Joined:
    Oct 18, 2017
    Posts:
    7
    I think both of you said I should update the bar only when the HP is changed, so never use update methods or coroutines for checking the hp everytime.

    Then I should use some kind of List (Collection) to manage the players who are targeting the same target, is that correct???
    So when the target is damaged, this target sends the new HP value to every player targeting it and then every player can update their own the target HP bar.

    I don't really know if it works like this with networking
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    You should collect all damage that is done during the frame, and at the end of the frame, you should apply damage. All damage collection and application should only happen on the 'master' computer in the game (usually the one that selects the map/scenario), and then broadcast to all clients who will update only the damag dispaly for the object(s) that received damage. Since the master is always also a client, receiving the damage will cause it to also update its damage display.
     
    DemeDev likes this.
  6. DemeDev

    DemeDev

    Joined:
    Oct 18, 2017
    Posts:
    7
    Ok I was missing some points because I don't know about networking.
    I understand better now and thanks for that detail about collecting the damage until the end of the frame. But when you say the master broadcast to all clients. Who are those clients? I mean you have to collect those people targeting the same enemy to send the new HP to them.
     
  7. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Those clients would be the other devices that run your game in network play.
     
  8. DemeDev

    DemeDev

    Joined:
    Oct 18, 2017
    Posts:
    7
    I know what is a client. But you dont want to send the HP from a single mob to 1000 players. Just those who are targeting it. I think I was right when I said I would need to collect those players on a List
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You're right that you shouldn't broadcast it to everyone (even though that's an acceptable approach for first prototypes).

    But I'd also say do not only update the ones that target the monster, but everyone that's potentially interested in its HP - this includes players close to that monster. E.g. look up proximity checks.

    You want to do that, because some things that are purely visual can benefit from it on the client-side. Suppose a low-hp monster gets additional markes, or effects, or animates differently - that's normally something you try to resolve on the client side only, because - given that you seperated it correctly - they're of pure visual nature and often can be inferred from the HP value (sometimes animation sync is needed, but that's already polishing).

    This makes syncing less complex, reduces RPCs, and might use less network bandwidth / overhead than synching the value based on a large set of different conditions (you may only have that one condition which is "is targeted? true / false", but don't block yourself from being flexible for additional features in the future).
     
  10. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Whoa. You never said you are developing a mmxx. Yes, in that case a check against a reasonable precondition is mandatory. Just how was that ever a part of the discussion. From your request it wasn't obvious that you have experience building a multiplayer game, much less a mmo. My apologies.
     
  11. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    IF the game is top down you have a easy way of telling which players see what.