Interface Issues

Hi,

Short summary problem:

Casting object to interface returns null if object is blueprint that implements the interface

(custom c++ class that implements interface works)

I want to spawn interface and call method from it.

The thing is, it will work perfectly fine if I make c++ class that implements this interface (and attach class or blueprint inherited from this class to SpawnableActors).



Unfortunately it will not work if I make clean blueprint without custom c++ class with proper set “implements interface” option - this is my main problem


Why is that?

This is my function that describes the problem:

void USpawnerComponent::Spawn()
{
	const int32 Index = GetSpawnableIndex();

	AActor* TestActor = GetWorld()->SpawnActor<AActor>(*SpawnableActors[Index], GetOwner()->GetTransform());
	if (TestActor)
	{
		bool bIsImplemented = TestActor->GetClass()->ImplementsInterface(USpawnable::StaticClass());
		if (bIsImplemented)
		{
			//bool bCanSpawn = ISpawnable:::Execute_CanBeSpawned(SpawnableActor); // compile error: Execute_CanBeSpawned is not a member
			ISpawnable* TestSpawnable = Cast<ISpawnable>(TestActor);
			if (TestSpawnable)
			{
				UE_LOG(LogTemp, Warning, TEXT("works")); // It should be printed but it's not
			}
			else
			{
				UE_LOG(LogTemp, Warning, TEXT("nope (?)")); // this is printed for some reason
			}
		}
	}
}

SpawnableActors is:

UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (MustImplement = "Spawnable"))
TArray<TSubclassOf<AActor>> SpawnableActors;

Finally, the interface:

UINTERFACE(MinimalAPI)
class USpawnable : public UInterface
{
	GENERATED_BODY()
};

class UNREALARCHERYSHOOTER_API ISpawnable
{
	GENERATED_BODY()

public:
	virtual bool CanBeSpawned() const { return true; }
};

I checked it twice to show the problem, if you read it one more time, you can see that it says:

This object implements interface (it’s true), but cast to interface returns nullptr - (as if object doesnt implement the interface)

So to sum up: it implements interface but cast returns nullptr

The problem appears only if object is blueprint with interface (c++ class works)

my bad, i think i gave you the wrong method :smiley:

The check that works with blueprints and lets you use the interface is this one:

if(AnActor->GetClass()->ImplementsInterface(UMyInterface::StaticClass()))
{
      IMyInterface::Execute_Function(AnActor);
}

Which is the 1st one you’re doing. so you already got it.

It’s doing the 2nd check that’s ruining it. that one can’t return the interface from blueprint.

Edit: but that’s what you’re doing and it doesn’t work apparently…

I see the function in your interface that you’re trying to retrieve from a blueprint isn’t a UFUNCTION() with either BlueprintImplementable, or BlueprintNativeEvent.

It would be reasonable to assume that the function is not making it blueprint-side.

In order to use interface methods on blueprint they must have a blueprint appropriate setup.

That makes sense.

What I wanted to do is make a method with default c++ implementation and not exposing it to blueprints, so blueprints could be just a marker.


So, I’ve tried to put UFUNCTION to method and the only difference is, execCanBeSpawned appeared (which is not the same as Execute_CanBeSpawned() which I don’t have, but as far as I saw on google has been used by people)



I don’t see any reason why non UFUNCTION methods would make interface to be nullptr casted, as it is.



With UFUNCTION cast still returns nullptr.

What I did exactly, is - create Actor blueprint, Add Spawnable interface in class settings of this blueprint.

I’ve got Execute_CanBeSpawned is not a member of ... on compile time


meta = (MustImplement = “Interface”) filters objects to assign in editor to TSubclassOf only objects that implement given Interface, so it’s not possible to assign there actor that doesnt implement the interface, quite useful

Regarding it not showing, it’s is well known that intellisense, and even visual assist, aren’t great at detecting functions from interfaces. Like, they will only sometimes show the correct functions after compiling.

Any any case… what does this do: meta = (MustImplement = “Spawnable”)?

Never seen if before. Have you tried this without it?

yeah, i figured as much, but could it the issue here? just for science sake you should try it without it.

What i mean is that i’ve never had any issue with interfaces and blueprints before, so i’m just guessing out my ■■■ here trying to figure out what’s different in your implementation, lol.

It doesn’t matter - without it doesn’t work either ¯_(ツ)_/¯