Replicating Actors in Blueprints

Choose your OS:

This page shows how to use the Replicates option on Actors in order to have them appear (or not appear) on the server versus a client machine. In the two examples cited, the Replicating Actors example shows how to set an Actor to replicate and the difference between a replicated and non-replicated Actor. The second example illustrates how you can dynamically spawn and control where an Actor appears by using the Switch Has Authority node (which checks to see if the server or client is running the script).

Replicating Actors

For this example, we are using the Blueprint Third Person Template with Starter Content.

The steps below illustrate how to set an Actor to Replicate, and the difference between a Replicated Actor and a Non-Replicated Actor.

  1. Inside the Game/StarterContent/Blueprints folder, Right-click on the Blueprint_Effect_Fire and choose Duplicate.

    HowTo1.png

  2. Repeat the previous step to create a second duplicated copy of the Blueprint.

  3. Re-name one of the copies to Fire_NoReplicate and the other to Fire_Replicate.

    HowTo2.png

  4. Open the Fire_NoReplicate Blueprint and click the classDefaults_button.png button, then in the Details Panel, un-check Net Load on Client.

    HowTo3.png

    With this option un-checked, the Actor will not be automatically loaded on the client when the map is loaded. Since Replicates is also not checked, the Actor will not appear on the client's machine at all even if it were spawned in.

  5. Compile and Save, then close the Blueprint and open the Fire_Replicate Blueprint.

  6. Click the classDefaults_button.png button to open the Blueprint Defaults in the Details Panel, then check the Replicates checkbox.

    HowTo4.png

    By checking Replicates, we are saying that this Actor can be replicated on client machines. With Net Load on Client also checked, the Actor is automatically loaded when the map loads and is displayed on client's machines. Where Replicates comes into play is, say you wanted to spawn in this Actor during gameplay at some point instead of on map load, you would need Replicates enabled to say that when the Actor is spawned, go ahead and spawn a replicated copy on client machines.

  7. Compile and Save then close the Blueprint.

  8. From the Content Browser, drag-and-drop the Fire_NoReplicate Blueprint in your level.

    HowTo5.png

  9. From the Content Browser, drag-and-drop the Fire_Replicate Blueprint in your level.

    HowTo6.png

  10. Click the down-arrow next to the Play Button and change the Number of Players option to 2.

    HowTo8.png

  11. Click the Play Button to play in the editor.

    HowTo9.png

    When you play in the editor and a new client window is opened, you should notice that the Fire_NoReplicate Blueprint is displayed on the server and not shown on the client. The Fire_Replicate Blueprint however is shown on both the client and the server as depicted above.

Dynamically Spawning Replicated Actors

For this example, we are continuing from the Replicating Actors example above.

The steps below showcase how to dynamically spawn Actors during gameplay using Replication on the server/client.

  1. In the level viewport, delete the Fire_Replicate and Fire_NoReplicate Actors from the level.

  2. From the Modes menu under All Classess, drag two Target Points into your level.

    Switch1.png

  3. Rename one Target Point to No_Replicate and the other to Replicate and select both in the World Outliner.

    Switch2.png

  4. Open the Level Blueprint from the Blueprints menu off the Main Toolbar.

    GHT1_1.png

  5. In the Level Blueprint, Right-click in the graph and select the Create References to 2 selected Actors option.

    Switch4.png

  6. Right-click in the graph again and search for and add the Event Begin Play node.

    Switch5.png

    While we are using the Event Begin Play node to spawn them in when the game starts, you could swap this for anything else that you would want to use to spawn your Actors in the level during gameplay. This could be anything from a key press to some other kind of dynamic event that occurs which calls for the Actors to spawn.

  7. Right-click in the graph and search for and add the Spawn Actor from Class node and set the Class to Fire_Replicate.

  8. Repeat the previous step to create another Spawn Actor from Class node and set the class to Fire_NoReplicate.

  9. Connect the nodes as shown below.

    Switch6.png

  10. Drag off the blue Replicate node and search for and add the Get Actor Transform node.

  11. Connect the Get Actor Transform node to the Spawn Transform pin of the Spawn Actor from Class node.

  12. Repeat the previous two steps for the blue No_Replicate node (the results should look similar to below).

    Switch7.png

    Next we will add the Switch Has Authority (or Authority Guard) node to determine who is executing the script, the Authority (Server) or a Remote Machine (Client). Typically this will be set to Authority to ensure that only the Server can execute gameplay critical scripts such as adjusting health values for players or distributing points, experience, etc. as you do not want to allow a Client machine to cheat by executing these types of scripts.

  13. Drag off the Event Begin Play node and add the Switch Has Authority node.

    Switch9.png

    Authority should automatically be the default and connect to the first Spawn Actor from Class node.

  14. Compile then close the Level Blueprint and play in the editor.

    Switch10.png

    You should see that since the Server executed the script, both fire effects are shown on the Server while only the one that is set to replicate is shown on the Client. This is useful when you want to spawn something on the Server or Client and not on both, or more importantly, execute any script following the Switch Has Authority node on either the Server or Client (as previously mentioned).

  15. Return to the Level Blueprint and drag off the Remote pin and connect it as shown below.

    Switch11.png

  16. Compile then close the Level Blueprint and play in the editor.

    Switch12.png

    You should now see that since the Switch Has Authority has been set to Remote, the script following it is only executed on the client machines and not the server. Say for example you had some sort of screen effect that is applied when a player takes damage, using Remote as the Authority Guard, you can spawn that effect only on the remote client that is executing the script.