Ok so I feel stupid after trying so long with no success, I just don’t get casting. This whole problem came up because I’m trying to make an inventory. So let’s say I’m trying to cast to Apple. Though whenever I want to cast to it, I get this paradox situation. If I use the Cast to Apple node without an object, it needs an object. If I make a reference to it and connect that, it says "Item is already a ‘Apple’, you don’t need to Cast to Apple. Though when I try omitting the casting, and just use the reference I created (new variable > variable type: Apple(Apple reference)), I can drag off its pin and access variables inside it, but if I want to actually use it, like print that variable, I get the “accessed none” error, so the reference is not good.
When creating references like this, the Default Value is either “None” and cannot set to anything, or says “Editing this value is not allowed”.
It applies to almost all Cast to nodes. With the exception of e.g. Cast to FirstPersonCharacter, where I can connect to its object the “Get Player Character” node, this problem is there in other cases, like I explained with the Apple. Cast to needs an object, but other blueprints cannot be referenced in any way.
Is there any way to get this whole Apple thing working?
I think the problem you’re having is not with Casting, so much as with the difference between “reference to Apple” and “instance of Apple.”
A variable in your Blueprint will be a reference to an Apple. References can be NULL (None) meaning they don’t reference any Apple. To actually have them have a value, you have to create an Apple instance, and then set the variable to reference that instance (using Set Variable.)
However, it sounds to me as what you want is really a Blueprint Interface. You want to be able to treat all inventory items the same. Thus, you should define an interface that encompasses what you need from an inventory (“icon picture” “inventory name” and perhaps “use function”) You should then have each Blueprint that represents an inventory item implement that interface. That way, your inventory can just be a collection (such as Array) of Inventory interface references, and all the different things will be treated the same in the inventory. When adding a new kind of thing (like a Banana) you wouldn’t need to update the inventory code at all, just implement the interface to return a picture of a banana, the name “Banana,” and whatever function you need to run when using the banana.
I did kinda the same you described with structures, that’s how I add stuff to my inventory. I have an OnClicked event for the Use button of my inventory, which, if I use interfaces, would fire let’s say the Use (interface message) function, which would fire the Event Use in my Apple blueprint etc. However I cannot do that because to use the Use (interface message), I need to reference the target blueprint, which I cannot do. Though if you can tell me how to target a blueprint that has no instance in the level, I’m interested in it.
Got it!
How this is typically solved is to create “stand-in” instances of objects in the inventory. You’ll create very lightweight blueprint instances that are not the objects themselves, but some kind of representation of the object. These do not need to be Actors – they can be a raw data blueprint. You then need to actually instantiate one of these “lightweight” objects for each thing in inventory.
If the user then activates the object (or wants to take it out,) the lightweight object would create an instance of an actual Actor through its Use function. (You can also make the class of Actor to create on use be a field in the lightweight data blueprint, if you want to automate that.)