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