Sites

Menu

Multiserver zoning

1: Set up replication

Before you set up replication, make sure you have changed your Example Project branch from release (which is the default) to multiserver-tutorial-start - see the tutorial’s Introduction for guidance.

Step 1: Make the change

In the Example Project each GDKCharacter contains a UHealthComponent with a variable called CurrentHealth, which keeps track of that character’s health. On your servers, CurrentHealth is reduced whenever a character is shot, but this reduction is not replicated on the clients connected to the game. This is because the CurrentHealth variable is not setup for replication.

To resolve this you need to mark the CurrentHealth property for replication, just as you would in the native Unreal Actor replication workflow. To do this:

  1. In your IDE, open UnrealGDKExampleProject\Game\Source\GDKShooter\Public\Characters\Components\HealthComponent.h.
  2. Navigate to the declaration of the CurrentHealth variable, and add the UProperty specifiers ReplicatedUsing = OnRep_CurrentHealth. The UProperty should now look like this:

    UPROPERTY(VisibleAnywhere, ReplicatedUsing = OnRep_CurrentHealth, Category = "Health")
    float CurrentHealth; 
    

    You have now marked this property for replication using the OnRep_CurrentHealth function that you’ll implement in the next section.

    Next you need to update the GetLifetimeReplicatedProps implementation of the TPSCharacter to specify the replication conditions for the CurrentHealth variable:

  3. In your IDE, open UnrealGDKExampleProject\Game\Source\GDKShooter\Private\Characters\Components\HealthComponent.cpp.

  4. Navigate to the GetLifetimeReplicatedProps function (which is implementd around line 182), and insert the following snippet:

    // Only replicate health to the owning client.
    DOREPLIFETIME(UHealthComponent, CurrentHealth);
    

    Finally, you need to implement the OnRep_CurrentHealth function so that the player health UI gets updated when the CurrentHealth variable is replicated:

  5. In your IDE, open UnrealGDKExampleProject\Game\Source\GDKShooter\Public\Characters\Components\HealthComponent.h.

  6. In the public scope of the class, insert the following snippet:

        UFUNCTION()
        void OnRep_CurrentHealth();
    
  7. In your IDE, open UnrealGDKExampleProject\Game\Source\GDKShooter\Private\Characters\Components\HealthComponent.cpp and insert the following snippet:

    void UHealthComponent::OnRep_CurrentHealth()
    {
        HealthUpdated.Broadcast(CurrentHealth, MaxHealth);
        if (CurrentHealth <= 0.f)
        {
            Death.Broadcast();
        }
    }
    


Notice that the workflow you just used mirrors that of native Unreal.

Step 2: Generate schema and rebuild your project

Because you’ve modified code related to replication, SpatialOS requires you to generate schema. Also, because you have changed code in your project, Unreal Engine requires you to rebuild your project. (Note: SpatialOS does not require you to generate a snapshot, this is because as you did this when you set up the Example Project and you only need to set up a snapshot once per project.)

To generate schema:

  • In the Unreal Editor, on the GDK toolbar, open the Schema drop-down menu and select Schema (Full Scan).
    (See the schema documentation for information on when and how to generate schema).

To rebuild your project:

  1. In Visual Studio, open GDKShooter.sln.
  2. In the Solution Explorer window, right-click on GDKShooter and select Build.



Now let’s test the health replication in a local deployment.

> Next: 2: Test changes locally




——
2019-08-03 Page updated with limited editorial review: added change branch.
2019-08-02 Page updated with limited editorial review: updated project name and GDKCharacter name.
2019-04-30 Page updated with limited editorial review

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums