At first, let me introduce a “problem” I’ve encountered.
I’m creating a multiplayer game, in which every action/mechanic is based on the “state” (Replicated UPROPERTY variable or pointer).
For example:
If I want my Charater to Grab a StaticMesh, I just change the UPrimitiveComponent* GrabTarget
to point at desired StaticMeshComponent.
And then I can execute a Grab-related logic based on this one single pointer.
(This explanation is ofcourse really simplified)
Because of that it’s really easy to create something that works well with OnRep calls and handles multiple edge cases (like late joining to session), at least for me.
However, I’ve found myself copy-pasting a lot of boilerplate code of multiple function declarations/definitions despite that these functions always do the same thing, just have a diffrent names or parameter types.
Example:
// ACTION_NAME - the name of action/mechanic that appears in Blueprints, e.g. "Grab"
// STATE_TYPE - the type of Replicated Property, on which the action/mechanic is based, e.g. "UPrimitiveComponent*"
// STATE_NAME - the name of Replicated Property, on which the action/mechanic is based, e.g. "GrabTarget"
public:
UFUNCTION(BlueprintCallable)
void ACTION_NAME(STATE_TYPE NewValue);
protected:
UFUNCTION()
void ExecuteACTION_NAME(STATE_TYPE NewValue);
UFUNCTION(BlueprintNativeEvent, meta = (DisplayName = "ACTION_NAME"))
void EventACTION_NAME(STATE_TYPE NewValue);
UFUNCTION(BlueprintImplementableEvent, BlueprintCosmetic, meta = (DisplayName = "ACTION_NAME (Cosmetic)"))
void CosmeticACTION_NAME(STATE_TYPE NewValue);
private:
UPROPERTY(ReplicatedUsing = OnRep_STATE_NAME)
STATE_TYPE STATE_NAME;
UFUNCTION()
void OnRep_STATE_NAME();
UFUNCTION(Server, Reliable)
void ServerACTION_NAME(STATE_TYPE NewValue);
void ServerACTION_NAME_Implementation(STATE_TYPE NewValue);
UFUNCTION(NetMulticast, Reliable)
void MulticastACTION_NAME(STATE_TYPE NewValue);
void MulticastACTION_NAME_Implementation(STATE_TYPE NewValue);
So my question is:
Can I somehow wrap this repetitive declaration code to the one single place?
What I’ve already tried:
- Using a C++ macro
As I understand, this is imposible. Unreal’s Reflection system checks every header file before preprocesor swaps directives to the proper code. It compiles corectly but Unreal doesn’t see any UFUNCTION/UPROPERTY within a macro. (Using UFUNCTION within a macro) - Creating a templated Class
To my knowleadge, this is impossible while using UCLASS (Template UClass or an other way ?)
What I haven’t tried yet:
- “Some kind” of code generation script. I belive that Visual Studio might be able to handle it. I’m already using UnrealMacroGenerator exstension which basically swaps a code. The repetitive code would still be visible in multiple files but managing it would be easier.
- Gameplay Ability System - I believe this plugin might does the same compared to what I wanna achieve. Though it will be an obvious overkill.
I will be really thankful for any answear
Please, have in mind that I’m not some kind of C++/Unreal programmer, and there might be an easy solution I just couldn’t figure it out