Summary:
After my last project with Unreal Blueprints, I wanted to pivot over to C++. The theme of the project this time around was networking, so I decided to mess around with the FPS template in Unreal and implement some networked multiplayer functionality.
I've only read a little bit of C++, and I've never written anything with it before, and I've also never done any network coding, so I was very much going in blind for this project. Overall, I'm happy that I got to learn a little bit of a lot of different concepts, both in terms of networking basics and C++ syntax. I also got to learn more about ownership, replication, and Unreal's Enhanced Input system.
I used a mix of Server RPCs, Multicasts, and OnReps to manage different objects and states, which I'll show examples of below.
Server RPC (Sending Client Projectiles to the Server):
This is how I spawn the projectile on the server when it's fired by the client. The reason why I call it from the Character class instead of from the weapon class is due to ownership issues, which gave me quite a bit of trouble during this project. Because the weapon object doesn't have ownership, it can't spawn the projectile on the server, so I call it from the Character instead.
Multicast RPC (Syncing Weapon Animations):
The Multicast happens alongside the Server RPC:
I called this from Character as well, just to keep everything somewhat organized and in the same script.
With this all implemented, the gun mechanics sync up nicely on each client:
OnRep Calls (Door Opening/Closing)
First I set up a basic door object, and then added an Enhanced Input Action and put it into the Input Mapping Context, and then use that to check if the door should open/close or not:
Then, I needed to sync it for the clients as well, so I used an OnRep function that worked pretty well. It seemed way more straightforward than the other stuff that I used:
Now the clients can see it too:
Conclusion:
I had a few struggles and grievances throughout the process that made it really frustrating at times. Figuring out the ownership issues was a pain, as I mentioned. Additionally, Live Coding would occasionally break my project, to the point where I would have to start a new one and copy over the old code. I figured out that I just needed to compile from Visual Studio instead, but it took a lot of trial and error before that point. Making sure that everything was Replicating properly, and wrapping my head around header files and uproperties was a big change compared to Unity or even Blueprints.
Although I didn't turn this into a full-fledged “mini-game” or anything, I am still happy with the different implementations that I tried. I feel like I learned a ton about Unreal during this process, in terms of networking, animations, scripting, and more. This was kinda like a crash course into the core of Unreal, which I definitely needed.