Reliable and Run on server in C++ code

I know to create an event i need to use this code:

 public:
     UFUNCTION(BlueprintNativeEvent, Category = "")
     void doStuff(int32 stuff);

But how do I make sure that it is reliable and that it is ‘Run on Server’? Do i need to add the server, reliable, and with validation tags in UFunction?

Hello,

Please note that there are 3 basic types of replicated functions in Unreal Engine 4: NetMulticast, Server and Client.

If you want your function to be executed locally on the server and then forwarded automatically to all clients, please use NetMulticast specifier.

Functions defined with Server specifier are the ones that are called by a client and executed on the server afterwards (in other words, they are executed on the server only).

Finally, Client functions are called by the server and are then only executed on the client that owns the Object.
Functions of Server and Client types can only be used on Actors that have a Net Owner. For Actors this means that are owned by a Player Controller (or that they are a Player Controller).

The decision whether to make your function Reliable or Unreliable depends on the importance of your function.
While Reliable calls are the ones that will definitely occur, Unreliable calls may be omitted due to heavy traffic.
Thus, if your function is essential in terms of game logic and should always be called, you should use Reliable specifier. On the other hand, if function represents some less important functionality, for example visuals, you can make it Unreliable.

Please also note that Reliable and Unreliable specifiers are only valid when used in conjunction with Client or Server.
Finally, WithValidation specifier is used when security of Remote Procedure Calls should be provided. Function that has this specifier should provide a _Validate implementation.

Hope this helped!

Have a great day!

3 Likes

I am aware how to use server, client ant netmulticast. But, you can’t user any of those when you use blueprintnativeevent.
My question is regards to Events, not normal functions.

Create 1 function for network and 1 function for BlueprintNativeEvent

Like this:

Foo.h

UFUNCTION(BlueprintNativeEvent, Category = "")
void doStuff(int32 stuff);
virtual void doStuff_Implementation(int32 stuff);

UFUNCTION(Server, Reliable, WithValidation)
void doStuffNetwork(int32 stuff);

Foo.cpp

void AFoo::doStuff_Implementation(int32 stuff)
{
	GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, 
		FString::Printf(TEXT("Stuff %i"), stuff));
}

void AFoo::doStuffNetwork_Implementation(int32 stuff)
{
	doStuff(stuff);
}

bool AFoo::doStuffNetwork_Validate(int32 stuff)
{
	return true;
}
1 Like

I wish we didn’t have to make 2 functions. I did this for other functions, i was hoping that events would be different. Thanks though!