I’m making a hotbar for my survival game at the moment. At the moment, for prototyping purposes, if the player right clicks an item in the inventory window, it takes that item and sends it to the hotbar. It kinda works they way I’ve done it, but theres a bug that happens when I send more than one item over to the hotbar.
I believe this bug is caused by the fact that when I refresh my hotbar widget by removing it from parent, the widget is still there, and it is causing the hotbar buttons to overlap and duplicate.
So, my question is, is there a way to delete a widget from the game memory fully, rather than just remove it from parent?
If not, then how do I cast from one widget to another? I’ve tried before but I don’t know what object it’s supposed to inherit from.
From the description it sounds like you have a hotbar widget, an inventory widget and then a widget for your inventory item(the item that is remaining on screen after refreshing the hobar). If I’m correct in assuming that, then it seems like you want to store the reference to the inventory item inside of your hotbar widget, then remove it from parent when the hotbar is destructed. Any user widget has an Event Destruct method that is called every time the widget is removed from the screen. This will be where you want to clear any children of the hotbar. Since they are seperate widgets, UE4 won’t automatically gc them because it doesn’t know if you still want those refs or not.
Widgets are automatically gc’d when removed from parent so I don’t think you’ll have to do anything special to clear memory as all references to that widget will be invalidated when the widget destructs. I think your problem is coming from the item widget not ever being removed
I’m assuming that your item widgets are nested inside of another widget (hotbar contains a horizontal box that has the item widgets added to them). If thats true, you can also call the ClearChildren method from the widget that is holding your item widgets.
I just ended a PIE session and the editor has been frozen for a bit so I want to get this out to you now so you can look it over a bit, then I’ll go ahead and post some screenshots when I have a chance so you can visualize what I’m talking about.
If you still want to cast to the widget, you’ll need a reference to a UserWidget object. I recommend promoting the output reference to a variable when creating the widget in question. If that widget is referenced inside of another actor, you’ll want to find a way to get that actor ref to your script by calling the GetAllActorsOfClass/GetAllActorsOfTag methods or exposing a reference to the actor in question on spawn and passing it through when the widget is created.
Let me know how this goes, if it doesn’t work, would you mind posting a couple screenshots of your code so I can get an idea of what’s going on? Also if you could provide a video showing the bug that would help alot too!
Here is the screenshot I promised.
Sorry for not responding sooner, I put this on the back burner because it stumped me. I tried both methods you suggested, and unfortunately my problem is still there.
I tested a bunch of things to try and find the problem, and I narrowed it down to the fact that widgets remain in the system memory for about 30 seconds before they are cleared after they are removed from the parent, and as a result, during this 30 seconds timespan, i’ve got 2 hotbars, which is causing my problem. So yeah, if you have any idea how to fix that please let me know.