Run some logic only on client?

Hi!

How can I run a chunk of code only on the client? E.g. in the tick function in an actor I have some code that is completely unnecessary to run on the server but still needs on the client.

Thanks!

#Answer

Wrap your tick code in this:

or to avoid checking every tick, start a timer function that runs with the required frequency only on clients, by using the above code to start the timer only on the client.

if(GEngine->GetNetMode(GetWorld()) == NM_Client)
{
    //code to only run on clients, will not run in single player / standalone
}

YourGame.h must be using Engine.h instead of EngineMinimal.h to see GEngine (Epic please fix this, have to tell all my customers this)

#More Info

Here’s all the macros I use to do these tests, you could stick this in a .h somewhere or in YourGame.h

#define ISDEDICATED (GEngine->GetNetMode(GetWorld()) == NM_DedicatedServer)
#define ISLISTEN (GEngine->GetNetMode(GetWorld()) == NM_ListenServer)
#define ISSTANDALONE (GEngine->GetNetMode(GetWorld()) == NM_Standalone)
#define ISCLIENT (GEngine->GetNetMode(GetWorld()) == NM_Client)

Thanks a lot!

Is there a blueprint equivalent? I’m stuck on something very similar - Unwanted Replication - Multiplayer & Networking - Unreal Engine Forums