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:
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.
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.
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.