What can I do here other than casting?

Hi all,

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:

TArray<AActor*> Result;
	GetOverlappingActors(Result, AWallItem::StaticClass());
	for (AWallItem* wallItem : Result)
    {
	wallItem->Interact();
    ...
    }

and the error that I got is:
image

If you have any idea on what to do other than casting, please help.

Thank you very much.

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();
            ...
        }
    }

Hope the comments help…

Thank you!

It is just in BP you can get it without casting… I was trying to do this basically.

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