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.
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.
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?
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.
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.