Cannot Cast to Widget or Actor (castfailed)

I want to get my ‘Inventory’ which is an array of the ‘inventory structure’ from my ‘CombatUIWidget’ and my ItemInfo which is a single ‘inventory structure’ from my HealthPotion Actor (which is not located in the game world).

The goal is to get these values from a chests blueprint to add the ItemInfo of the HealthPotion to the inventory array as soon as the chest is opened.

I tried to cast to the CombatUIWidget and HealthPotion with references of themselves in the values of the cast but both casts fail. Can somebody please explain my mistake? Thx!

Both casts are failing and if I set up the output string like you described the string text is empty. My references are both reference variables of the type ‘Health Potion’ / ‘CombatUiWidget’ which both exist as own blueprint, both references are set to editable. The variables ‘item Info’ and ‘Inventory’ that I want to get from them are both editable as well.
What could be the mistake/how can I fix this?

Try plugging the “Cast Failed” into a print node and connect “Combat UIWidget Ref” into a “get display name” then the print string. This will tell you what the variable is set to – it could be the wrong type (i.e. not a CombatUIWidget) or a null .

You can do this with the HealthPotion as well.

It’s a good idea to test which of the casts are failing – if either fails then this code won’t work.

It sounds like those variables aren’t set to the actual items in the world – the variables may be null. Could that be the case?

How are you making the Combat UI Widget refer to the correct ? (When are you setting that variable?) How are you setting the health potion variable?

So to make it simple for me.

You want your inventory array which is a Struct Array in a Widget.

Also you want to get a single Inventory Struct, that is in an actor, BUT that actor is not in the world.

You have a Treasure Chest, which when opens, adds to the same single inventory Struct, That is still not in the world?

Do I have you right?

Basically I just want an item added to my Inventory as soon as a chest is opened in the game.

My idea was to create a widget (CombatUIWidget) which also creates and refreshes an array of structures (Inventory).
I also created an actor blueprint (HealthPot) which holds a structure of the specific item information (ItemInfo).
Then when the chest (blueprint actor) is opened I’ll call the Inventory (from the widget) and add the ItemInfo (from the HealthPot).

The HealthPot actor is not existing in the world because the only thing needed is the ItemInfo variable and the item should only exist in the inventory.
The widget which creates the inventory struct is created on ‘event begin play’ (from the UI blueprint that’s set in the game mode of the level).

Right now ‘combatUIwidget_ref’ is default ‘none’ with no other options to choose, the default value of ‘healthPotion_ref’ is not editable even though the variable is set to editable.
Also when I’m creating the CombatUiWidget I promoted the output to a variable which is also from the CombatUiWidget type, is editable and has default ‘none’.
I can’t access it from the chest blueprint though…

I tried removing the casts and just getting the ‘inventory’ and ‘item info’ from the reference variables (created in the chest actor blueprint).
I can drag them out but when ending play testing I get errors of the type ‘Accessed None’ for those variables.

I’m relatively new to UE4 and this all might be the totally wrong way but I can hardly find any information on the topic.
If you need additional information/screenshots to help me please just let me know.
Any help would be appreciated!

Hey Hinato, I see the problem.

Your cast functions aren’t working because they don’t do what you might think they do. Cast takes a variable or reference of one type and turns it into a different type. Just say you have a variable reference to an “Actor” and it can refer to any type of . Cast checks to see if the variable you’re referring to is actually a certain type of .

Your problem is your variables don’t refer to anything – they’re set to none by default and you need to have the blueprint set those variables.

Without seeing your project it’s hard to see the fix but it looks like you’re just approaching the blueprint from a fairly unconventional way (e.g. having a struct inside an that’s not actually instantiated). You might want to do some blueprint tutorials so you can learn how variables can get set using traces or collisions or by finding objects in the game.

Thx! I was searching for additional guides on the topic already and I think I understand what you mean. To communicate between 2 blueprints both (or their parents) need to be actors in the world so that I can specify which of the objects in the world I’m referencing. Correct?

In that case it should probably work if I move the ItemInfo struct to the chest item which is in the world and the inventory array of structs to the controlled character (or to the characters parent to give all characters their own inventory).

My inventory is designed somewhat like the one in the UnrealEngine Guide (- YouTube)
All blueprint communication guides I found talk about blueprints placed in the world so I suppose this is the only way to do it…

answer is

Put your BP in the world, Who cares if it is 500 miles from your game, they are still there to reference.

You can nor reference a BP that does not exist in the world.

Drop all you BP’s in to the world.

I hope it help :slight_smile:

I have managed to get a chest which adds a health potion to the inventory. This health potion grants HP when used. Thx for all the help so far!

In my chest blueprint I created a Cast to the HUD blueprint which is set in the game mode (with get player controller → get hud) and got access to the ‘CombatUI’ widget and therefore the ‘inventory’ array of structs. I created one of the structs named ‘HealthPotion’ as a variable inside of the chest blueprint and filled in the respective values there. Then I add this struct to the ‘inventory’ array as soon as the chest is opened.

To use the item I created an action menu within the ‘CombatUI’ widget. At the moment there is a condition attached which checks if the item is a health potion (basically it checks if the value ‘name’ of the respective struct of the inventory equals ‘HealthPotion’). If yes the player character health is increased by X.

So there are still 2 problems left… 1. I have to create all Items in the respective loot container instead of just creating them once as a blueprint and just giving the container the information of the blueprint. 2. If I want to use an inventory item I have to check which one of all possible items it is (with conditions) and then define its effect within my ‘CombatUI’ widget.

I tried placing one of the ‘HealthPot’ actors (which posess a On Use Event as well as the ItemInfo struct for the ‘inventory’) in the world and creating an ‘HealthPot’ reference variable. It is still not recognized as Input for a cast and I can’t change the default value because of a ‘The default value is not editable’ message. Any additional help would be very appreciated! If you need images of any blueprints just let me know!