I’m trying to call a class that I created to get its properties. The current class (player) and the class I’m trying to call (WallItem) are both implements the same interface.
Note: WallItem is a c++ actor.
Here is the code:
TArray<AActor*> Result;
GetOverlappingActors(Result, AWallItem::StaticClass());
AWallItem* wallItem = Cast<AWallItem>(Result[0]); // Do I need casting here??????
wallItem->Interact();
What I tried to do first was but I had some errors… Code:
The error you are getting is normal. If I understand, you want to iterate all objects that are overlapping with this (the object you are writing the code in), so the second block of your code is almost correct. You can try this one:
TArray<AActor*> Result;
GetOverlappingActors(Result, AWallItem::StaticClass());
// Result is an array of AActor not AWallItem, that is why you need the cast
// You could use the UInterface architecture
// if you do not want for some reason the cast (this is another topic)
for (AActor* wallItemActor : Result)
{
// You must cast the wallItemActor cause its an AActor. Not AWallItem.
AWallItem* wallItem = Cast<AWallItem>(wallItemActor);
// Check if is nullptr. SAFETY FIRST :-)
if(wallItem != nullptr)
{
// Call your function etc.
wallItem->Interact();
...
}
}