how to use variables from other class in ue5 c++?

I’m trying to access a variable in my partyinfo class from my menu class, but how? Here is my code.

menu

APlayerState* PlayerState = Pawn->GetPlayerState();

APartyInfo* PartyInfo;

if (PlayerState) {
if (PartyInfo) {
FString PlayerName = PlayerState->GetPlayerName();
GEngine->AddOnScreenDebugMessage(-1, 15.0F, FColor::Blue, PlayerName);
}
else {
GEngine->AddOnScreenDebugMessage(-1, 15.0F, FColor::Red, “Party info is nullptr”);
}
}

and party info

class SPELLBOUNDSTUMBLE_API APartyInfo : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor’s properties
APartyInfo();

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

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

virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
UFUNCTION()
void OnRep_PartyID();

UFUNCTION()
void OnRep_bIsLeader();

UFUNCTION()
void OnRep_Members();

// Member variables
UPROPERTY(EditAnywhere, BlueprintReadWrite, ReplicatedUsing = OnRep_PartyID)
int32 PartyID;

UPROPERTY(EditAnywhere, BlueprintReadWrite, ReplicatedUsing = OnRep_bIsLeader)
bool bIsLeader;

UPROPERTY(EditAnywhere, BlueprintReadWrite, ReplicatedUsing = OnRep_Members)
TArray<ACPP_Character*> Members;

};

The print “GEngine->AddOnScreenDebugMessage(-1, 15.0F, FColor::Red, “Party info is nullptr”);” always appears when I run this.

With Class calling or you can use GetWorld function also that is part of the unreal APi to get stuff from other classes. But it’s better with class calling, you can call with class objects, pointer objects or static calls.
Try first with Class Objects, don’t forget to include the header the cpp or H file of the other class where you are calling from in your includes.

It’s best you go online and learn all these 3 forms for class caling by class object, by pointer object and by :: static call, for static your functions have to be static, so the functions you are calling from elsewhere are static and you use a static call, this is popular.

When you declared your PartyInfo variable you did not assigned a value. Since it is a pointer, it will be a nullptr and that condition will always fail.

Your declaration

APartyInfo* PartyInfo;

Should be:

APartyInfo* PartyInfo = NewObject<APartyInfo>(args...);

If your PartyInfo object already exists, you need to get its reference from somewhere.

Yes then go → on the lines below.
He needs to create a handler first like you have shown where the pointer type is the class object.
He can get any class members this way that are in the allow section inside the header.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.