Networking: Am I doing this right?

I’m just getting into networking, and I’m setting up an inventory system (think: DayZ) to work with multiplayer. I think I’ve got it, but I’m not sure if my method is correct, or wasteful, or whatever, because I don’t have much experience with netcode.

Right now I’m doing the “Pick Up Item” functionality. Here’s a summary of how I did it:

  1. When key is pressed on local client, server is notified that we want to pick up an item.
  2. Server-only function scans for items, gets the actor we want to pick up.
  3. Server passes this actor into a multicast function, and our player gets the same item on all clients + server.

Here’s some pseudocode of my method:


// Player presses button, calls this. Not replicated.
void InputPickUp()
{// We call the server, tell it we want to pick up an item.

// Replicated to server.
void ServerPickUp()
{// Get the actor we want to pick up on the server. AItem is the class for pickups.
AItem* FoundItem = ScanForItems();


// Multicast replicated.
void MulticastPickUp(AItem* NewItem)
// Since this is called everywhere, we will get the same item on all instances of the game.

// Not replicated.
void ActuallyPickUp(AItem* NewItem)
{// Actually do the inventory logic.


SO… is this right way to do things? Am I causing bandwidth waste or anything? Also, is using 4 functions (client input, server, multicast, functionality) normal for networking? I’d love to get your guys’ opinions on this. Thanks in advance! :slight_smile:

I’m still learning to do networking stuff myself, but I might be able to offer some insight that I think might be useful. The code that runs within an instance of your actor on the server will change that actor’s state on the server. You need to then replicate this state out to the clients.

With regards to an inventory, does each player’s inventory contents need to be made known to every other player in the server? If not, then you probably don’t need a multicast function for anything other than removing the Item actor from the world, adding the item to the players inventory should be done so only the server and that client know about it.

Right now, I have the same multicast function both remove the Item and add it to the inventory, so I don’t think it’s reducing network performance. Also, isn’t it safer to keep everything synced up? In case other players had to access your inventory for trading or something.

In regards to the state on the server: all the serverside function does is trace to find an item to pick up. The multicast does everything else.

Also, should I be replicating the inventory array(s) to avoid any possible discrepancy, or is that a waste?

Maybe, but in the case of trading you’d still want the server to handle the movement of items to another player’s inventory. If you have the code to add items to a player’s inventory runnable on the client, people can cheat by giving themselves items.

This is what I was thinking originally, and I think it’s how it should be done. I’m not sure with replication on arrays whether you get an updated element or get an updated version of the whole variable (or if there’s even a difference from the engine’s perspective). But in either case, the server should manage the Actor’s inventory.

Alright. So I guess the client shouldn’t be doing anything other than passing the input on to the server.

I actually have an array for each article of clothing (backpack, pants, etc.) so I hope there won’t be too many replicated arrays :slight_smile: I’ll give it a shot and see how it works.

Hmm, looks like replicating arrays is not as easy as it sounds.

Does anybody have any examples or references besides the docs? There’s not much there to work with. :slight_smile: