I’ve been making the switch from Unity to UE4 over the last couple of weeks and I’ve been cruising through learning the engine until hitting this problem.
Basically I have three Actors: Dispenser, Item and Player. The Dispenser has a sphere trigger that when the player collides with should give the player whatever item the dispenser holds. I have multiple Item blueprints that I have been trying to add as members of the Dispenser object so I can pass them to the player on collision without instantiating them as physical objects.
Here are the relative snippets of my code:
Dispenser.h
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Collision=Items)
AItem* item;
UFUNCTION(...)
void TriggerCollision(....)
Dispenser.cpp
void ADispenser::TriggerCollision(....) {
APlayer* player= Cast<APlayer>(
UGameplayStatics::GetPlayerPawn( (), 0 ) );
player->Pickup(item);
}
Player.cpp
void APlayer::Pickup(AItem* item) {
if (item != nullptr) { // ITEM IS ALWAYS NULL POINTER HERE
FString itemName = item->name;
}
}
Item.h
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=ItemProperties)
FString name;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=ItemProperties)
int32 amount;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=ItemProperties)
UTexture2D* icon;
The problem here is that when I declare the dispenser item as AItem* I cannot add it to the Dispenser blueprint (it doesn’t show any item blueprints in the blueprint member ‘item’ dropdown). I thought that I could cast from a super-object of Item so I changed the necessary fields:
Dispenser.h
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Items)
UClass* item;
Dispenser.cpp
void ADispenser::TriggerCollision(....) {
APlayer* player= Cast<APlayer>(
UGameplayStatics::GetPlayerPawn( (), 0 ) );
AItem* pickupItem = Cast<AItem>(item);
player->Pickup(pickupItem );
}
By declaring the item member as UClass* I was able to select item blueprints for that Dispenser member in the editor but after attempting the cast to AItem *, pickupItem remains as nullptr.
I’ve tried declaring the item member as a range of classes that AItem inherits from but I’ve had no luck. I’m starting to think there’s some special way to achieve this, so does anyone know how I can add a blueprint as another blueprint’s member and then access it by reference instead of having to instantiate it?
Thanks in advance.