Download

[C++] Variable gets set to null if replicated to owning client using a function

Hi,

I want to replicate a Variable from the SERVER to the CLIENT. I am using a replicated function for that using owning client. Now the problem is that the variable gets set to null on the owning client.

This is where i create and send the object: http://hastebin.com/noreqefupo.coffee

This is the replicated function in the header: http://hastebin.com/udalikobom.pas

And this is what i do in the replicated function: http://hastebin.com/ovonoyoreb.coffee

Strangely all your files is blank for me, can you reupload them ?

Have you override this function in your uobject ?


virtual bool IsSupportedForNetworking();

And you simply need to return true in this function.

I hope this help you ! :slight_smile:

I can’t see the snippets either. But the object that the variable points to is set to replicated right?

If that is already the case… a likely issue is that if you spawn the replicated actor on the server and immediately try to pass a reference to it to the client via an RPC, the actor hasn’t replicated over to the client yet. That’s why the client receives null: the replicated actor hasn’t been initialized yet on the client.

There is a tidy way around this issue, if the idea is that the client needs a reference to the newly spawned actor and react to that. Instead of an RPC, which has this risk of the passed actor not existing on the client yet, you can declare a ReplicatedUsing variable with a function that fires once the variable (and thus the actor it points to) is initialized on the client. So instead of an RPC, the server sets a replicated variable to the spawned actor (or other replicated UObject). Eventually, the object is replicated to the client, which detects that and runs the OnRep function.



// Your client's reaction to variable being initialized goes here
UFUNCTION()
void OnRep_MyDog();

// The server sets this, it replicates to the client and OnRep_MyDog will execute on the client
UPROPERTY(ReplicatedUsing=OnRep_MyDog)
UObject* MyDog;


Edit: What Jackblue says is important too if your object is not a component or actor!

If the object isn’t created by the Server and replicated down to the client on spawn, then pointers cannot be replicated.