Problem with casting
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.
asked Dec 03 '15 at 07:39 PM in Blueprint Scripting
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.
answered Dec 03 '15 at 08:30 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here