In my inventory system i have a blueprint class “ItemBehavior” that has parent class of an object(not actor like usual). This blueprint holds every information about the item: how many you have these in your inventory, their attributes, how they behave when you use them etc. But the problem is that i can’t get reference to the player character using “GetPlayerCharacter” node in blueprints that inherit from object class. I don’t want to store reference to player in a variable in every item, I just want want to get the needed data and that’s it.
Maybe I should use actor instead of object to do this ? Any thoughts ?
I guess you could have a static function anywhere in your c++ code that returns the first PlayerController for you.
For example, in your header file you could have the following:
//MyClass.h
UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Player Character")
static ACharacter* GetPlayerCharacter();
//MyClass.cpp
ACharacter* AMyClass::GetPlayerCharacter()
{
APlayerController* PC = GEngine->GetWorld()->GetFirstPlayerController();
if (PC == nullptr)
{
return nullptr;
}
return Cast<ACharacter>(PC->GetControlledPawn());
}
This would give you a blueprint node callable anywhere:
1 Like