Skip to content

Instantly share code, notes, and snippets.

@sinbad
Last active June 9, 2020 11:41
Show Gist options
  • Save sinbad/b5f2c24037e626c2b8c78147ffdf6af7 to your computer and use it in GitHub Desktop.
Save sinbad/b5f2c24037e626c2b8c78147ffdf6af7 to your computer and use it in GitHub Desktop.
UE4 RPCs with both C++ and Blueprint implementations

I want a server RPC function which:

  • Is callable by both C++ and Blueprints
  • Is overrideable in Blueprints as well as C++

The basic C++ declared RPC with BlueprintCallable gets me the C++ / Blueprint visibility, but NOT the BP overrideable aspect, because you're not allowed to use BlueprintNativeEvent on Server functions. I.e.

UFUNCTION(BlueprintCallable, Server, Reliable)
void DoSomethingOnServer();

I also don't want to just create a BP Server event because that's not visible to C++.

So right now I double-dispatch like this:

// .h
...
public:
 	UFUNCTION(BlueprintCallable, Server, Reliable)
 	void DoSomethingOnServer();
protected:
 	UFUNCTION(BlueprintNativeEvent)
	void DoSomethingOnServer_ServerFunc(float Force, FVector CentreOffset);
...
// .cpp
void MyClass::DoSomethingOnServer_Implementation() 
{
 	DoSomethingOnServer_ServerFunc();
}
void MyClass::DoSomethingOnServer_ServerFunc()
{
 	// Either I can implement stuff here, and/or override in BP
 	// (If I only want BP right now I can use BlueprintImplementableEvent and skip this C++ impl)
}

This gets me what I want because I can call DoSomethingOnServer from C++ and BP, and whatever code is in DoSomethingOnServer_ServerFunc, in either C++ or Blueprints, will be executed server side.

Am I insane? Or is there a better way to hit these feature points?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment