Forward declaration, casting an array?

Hello! I stumbled upon a circular dependency issue, but I am not so confident on forward declarations and need some help.

In my GameInstance header I have forward declared my actor’s class:

class AWSSalesPoint;

And I need an array of these actors:
TArray<AWSSalesPoint> SalesPoints;

Now how do I cast this array to my actual class in the .cpp file?
I have included the WSSalesPoint.h in the cpp file, and I was using it like this until the dependency issue occured:

void UWSGameInstance::WaveComplete()
{
	EnemiesKilled = 0;
	EnemiesSpawned = 0;
	Difficulty *= DifficultyMultiplier;
	if(SalesPoints.Num() > 0)
		for (int i; i < SalesPoints.Num(); i++)
		{
			SalesPoints[i].InitSale();
		}
	VendorOpen = true;
}

Obviously, currently the gameinstance is trying to use an empty array and doesn’t recognize any functions within the class. How would I cast it to the now included class?

The reason for the circular dependency is because the members of the array are added from the SalesPoints themselves on beginplay, as these are placed in the level by hand.

void AWSSalesPoint::BeginPlay()
{
	Super::BeginPlay();
	GM = Cast<AWinstickGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
	UWSGameInstance* GameInst;
	GameInst = Cast<UWSGameInstance>(UGameplayStatics::GetGameInstance(GetWorld()));
	GameInst->SalesPoints.Add(*this);
	InitSale();
	
}

Here I got away with using the include in the .cpp file as I’m only needing the reference once anyway.

It sounds like your setup is fine: a forward declaration in the GameInstance header and including opposing headers in each of your CPP files.

Your biggest issue is that you really, really shouldn’t use anything derived from UObject (including AActors) by value like that. Your GameInstance should have an array of pointers (TArray<AWSSalesPoint*>) instead and your point would do .Add(this); instead.

Cheers, I figured I should have been using pointers while debugging, and fixed it too. Thanks!

I was still getting circular dependency errors, but I was able to work around it by making my array generic actors, then breaking my array loop into individual instances and casting those.

		for (AActor* Actor : SalesPoints)
		{
			AWSSalesPoint* SalePoint = Cast<AWSSalesPoint>(Actor);
			SalePoint->InitSale();
		}

You could post the actual errors that you’re getting and your headers. It doesn’t sound like you should have any circular dependency issues. Unless there is another header that you’re including that is causing the problem, but if that were the case changing your array’s pointer type wouldn’t fix the issue.

Oh, I explained poorly, I wasn’t getting circular errors any more with how I forwarded, raher I was unsure of how to use the forwared class with an array.

Ah okay. Well you should be able to do the following (I do it plenty):

  1. forward declare AWSSalesPoint in your game instance header
  2. declare an array of AWSSalesPoint* in your game instance class
  3. include the AWSSalesPoint class header in your game instance cpp
  4. profit?

If you’re still getting errors after doing that, you should post the actual text instead of trying to describe them.

One issue I noticed with your initial post that could have contributed is that you forgot to initialize your loop counter i.

1 Like

Oh that works too! For some reason I thought you have to define which class declaration it was going to use by casting it, I didn’t realize it would just recognize the included header.