We are currently playing around more with Event Dispatchers and right now we have the following situation:
We have the following things:
Primary Data Asset to store: Item Name, Item Icon and Static Mesh
1 Data Asset for a Test Item
BP_ItemBase where we use the data asset to set the item we wanna set for this actor.
An Inventory UI with only 1 Slot
And a functioning code that whenever E is pressed and the Sphere Trace hits the item, the actor gets destroyed and name and icon will be set inside the inventory, just to âsimulateâ the item within the inventory widget.
We wanted to create less dependency and therefore started to use Event Dispatchers.
Now, we do know and it makes perfect sense, that it only works if we pick up the item while the inventory is open/displayed on screen. But what we just canât figure out is how to make all this work regardless of the inventory widget being displayed on screen.
The understanding of Event Dispatchers doesnât seem to be the problem. Been doing loads of researches about this topic and plenty of other, already working, experiments. The goal is to create as less dependency as possible. No idea if it would be possible if the BP_ThirdPersonCharacter would bind from the call but in theory it makes us have to have a reference to the widget within the Character which we donât want. Or is that wrong? Because obviously, if we want the widget to react onto the item picked up, we want the widget to do the things and not a reference somewhere else.
Anyways, apologies for the long explanation and appreciate all the help we can get on this matter!
While separating the inventory component from the widget is a good idea, the component could update the savegameâs blueprint, and then you could try managing the inventory from the Hud class using an interface and a Refresh function, then the widget could look at the current savegame inventory. This would completely separate the playerâs inventory system from the inventory widget, making it work even without the player in the game.
I gotta say in advance itâs literally just a dummy project to try out different functionalities together with Event Dispatchers. Meaning there is no actual savegame and there wonât be. For future bigger projects it surely will help a ton and itâs highly appreciated! Thank you so much.
However I donât know if I misunderstood or if our explanation of the âproblemâ is a bit misleading.
If the inventory widget is open (after pressing the IA for triggering the inventory to add it to the viewport), the item can be picked up and gets destroyed. However if the inventory widget is NOT open/not added to the viewport, nothing happens. The sphere trace does the collision, it detects the item but nothing happens. It doesnât get destroyed nor added into the inventory slot. While it makes perfect sense since the function is inside the âConstruction Eventâ of the widget, meaning only if the widget is constructed, the code will run, our question lies into IF there is any way to prevent that. As in, is there a way to still do the logic after the call has been made, regardless of the widget being constructed or not.
We tried to use the Pre-Construct Event but, unsurprisingly, it didnât do the trick.
If you need the code for our IA to open the inventory itâs literally just a flip flop between âAdd to Viewportâ and âRemove from Parentâ. Nothing spectacular there.
Thereâs an event in widgets called initialized to perform binds before constructors, but if there isnât ââCreate Widgetâ (which is actually a spawn widget), it shouldnât work. Because without âCreate Widget" it doesnât exist in the world. Remember, youâre working with objects, You always need a reference to objects created in the world
Some of the things were already mentioned by @DomusLudus above but I wanted to re-iterateâŚ
This is not the proper use case for events. There is, actually, no need for events here at all. Since you already have a base class you can address it directly. If you are not sure weather all pickable objects will derive from BP_ItemBase use an interface instead.
If you are hell-bent on using events, use them properly. Before destroying an actor, which has a bound event, you have to unbind it.
Separate the responsibilities. The UI should neither pick nor contain the item, the character does. Move all gameplay functions in your actors and components. Treat the UI as a window in their data, nothing more. Your game should be played without the UI as the server/AI will play it without one.
Thank you so much both of you for all your inputs! This surely helped a ton.
I mean Iâve mentioned above already, itâs just a dummy project. Fooling around in order to properly learn all the things possible. Main focus right now lying in Event Dispatchers. Also, thanks for the Unbinding mention. Totally forgot about it since I was kinda caught up in figuring out why it doesnât work.
I mean sure if I would go and build a proper inventory system, of course the character contains the stuff.
But isnât âCreate Widgetâ actually, creating it into the world already? Thatâs actually good to know. Idk how we didnât know that haha. Weâve just started working with UE a couple months ago, so itâs still a bit heavy to get all the stuff. Also just realized since I am always referring to âweâ, basically 2 people 1 with software engineering experience and 1 with no exp at all.
Using these dummy projects especially for my partner to trial through them and try getting into coding and all that (and see what new things I can learn abt using UE)
Sure as hell excited to where it will bring us since itâs a lot of fun working with UE. But itâs a lot to learn and take in
Iâll give you my solution although a lot of things are subject of taste more than âwhat is rightâ. Also Iâm throwing some simple structs around instead of data objects.
The inventory widget is connected to the component through an event to avoid calling UI function from the component. This way you can delete or replace the UI and the game will run all the same.
My main point is that the evens (in that case) are used so the Component has no notion of the UI even though it controls the UI. If I was sure the inventory content wonât change while the widget is opened I could just read all items at the start and forgo any and all events.
Although I donât fully understand how your widgetâs supposed to look like.
I guess you have some sort of box as an inventory, then the slots which receive the item info while being set into the inventory based on how many items you carry (the array)? Meaning all you see whilst opening the inventory is just⌠the (probably) sizebox of slots in the alignment you created the box as?
Oh there is an Inventory Box specifically. Thatâs actually nice. So I wouldnât have to work with a vertical/horizontal box. Constantly learning new things is the joy of development. Does the Inventory Box have any special function compared to using a vertical/horizontal box?
But thank you so much once again and I hope we may see one another in different topic interactions Stay safe. Iâll mark the solution