• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Unreal engine 4 interclass communication with delegates proble

April 11, 2020 by

Questions › Unreal engine 4 interclass communication with delegates proble
0
Vote Up
Vote Down
Garmaine asked 3 years ago

I am relatively new when it comes to unreal engine and c++.

I am trying to implement communication between multiple different pawn classes, that all inherit from the same custom pawn class. The goal is to be able to broadcast from one of the childclasses, to other childclasses of the parent and handle the delegate there (namely, add a reference of itself to an array owned by the caller).

My first idea was to simply declare the Delegate in the parent class so all the children can access the delegate easily.

Parent CGFCharacter.h

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "CGFCharacter.generated.h"

// Polling for location event
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FPollLocationsSignature, AActor*, OwningActor);

UCLASS()
class PROJECT_API ACGFCharacter : public ACharacter
{
    GENERATED_BODY()

public:
    // Sets default values for this pawn's properties
    ACGFCharacter();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    UFUNCTION()
    void HandlePollLocations(AActor* OwningActor);

public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    FPollLocationsSignature PollLocations;
};

CGFCharacter.cpp

ACGFCharacter::ACGFCharacter()
{
     // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = false;

    GetMesh()->SetupAttachment(GetArrowComponent());

    // Listen to location polling events
    PollLocations.AddDynamic(this, &ACGFCharacter::HandlePollLocations);

}

void ACGFCharacter::HandlePollLocations(AActor* OwningActor)
{
    ACGFPlayerCharacter* OwningCGFPlayerCharacter = Cast<ACGFPlayerCharacter>(OwningActor);
    if (OwningCGFPlayerCharacter)
    {
        OwningCGFPlayerCharacter->AddTarget(this);
        //UE_LOG(LogTemp, Log, TEXT("Tried adding %d"),this);
        //UE_LOG(LogTemp, Log, TEXT("Owningactor pointer %d"),OwningCGFPlayerCharacter);
    }
}

I broadcast it in child A like PollLocations.Broadcast(this);

Child B should be able to handle the event now since it also inherits from ACGFCharacter.

I got this to work only for the broadcaster (ClassA), but not for the class B.

My guess was that it was due to FPollLocationsSignature PollLocations being unique to each instance, so i tried changing it to static like

FPollLocationsSignature PollLocations; -> static FPollLocationsSignature PollLocations;
PollLocations.Broadcast(this); -> ACGFCharacter::PollLocations.Broadcast(this);
PollLocations.AddDynamic(this, &ACGFCharacter::HandlePollLocations); -> ACGFCharacter::PollLocations.AddDynamic(this, &ACGFCharacter::HandlePollLocations);

however, this gives me the following error:

Error    LNK2001    unresolved external symbol "public: static class FPollLocationsSignature ACGFCharacter::PollLocations" (?PollLocations@ACGFCharacter@@2VFPollLocationsSignature@@A)    CardGameFighter    E:\Users\User\Documents\Unreal Projects\Project\Intermediate\ProjectFiles\CGFCharacter.cpp.obj    1  

Error    LNK2001    unresolved external symbol "public: static class FPollLocationsSignature ACGFCharacter::PollLocations" (?PollLocations@ACGFCharacter@@2VFPollLocationsSignature@@A)    CardGameFighter    E:\Users\User\Documents\Unreal Projects\Project\Intermediate\ProjectFiles\CGFPlayerCharacter.cpp.obj    1  

Is there a way to get it working this way? What am i doing wrong? Is there a better way to achive this?

I have also seen people suggesting to use a global singleton in order to achive this, but i have no idea how to implement it, could someone give me a brief example?

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: c++, delegates, inheritance, static, unreal-engine4

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023