Download

Unique ID for Replicated Actor?

Hello guys, is there any unique id that can be used to reference actors both on local clients and remote ones too? I tried GetUniqueID() but it returns a different ID for each client.

What I’m trying to do is generate the appearance of a complicated actor randomly based on a seed. The seed must be the SAME on EACH client to order to produce the same result.

I know the engine internally is probably already generating a unique ID for each object since it automatically knows who to update on each client when replicating. These guys briefly touched on the subject How are pointers to UOBJECT replicated under the hood? - Unreal Engine Forums

I’d hate to create a new replicated UPROPERTY when I know the engine is already replicating the same thing.

???

After days of searching… I finally found the answer:


FNetworkGUID netId = GetWorld()->GetNetDriver()->GuidCache->GetOrAssignNetGUID(this);
int32 seed = netId.Value;


This seed isn’t unique to each session so restarting sometimes changes nothing, but maybe the host could replicate a universal starting seed for every client at the start of each game. You could also use FApp::GetSessionId(); which changes every new session but not in the editor.

The Actor-names are also unique for every actor per level as far as I know. I am now not 100% sure how this is handled on clients, though

(AActor::GetFName is the method)

GetUniqueID() is a local ID and cannot be used over the network. You also cannot use the NetGUID of the actor because they are unique for every connection (aka, different for every client).

You have to create your own variable, there is no existing value you can use. I do this frequently. You can ensure the property only replicates when the actor is created by using COND_Initial as the replication condition:



.h
UPROPERTY(Replicated)
int32 SomeID;

.cpp
BeginPlay()
{
    if (HasAuthority())
    {
        SomeID = x;
    }
}

GetLifetimeReplicatedProps()
{
    DOREPLIFETIME_CONDITION(AMyActor, SomeID, COND_Initial);
}