Inventory system help

Since I didn’t get any help troubleshooting the inventory system I was going to use from a tutorial, I decided to try to make my own. I’m at the point where I’m setting up the logic for adding the item to an array and deleting the item from the world.

When I compile, it gives me some errors on a few of the nodes but I can’t understand what’s wrong.

You don’t have an object connected to the cast.

Okay, I made a variable in this blueprint of the type BP_Creature object reference, which is the basic parent class for the player character, NPCs, and monsters/animals. That solved the issue with the Carrying Capacity variable, but the other nodes are still throwing errors.

The way it’s supposed to work is that whatever I hit with the line trace will be an item, so i’ll get it’s BP Class, then pass that BP Class to the array which serves as the inventory.

typically an inventory holds objects not the classes of the objects.
we would use a structure holding classes, if we wanted to know the types of things in the inventory, where we no longer have say a specific car in the inventory, we have the concept of a car.

we can do some stuff with the class like spawn things, but we have no data to back it up.

think on the class as getting a blueprint of the thing, but none of the values actually exist.

Try changing the inventory to be an array of Object-References instead of array of Class-References

also the Cast<T> node requires something plugged into the Object Pin otherwise it WILL throw an error.

I originally had it as an array of object references but that throws an error when I plug in the hit target from the line trace. On mobile at the moment but I’ll post a pic shortly

New error messages:

what is the type of the inventory?
currently you are trying to add an object of type AActor, which if the type is something that inherits from AActor it could fail because it could be “Any Actor” (in terms of the C++/Asembly you could be trying to add a chunk of floor into the inventory)
“it is hardly ever wrong to validate types before doing things with the object”

so now is when you would cast the HitActor into an IsValid() (I would suggest the one that already has the branch pins), and add the result off the cast to the Array.

it is “safe” to go the other way (treating your InveotryItem as an actor), but it generally is not safe to try and use an object upward without casting

The IsValid nodes I can find just return boolean values. I’m not sure how I’d use that to get an AActor reference into the pin on the Add to Array node

something like this

Even if you were doing a “Trace For Objects” feeding in the type of the object it still would return a FHitResult and an FHitResult is defined as a non-template dataType that holds either a single Actor “Hit Actor” or an array of Actors “Hit Actors”

technically if this was a Trace For Objects you can have additional trust in the “Hit Actor(s)” not needing the isValid() Test but it is still “better to check then to have to troubleshoot without debug”

without a Trace for Object(s) the HitActor from a Line trace could be anything, and as far as the engine is concerned it could be an item, a player, the sky, or the floor; it is “just an actor”

I created a custom channel, Items, and set the default to Ignore. Then I changed the setting to… Block, I think, for my BP_Item class. I was counting on having each item type be its own child class of this one and inherit that setting so that only items would get deleted and added to the array

sure, that is a reasonable line of thought, but the Engine doesn’t know that.

when programming (blueprints or structured English code) think of the computer as a 5 year old; it will do everything you tell it to, but it doesn’t know anything unless you tell it to know it.

for a Trace by Channel it knows what channel you are using as the criteria, but it doesn’t outright know (especially at compile time) what could/shall/will be on the channel, it just tests the line against everything on that channel. you could have only really specific things on that channel, or Tim the level designer could have put everything box on that channel because “they are items aren’t they”

so the Editor/Engine doesn’t know your intention, it just knows your instructions, so validate your objects, before access or assigning.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.