x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.
Is there any way to get this whole Apple thing working?

Product Version: UE 4.9
Tags:
more ▼

asked Dec 03 '15 at 07:39 PM in Blueprint Scripting

avatar image

tendo23
37 6 9 12

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Dec 03 '15 at 08:30 PM

avatar image

jwatte
1.3k 49 42 88

avatar image tendo23 Dec 03 '15 at 10:34 PM

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.

avatar image jwatte Dec 03 '15 at 10:50 PM

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.)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question