I am working on a singleplayer game which I hope to eventually convert to multiplayer. I do have a very basic knowledge of replication and some stuff about how multiplayer works, but not much. It has come to my attention that perhaps I should be starting to think about multiplayer sooner, to avoid making the game difficult to convert in the future when it has more features. However, I am not really sure what I should be doing.
As I understand it, the way replication works is that if the client wants to spawn something that needs to be seen by all players, they need to tell the server to spawn it as well as set the class of the thing they are spawning to replicate. First question, is this correct? Secondly, how does the client communicate with the server to tell it to spawn something? Could someone explain simply what I would have to do to make it so that if any player when they pressed a key could spawn an actor, say, and all other players could see it.
Also, are there any other things that I should know and bear in mind other than the issue I just mentioned? (I understand about sessions and how you would make a main menu setup with hosting and joining so that isn’t an issue, it is more gameplay I am worried about)
And finally, would you recommend that I keep going with singleplayer exactly as I am doing (with no considerations for multiplayer), make the game multiplayer right now before I add too many features, or keep going with singleplayer but work slightly differently based on the answers to the previous questions?
If anyone could help answer some or all of my questions it would be greatly appreciated.
they need to tell the server to spawn it as well
You only need to spawn it on the server and it if the actor is replicated it will also spawn on the clients.
how does the client communicate with the server to tell it to spawn something
When you create a custom even in the properties you can set the RPC mode. If you set this to “server only” the event will only be executed on the server. In the same way you can set it to “owning client only” in case you want the server to call an event on a specific client, or “multicast” if you want the server to call the event on all clients.
Also, are there any other things that I should know and bear in mind
For multiplayer it’s very important where your functions, events and variables are located. You should read up on how Game Instance, Game Mode, Game State, Player State and Player Controllers work for multiplayer. Quickly: Game Instance is something that isn’t replicated and can’t have replicated RPC events. Everyone has their own Game Instance. Game Mode is something that only exists on the server. Game State is something everyone has. Player State is something everyone has for every player. Player Controllers is something all players only have their own of except for the server who has a Player Controllers for every player.
So if right now for your single player you are putting everything in Game Instance or Player Controllers that might not work.
I would suggest that if you want your game to be multiplayer at some point you start with it now. I did for my game after working on single player for a while and I already had to make a bunch of changes that were much easier to do now than later.
Keep in mind that Game Mode is only on the server. So if you have an event on it that you want to call from a client you can’t.
If I spawn a replicated actor from a client, will the other clients not be able to see it?
No, only if that client is also the server.
Player index 0 is always the local player so that should work fine.
Thanks for the answer. Very helpful. Right now I have most my code in the player character, and I have some in the GameMode. Stuff in the game mode blueprint is stuff that I would only want to happen on the server anyway, so I probably won’t need to make too many changes in that sense. Just to clarify - If I spawn a replicated actor from a client, will the other clients not be able to see it?
Oh and one other multiplayer question I need some help with - right now when I want my player character to communicate with my hud I do Get Player Controller (Player Index 0) - Get HUD - Cast to… and then do whatever I want. And then I do Get Player Character (Player Index 0) - Cast to BP_Character to send data from the HUD back to the character. When I make it multiplayer, will I have to change all that code to make it a different player index? Or will leaving it at player index 0 work fine? Thanks for all your help.
Perfect. I think I will start working on making my game multiplayer right away. Thanks for your advice.