Declaring a function with c++ macro

I try to declare and define the replication function for GAS attributes with C++ macro.

Macro:

#define ATTRIBUTE_REPLICATE_FUNCTION(PropertyName) \
virtual void OnRep_##PropertyName##(const FGameplayAttributeData& OldValue) \
{ \
	GAMEPLAYATTRIBUTE_REPNOTIFY(UBaseAttributeSet, ##PropertyName##, OldValue) \
}

It works fine by itself. But when I add ReplicatedUsing specifier, the project does not compile.

Example:

UPROPERTY(BlueprintReadOnly, Category = "Attributes", ReplicatedUsing = OnRep_Health)
FGameplayAttributeData Health;
ATTRIBUTE_ACCESSORS(UBaseAttributeSet, Health);
ATTRIBUTE_REPLICATE_FUNCTION(Health);

compilation error:
Replication notification function 'OnRep_Health' not found

I feel like that’s because UPROPERTY is parsed by the Unreal Header Tool before C++ compiler handles the macro

Is there a way to make it work?

1 Like

I feel like that’s because UPROPERTY is parsed by the Unreal Header Tool before C++ compiler handles the macro

That’s definitively possible. Maybe try to put the ATTRIBUTE_REPLICATE_FUNCTION and ATTRIBUTE_ACCESSORS before the UPROPERTY declaration, but I don’t think it will work…

No, it doesn’t work