Remote Procedure Calls: Multiplayer logic basics

See video playlist.

In the previous tutorial we created an event dispatcher in the widget blueprint, created a binding to a custom event in the GameState blueprint and checked if the client or the server clicked the button. Following that we checked the value of the players card against the opponent and duplicated the code for the client to check the server.

In this tutorial we focus on the relationship between the client and host, we will investigate the structure of the game engines client/host networking model so that we can succesfully implement our game logic for a full-duplex realtime multiplayer game.

Updating index for structs

In our GameState blueprint create two custom events both set to Multicast and Reliable. Pull a reference to the deck index from the variables (Get) and ++ increment integer. Pull another reference from the varibles for the same index and "Set" it to the output of the increment integer.

Adding function in Pawn Interface

Create a new function in the Pawn interface, here I have added references to the structure, widget and index but infact none of these are essential. Simply create a function and give it a name.

Implmenting the pawn inteface function

In the Pawn blueprint we do the same thing we did for our indexes. Create a custom function that is set to run on server and reliable, create another custom function which is set to run Multicast. Finally using the MyGameState variable we need to call a function in the Game State blueprint (which we haven't created yet).

On server, multicast widget updates

Back in the GameState, create a custom event that is set to run on server and reliable. Call the Event Update Widget which is our widget interface function and hook up the widget reference and structure pins. Create a similar custom event, this time set it to Multicast and reliable.

Now go back to the Pawn blueprint complete the function by calling the Send Server Update.

Host updating index

We are going to add a notification for the win/lose logic, the print string node is used only for debugging and testing purposes. At a later point we can implement a player notification system but for now a print string will suffice. The next step is to increment the deck index, as the image shows here the index for both player one and two is updated whether you win or lose a card. This logic will differ in the final game but in the interest of testing the network code we have opted to increment both players index values which will update the users card on client/host and vice versa.

Client updating index

On the remote branch we need to call the interface which updates the corresponding index number. Get a reference to the player pawn and attach it to the interface you have implemented in your Pawn, in this example it is the Update Widget Struct (Message).

Host updating widgets

The custom events we created earlier; send server update and send client update, can now be called from the authority branch in your GameState blueprint. This function calls the widget interface for the corresponding user interface updating the values which bind the image and text values in the UI.

Client updating widgets

Finally to update the hosts user interface from a client the remote branch must send a call from the pawn to run the update widget as the server. A client cannot call the update directly from the GameState blueprint if it is not the authority branch. We still want the host to update their UI with the next card however so we use the interface to run the Update Widget Struct function we implemented on the Pawn, inside the Pawn the function is passed to another function which simply calls the next function to be run on server. The next function calls yet another function which is set to multicast (to all connected clients) which then casts to the GameState blueprint calling the send server update function we created above.

Before we test the program make sure you go into the Game Mode blueprint and override the default Pawn with the Pawn you have created.

At this point you should have a functioning realtime multiplayer game. In this tutorial we have created functions to increment the index values, used an interface to call the increment function from a remote client. We have also created functions to update the client/host user interface and implemented an interface to allow the remote client to call for an update to the hosts UI.

That conclused this part of the tutorial, if you have any questions please post them in the comments section (in development (:).