Call Inherited Blueprint function from C++

I read all other questions but none are the same. How to call Blueprint function from C++ (when Blueprint inherited from other C++ class).

  • RangedWeapon inherits of Tool
  • Other Weapon(Blueprint) inherits of RangedWeapon
  • From PlayerController I wanto to get the EquippedTool of Character and call a Method of Blueprint.
  • Currently in the last code, “tool” is always NULL, after cast.

RangedWeapon.h

UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
void Shoot();

RangedWeapon.cpp

 void ARangedWeapon::Shoot_Implementation() {
        UE_LOG(LogTemp, Log, TEXT("Shoot"));
    }

Character.h

/** Tool Equipped */
UPROPERTY(EditAnywhere, Category = equipment, meta = (AllowPrivateAccess = "true"))
TSubclassOf<class ATool> EquippedTool;

PlayerController.cpp

void ASurvivormanPlayerController::Action() {
    	ASurvivormanCharacter* currentChar = Cast<ASurvivormanCharacter>(GetPawn());
    	ARangedWeapon* tool = Cast<ARangedWeapon>(currentChar->EquippedTool);  //ClassDefaultObject, staticclass
    	UE_LOG(LogTemp, Log, TEXT("exists: %s"), (tool != NULL ? TEXT("True") : TEXT("False")) );
    	if (tool)
    	{
    		tool->Shoot();
    	}
    }

TSubclassOf EquippedTool; Is an UClass, not an object heriting ATool. So your cast to ATool won’t ever work.

You should spawn it before.

#.h

UPROPERTY()
ATool* MyActorTool;

UPROPERTY(EditAnywhere, Category = equipment, meta = (AllowPrivateAccess = "true"))
TSubclassOf<class ATool> EquippedTool; // Must be set in a blueprint subclass or with ConstructorHelper

#.cpp

UWorld* const World = GetWorld();
if (World) {
	const FVector Location = /* the location I want to*/;
	const FRotator Rotation = /* the rotation I want to*/;
	MyActorTool = World->SpawnActor<ATool>(EquippedTool, Location, Rotation);
}

Thank you for the awswer, but how I make this work? Because if I not cast, the EquippedTool not have the methods.

I have to get (currentChar->EquippedTool) and spawn the tool before call method then? Will try.