What do I need to add to make the inventory and the chest work

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GrumpyIdiot

Here is what I mean
So I did watch heartbeast tutorials and I love his inventory videos the inventory is simple and works so fine and uses Godot resources
So all I want to add to it a chest and drag and drop items to the chest and vice versa
So I used my little brain and thought I could do it on my own now it works but it doesn’t?

Like I’m having this weird issue
When I drag items from the inventory to the chest, one of the items in the chest disappears everytime I drag and drop an item from the inventory
And the same happens when I drag the items from the chest to the inventory

There is no error just this weird issue

Does anyone know how to fix that and I know I’m doing it wrong cuz I mainly just duplicated the inventory and changed the names so now I have two different inventories
I know I know it is wrong but I was just trying to see what happens and it worked but not enough so yeah all I’m asking, how do I make the dragging and dropping works between the inventories

And I’m not asking you to code it at all but how would you try to do it like just explain the way

No need for details just the big picture. That’s all I want cuz I know how to code barely and I even learned python but I don’t know to put things together like what I need inorder to get something to work right cuz I know in code there are a lot of ways to make something but like which way is the best one
I hope you understand what I’m talking about and I really need just help I have been working on it for two days I really need to finish it

:bust_in_silhouette: Reply From: ibrahim.semab

the issue you are having is related to how the inventories are being updated when items are dragged and dropped between them. Without seeing your code, it’s hard to pinpoint exactly what the issue is, but I can give you some general advice on how to approach this problem.

Firstly, it’s good that you have recognized that duplicating the inventory and changing the names is not a good solution. Instead, you should consider creating an Inventory class that can be used for both the player inventory and the chest inventory. This way, you can reuse the same code for both inventories, and ensure that they are always in sync.

In terms of the dragging and dropping, you should create a DragHandler class that can be attached to both the items in the inventory and the chest. This class would handle the drag and drop logic, and would need to communicate with the Inventory class to update the contents of the inventories when items are moved.

Here’s a rough outline of how you might approach this:

  1. Create an Inventory class that stores the contents of the inventory
    as an array. This class should have methods for adding, removing,
    and moving items within the inventory.

  2. Create an Item node that can be added to the inventory or chest.
    This node should have a DragHandler script attached to it that
    handles the drag and drop logic.

  3. When an item is dragged from the inventory or chest, the DragHandler
    should create a new instance of the Item node to represent the
    dragged item. This instance should be added to the root node of the
    scene, so that it can be dragged freely.

  4. When the dragged item is dropped onto the inventory or chest, the
    DragHandler should check if the drop location is valid (e.g. there
    is enough space in the inventory, or the chest is not already full).
    If the drop location is valid, the DragHandler should update the
    contents of both inventories accordingly.

  5. To prevent items from disappearing when dragged and dropped, you
    should make sure that the item being dragged is removed from its
    original location before it is added to the new location. This will
    ensure that there are no duplicate items in either inventory.

Thank you so much for this wonderful answer, you are amazing!!!

Anyway I can show you the code if you want, the problem is everytime I try to upload a picture on here it gives me an error.

That’s what I was trying to do but I didn’t know how to approach it, anyway I’ll do that!!
Also I do have a code that can drag and drop and swap and the item get deleted after I drag it so the inventory works perfectly it’s just when i play the scene that has the two inventories together this happened

But yeah I’ll try what you mentioned and hopefully it works!! Thank you so much again! I really appreciate your answer!:heart:

GrumpyIdiot | 2023-05-02 18:14

:bust_in_silhouette: Reply From: CharlesGrimes

It sounds like you’re encountering a bug when dragging and dropping items between your inventory and chest. Duplicating the inventory and changing the names may have caused some unexpected behavior in your code.

To fix this issue, you’ll likely need to modify your code so that the items are properly transferred between the inventory and chest. One possible approach would be to create a separate class for the chest, with methods for adding and removing items from it.

When an item is dragged from the inventory to the chest, you’ll need to check if there’s room in the chest to add the item. If there is, you can remove the item from the inventory and add it to the chest. If there isn’t room, you’ll need to either prevent the drag and drop action or remove an existing item from the chest to make room for the new item.

When an item is dragged from the chest to the inventory, you’ll need to check if there’s room in the inventory to add the item. If there is, you can remove the item from the chest and add it to the inventory. If there isn’t room, you’ll need to either prevent the drag and drop action or remove an existing item from the inventory to make room for the new item.

This is just one possible approach, and there are many ways to implement it in code. You’ll likely need to experiment and debug your code to find the best solution for your specific needs.

Good luck with your project!

Yes I’m trying to do that be it is still not working, I know I know nothing and they arrays are pain in the a**
But I’ll try harder and different ways but up till now there is no luck I’m back to zero again

And thank you for replying, I really appreciate your comment!

GrumpyIdiot | 2023-05-03 23:39