Referencing a blueprint as member of another blueprint

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.

You seem to not be aware of fact that blueprint is also a class same as C++ class, which you need to spawn (instantiate) first in order to use it. UClass* (or TSubclassOf which i command you to use if you want to limit class selection in blueprint) is just class identifier and you can use it to spawn actor of that class for example. In C++ you spawn objects useing ()->SpawnActor(/*UClass* goes here*/);, it will return actor pointer of spawned object. Contain it in varable and then use it. You can also do this

()->SpawnActor<AItem>(/*UClass* goes here*/);

Which will automaticly cast return value to your spawned actor class

Go watch my tutorial about basics of objects and classes, it’s made for blueprint users, but the same is applied to blueprints so somehow you will be able to convert it in to C++:

Ah that makes sense, I’ve been taking the wrong approach thanks to what I’ve done in Unity.

I’m at work right now so I’ll watch the video when I’m home later.