How to access variables from another class in C++

Hello guys. I’m newbie to the Unreal Engine C++ and don’t know much about syntax in the UE4 but i’m still learning. How can i access MinLifespan and MaxLifespan in PooledObjects.cpp from Spawner.cpp. I really don’t understand the syntax.I need someone to show me.
Here is my code :

PooledObjects.cpp

#include "PooledObjects.h"
#include "Classes/Components/StaticMeshComponent.h"
#include "Classes/Components/MeshComponent.h"
#include "ConstructorHelpers.h"


APooledObjects::APooledObjects()
{
    MinLifespan = 0.0f;
    MaxLifespan = 0.0f;
}

Spawner.cpp

#include "Spawner.h"
#include "Classes/Components/BoxComponent.h"
#include "Classes/Engine/World.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "Runtime/Engine/Public/TimerManager.h"
#include "ObjectPooler.h"
#include "PooledObjects.h"

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

	CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
	RootComponent = CollisionBox;

	SpawnDelayRangeMin = 1.0f;
	SpawnDelayRangeMax = 2.0f;
	//MinLifespan = 3.0f;
	//MaxLifespan = 5.0f;

	UWorld* world = GetWorld();
	if (world)
	{
		APooledObjects* LifeRef = Cast<APooledObjects>(world);
		if (LifeRef)
		{
			LifeRef->MinLifespan, MaxLifespan;
			SetLifeSpan(FMath::FRandRange(MinLifespan, MaxLifespan));
		}

	}
	
}

You probably want to use the expression:

SetLifeSpan(FMath::FRandRange( LifeRef->MinLifespan, LifeRef->MaxLifespan ));

But this will only work if you declare Min/MaxLifespan as public in APooledObjects because member variables are private by default. Please note that LifeRef is not a reference but a pointer. The operator -> is used to access a member variable from a pointer to an instance or struct.

Also I would recommend to work through some tutorial teaching the basics of C++.

Thank you j.mueller for replying. I try the steps that you stated earlier. Min/MaxLifespan already declare as public but still it didn’t work. Is there any other solution?

What error are you receiving from the compiler?

Also, is APooledObject a subclass of UWorld?

I didn’t receive any error but the variable Min/MaxLifespan still can’t be accessed from spawner.cpp. I try to change the value of the Min/MaxLifespan variable in editor through spawner but it didn’t work. APooledObject is not subclass of UWorld.

That’s your problem. You’re attempting to cast UWorld into APooledObject. Since it is not a subclass of UWorld it returns null instead.

Does ASpawner have access to the APooledObject via a pointer/reference?

If not you’ll need to search for APooledObject with the code below:

	TArray<APooledObject*> Actors;
	UGameplayStatics::GetAllActorsOfClass(GetWorld(), APooledObject::StaticClass(), Actors);

This will return all the APooledObject in your level. Either use or store the pointer to the APooledObject in your ASpawner class.

I try the code you stated above and i get this error.

	a reference of type "TArray<AActor *, FDefaultAllocator> &" (not const-qualified) cannot be initialized with a value of type "TArray<APooledObjects *, FDefaultAllocator>"

Can you show me the code you wrote that caused this error?

i got the error because of the Actors.

TArray<APooledObjects*> Actors;
	UGameplayStatics::GetAllActorsOfClass(this->GetWorld(), APooledObjects::StaticClass(), Actors);

	for (APooledObjects* Life: Actors)
	{
		APooledObjects* LifeRef = Cast<APooledObjects>(Life);
		if (LifeRef != nullptr)
		{
			SetLifeSpan(FMath::FRandRange(LifeRef->MinLifespan, LifeRef->MaxLifespan));
		}
	}

The one after the APooledObject::StaticClass(),

Try TArray<AActor *> Actors; in Line 1 instead. Using APooledObjects would do an implicit upcast of all actors, what would be invalid for actors of a different class and that´s why the compiler refuses to accept this array.