How to make a very simple one-slot inventory (beginner!)

Hello all! ^^

I’m currently working on a game that requires a very simple inventory.

The player can take off their hat, and store one item in it, and drop that item when they choose by shaking their hat. The item should fall with physics so that it can be used for puzzles!

The inventory needs to only be one slot big, and essentially needs to follow this logic:
//

If overlap with item and press (pick up button) play pick up animation and destroy item.

If press (drop button) and item is currently held, play shaking hat animation and create item.
//

It sounds pretty simple, but I’m still a beginner with code and am not sure how to translate this into code! Any pointers would be much appreciated! ^^

I followed a few YouTube tutorials, but ironically they were too complicated for the system that I need. They all included multiple slots and stackable items, but all I need is one slot and the ability to be able to pick up and drop the item!

Incase this helps, the game is pixel art 2D with the character and everything they interact with being in this style, but the world is 3D! I already have the hat animations!

Thanks for reading! ^^

2 Likes

You can make a blueprint which contains a mesh and a collision volume.

In the code of this BP, you make sure that it only accepts input when the player is overlapping the box.

When the player is overlapping, and presses ( interact ), you save in a variable ( in the player ) what the mesh is, and destroy the BP. Play the pickup animation.

( If the player tries to pick something else up, it’s not ok, because the mesh variable is set. )

When the player wants to drop the item, spawn a new BP, set the mesh, and enable physics. Clear the mesh variable. Play the drop animation.

That’s it :slight_smile:

1 Like

I tested it, this is the object

and in the player

Basic, but should do it…

Thank you so much!

I will test it tomorrow and let you know if I can get it to work, but it looks great! Thank you! ^^

1 Like

@ClockworkOcean

Sorry for the delay! ^^

I used the code you provided (thanks!!), and the pick up works great! But I ran into a bit of an issue with the spawn mechanic:

Pick Up Code:

Drop Code:

Where your SpawnActor BP had ‘mesh’, mine has ‘owner’ and the mesh cannot plug into this part. I’m not sure why, but I’m hoping its just a rookie mistake or simple error on my part! ^^ I included all the code just in case its something that’s obvious, but that I can’t spot.

The only thing that may be a little different is the names of my objects. Any references to a Sugar Cube are about the pick up/ drop object - in case its a bit confusing!

Thanks again!

Very good so far :slight_smile:

You need a totally generic pickup BP.

It’s just like this

The mesh is just a static mesh reference, but it’s not set to anything. It’s exposed on spawn

And in the construction script

So to make a sugar cube pickup, you just put the BP in the level and set the mesh to a sugar cube. Or, if you want, you can make the sugar cube a child actor of the main pickup BP ( by right clicking in the content browser ) and set the cube there…

Same thing when you spawn it, because you set that variable to ‘expose on spawn’ it appears in the spawn call, and you can set it from the character.

Tell me if it’s not clear :slight_smile:

Hi there, I managed to fix the static mesh plug (thanks for the help!) and now it can plug in rather than saying object:

Although I’m still running into a few problems:

I have an error/ warning when using to Cast To Player node? I’m not sure why this is and it makes the compiling a warning sign?

I also can’t set this object as the static mesh? It’s a 2D sprite so will I have to change the child class to sprite rather than static mesh?

As well, when I chose a placeholder static mesh (cube) it’s invisible in the menu and the level?

Thanks so much for taking your time to help and explain everything so beginner friendly, I really appreciate it! :smiling_face: :v:

1 Like

First problem, is you need to use ‘get player character’ and cast it to your player.

Do you have a sprite for each object? We can make it work with sprites, or meshes. Looks like you need sprites.

Thanks!
I’m working with sprites for these objects, so every object that came be picked up will be a 2D Sprite! ^^

Ok, so you have a ‘banana’ sprite, and a ‘shovel’ sprite and… like that?

It’s very similar, gimme a minute…

Instead of having a mesh, we have a sprite. So the pickup looks like this

Notice that the sprite variable is exposed on spawn.

and for when you place these in the level, you can use construction to set the sprite

Here’s one in the level, see I set the sprite

The variable in the player, is now ( drum roll ) a sprite

and when you spawn, you do the same thing

Tell me how it goes :slight_smile:

Thank you so much!

I’m away from my computer right now, but I’ll test this asap! ^^

1 Like

Hello again!

I have a quick question about the code!

What is the difference between the variables ‘Sprite’ and ‘Carried Sprite’ ? I’m currently trying to follow the first image, but I’m a little confused about the section ‘Set Carried Sprite’, as I don’t seem to have it in my variables.

Is it a new variable I need to make, or a rename of another variable?

Thanks so much!! ^^

Sprite is the name of the variable in the pickup BP. Carried sprite, is the name of the variable in the player.

Before, when it was a mesh, we had the mesh variable in the BP and the player. Now we have a sprite variable in both. That’s all :slight_smile:

Hello!

Everything is working great now! I added my own animation and delay code to it, and this is what I currently have:

Current Progress Video!

I have another question if that’s okay!

I’ve run into some difficulty with getting 2D sprites to obey physics, and I’m wondering what the best way to apply physics to this object would be!

So, when the player drops it from their hat, it falls to the floor.

Thanks so much!^^

FAB! :slight_smile:

I know nothing about 2D physics :slight_smile:

Apparently ‘constrained’ physics is possible

Thanks so much for all your help!! It’s been really helpful!! :heart: ^^

1 Like

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