How to get actor with Interface?

I want to use Interface in Actor on Scene to get value from another actor on scene. But when i use interface I cant get pointer to Actor. I use this on screenshot but this don t work.


I m sory this stupit question but I cant find information all on youtube get cast to player.

1 Like

You should look at the documentation regarding interfaces. You aren’t using the correct syntax for them

This is what the syntax should resemble:

if (MyActor->GetClass()->ImplementsInterface(UBinaryFruit::StaticClass())) {
	IBinaryFruit::Execute_MYINTERFACEFUNCTION(MyActor);
// you can pass in other parameters that are in the interface function definition
}

// where  MyActor is the class that implements the interface.
// MYINTERFACEFUNCTION is a function that is defined inside of the interface and is implemented in the class MyActor

if your interface function has parameters then pass them after the first parameter
The execute_ function takes the first parameter as the object the execute is run on, then the following parameters need to be like the interface function takes in

IMyInterface::Execute_MyAction(actor_with_interface, param1, param2 etc);

Ok I try to explain. I want get pointer to my actor. Now i try this but my engine crash.
image
image


This Actor on scene but in BP version what i need to do?

You can have a blueprint implementable function that is implemented by the actor where in the editor it will return it’s blueprint counterpart.

Sorry can you explain more specifically? I am stupid

UFUNCTION(BlueprintImplementableEvent)
AActor* Get();

Sorry. I can t understand how this helps me. I Execute interface function but get a error. I want do something like this


But APanalRandom not pointer to actor like interaction interface where you get a OutHit.GetActor.
How i can get a Actor with out hitting object? I cant understand

Did you include the header for APanelrandom? (Probably ‘include “panelrandom.h”’)

Also if it’s a cut off cast it should have a syntax like this:

APanelRandom* rand = Cast<APanelRandom>(old_variable);

Yes in panalrandom.cpp i have .h
image

What you meaning about old_variable?

I m try casting like this

You should be transforming a variable with a cast. Its used to “translate” one class into another if possible.

Like this?
image

When i do this i have a crash

You cant cast an uninitialized variable.
Your actor is empty.
You would need to do

Aactor* Panel = NewObject<Aactor>();

Why not just do

APanelRandom* PanelR = NewObject<APanelRandom>();

When i do this i have crash.

I include include “UObject/UObjectGlobals.h”

Didn’t have the syntax in front of me (was on the phone). You need to pass in the outer object for newObject to work

AActor* actor = NewObject<AActor>(GetWorld());
APanelRandom* pr = Cast<APanelRandom>(actor);

Though the cast will always fail as actor does not derive from APanelRandom

Also NewObject needs to be called outside of the constructor (CDO)

1 Like

I do this


How i can call NewObject? I m soo stupid I cant find this in internet

Just create the class directly no need for any AActor in this case.
A working example:

void AMyClass::BeginPlay()
{
	Super::BeginPlay();
	APanelRandom* PanelR = NewObject<APanelRandom>(GetWorld(), TEXT("GRE"));
	GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, FString::SanitizeFloat(PanelR->randomNR));
}

PanelRandom.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PanelRandom.generated.h"

UCLASS()
class YOUR_API APanelRandom : public AActor
{
	GENERATED_BODY()
	
public:	
	APanelRandom();

	UPROPERTY(EditAnywhere,BlueprintReadWrite)
	float randomNR = 0;
};

PanelRandom.cpp

#include "PanelRandom.h"
#include "Math/UnrealMathUtility.h" 


APanelRandom::APanelRandom()
{
	PrimaryActorTick.bCanEverTick = true;
	randomNR = FMath::RandRange(0, 20);
}

There is no need to pass in the static class to NewObject.
You are already stating it’s return through NewObject(…);

You would use static class if you were using the spawn actor function

FActorSpawnParameters sp;	

APanelRandom * rand = GetWorld()->SpawnActor<APanelRandom>(APanelRandom::StaticClass(), sp);

1 Like

Anyway we were going off topic. Here is the main function
Though it used to be enough to call exec_ now it seems the pure c++ needs a cast and calling implementation

#include "InterfaceCaller.h"
#include "Interfaces/IBinaryFruit.h"
#include "PanelRandom.h"
#include "Interfaces/IInteractable.h"


AInterfaceCaller::AInterfaceCaller()
{
	PrimaryActorTick.bCanEverTick = true;
}


void AInterfaceCaller::BeginPlay()
{
	Super::BeginPlay();		
	APanelRandom* PanelR = NewObject<APanelRandom>(GetWorld(), TEXT("PanelR"));
	
	if (PanelR != nullptr) {
		if (PanelR->GetClass()->ImplementsInterface(UBinaryFruit::StaticClass()))
		{		
			// c++
			if (IBinaryFruit* f = Cast<IBinaryFruit>(PanelR)) {
				if (AActor* returnedActor = f->Get_Implementation()) {

					if (IInteractable* inter = Cast<IInteractable>(returnedActor)) {
						bool Interaced = inter->Interact_Implementation();
					}
				}
			}
			
			// blueprint (need overides implemented from within bp)
			AActor* retrieved1 = IBinaryFruit::Execute_Get(PanelR);			
			if (AActor* retrieved = IBinaryFruit::Execute_Get(PanelR))
			{
				if (retrieved->GetClass()->ImplementsInterface(UInteractable::StaticClass()))
				{
					bool Interaced = IInteractable::Execute_Interact(retrieved);
				}
			}
		}
	}
}

Full project
ProjectInterf.zip (37.7 KB)

2 Likes