Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Third Party [PUN2] Abstracting the multiplayer logic from the base game logic

Discussion in 'Multiplayer' started by PascalTheDog, Jan 21, 2022.

  1. PascalTheDog

    PascalTheDog

    Joined:
    Mar 16, 2015
    Posts:
    86
    Hey,

    I've been tasked with developing an FPS fgame, and it's been decided that it should feature a multiplayer mode. I'd say I'm a decent programmer; I can whip up elegant prototypes fairly quickly, and I had a simple FPS setup with shooting, health, score, interface and all sorts of fancy extras set up in no time — all modular, using UnityEvents. Now I have to make it multiplayer (using Photon PUN2), and quite frankly I'm pulling my hair and hemorrhaging time.

    I have no prior experience in multiplayer programming, and it shows: I suck at it. Chances are, someone else is gonna take over the multiplayer aspect of the project eventually, but in the meantime I still have to deliver functional multiplayer builds. What I feel should take me minutes is taking me hours; I'm basically grasping at straws, and as a result I keep having to rewrite my base code in the meager hope that it would work as intended in multiplayer.

    I wish there was some simple way to have a dedicated code layer deal with going multiplayer. Is there any way to "abstract" the multiplayer logic from the base game logic? It would be great if I could work as if the game were a solo experience, and then have separate classes (which eventually I hope someone else would write) handle the multiplayer bit. Ideally my Health class for instance wouldn't have to handle PhotonViews, ownership, streams, RPCs or anything — just the base code handling the practical hurting/healing/dying/reviving.

    Is that at all possible?

    Cheers.
     
  2. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    If you really have everything setup already event-based you've done most of the work. That was a great decision. If you really want to skip the networking and let someone else do it they should theoretically be able to hook into your existing events for everything. If you make sure to make your components (like your Health class) with inheritance in mind that will help even more. Then whoever does the networking can take your Health and make NetworkedHealth that inherits from it.
     
  3. PascalTheDog

    PascalTheDog

    Joined:
    Mar 16, 2015
    Posts:
    86
    Thank you for your response! I had not considered using inheritance as a way to separate the base code and the multiplayer logic.

    NetworkedHealth would have access to events like OnHealthChange, OnHurt, OnHeal, OnDie, OnRevive, OnSpawn, etc from its Health parent class. But how would it use those events to networking ends? Wouldn't the base code interfere?
     
  4. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    It shouldn't interfere. Of course there are places you'd likely have to tweak things. If you base health component keeps track of the player health and handles OnHealthChange, for example, in NetworkHealth you could override OnHealthChange to update the Photon PlayerProps for that player and set their new health value, which would then sync it for all clients.
     
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,076
    This is quite an uncommon approach. Yes, some updates/events work well for making them networked but all the indirections could make it quite convoluted. You not only have to worry about sending the state to everyone else but also how they receive and apply it.
    What happens if your character control scripts are not aware of multiplayer? Others join your game and your scripts will attempt to control all characters. Or someone has to network the local character to a bunch of proxy characters, which use a different prefab and setup.

    Maybe it's my work blindness on the topic but .. I think it's easier to deeply integrate multiplayer. You could even run a Host and Client for "singleplayer" games without a real downside.