김진우
김진우

Reputation: 161

UE4 Compile error with UFUNCTION(NetMulticast). error LNK2005: function already defined in *.cpp.obj

MyActor.h

UCLASS()
class FPS_API AMyActor: public AActor
{
    GENERATED_BODY()
...
public:
    UFUNCTION(NetMulticast, Reliable)
    void MulticastRPCMyFunction();
...
}

MyActor.cpp

void AMyActor::MulticastRPCMyFunction()
{
    UE_LOG(LogTemp, Log, TEXT("Message"));
}

When i compile my project, i can check the error message below. Compile error

*.gen.cpp.obj : error LNK2005: "public: void __cdecl *::MulticastRPCMyFunction(void)" (?MulticastRPCMyFunction@*@@QEAAXXZ) already defined in *.cpp.obj

Upvotes: 0

Views: 1693

Answers (1)

BenjaFriend
BenjaFriend

Reputation: 664

With networked functions (in your case the NetMulticast metadata) you do not name the function the same thing in the Cpp file as the header file because it gets generated by UHT (hence the linker error about it already being defined).

In your case your Cpp file would need to look like this:

void AMyActor::MulticastRPCMyFunction_Implementation()
{
    UE_LOG(LogTemp, Log, TEXT("Message"));
}

Notice the _Implementation addition to the function name.

If you ever add the WithValidation metadata, then you would need another function with _Validate added to the end of the function name.

Upvotes: 1

Related Questions