About Event Dispatchers with UI

Hello everyone!

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.

This is the BPC_InventorySystem

This is the W_Inventory

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!

Thank you and a wonderful day! Happy coding

1 Like

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.

1 Like

Hey! Thanks for your answer!

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.

1 Like

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

1 Like

Some of the things were already mentioned by @DomusLudus above but I wanted to re-iterate…

  1. 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.

  2. 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.

  3. 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.

1 Like

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) :slight_smile:

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 :rofl:

1 Like

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 Character does everything:

The pickup only provides the data structure through an interface:

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.

The inventory creates slots. The slots only care to set their own values.

1 Like

Wow! thank you so much for all the effort :frowning: Feel abit bad for asking such a stupid question on a dummy project. We really appreciate it!

What exactly are you doing within your Actor Component besides making the AddItem event? And how does the event of add item look like in your example?

1 Like

Oh I forgot to paste that - nothing really. Just calling the event after adding the item.

Also don’t feel bad. Its fun for me :slight_smile:

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.

1 Like

Exactly that is the point. Independencies.

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?

The inventory looks like this:
image
(nothing is shown in the designer as everything is empty)

The slot looks like this:

Ingame (After setting some default name and icon for the BP_PickupItem)

1 Like

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 :slight_smile: Stay safe. I’ll mark the solution

Oh no. It’s just a vertical box I named Inventory list the same way IconImage is just an image. Sorry. It’s the same old thing.

Take care, man!

1 Like

Aw sad okay haha. You too!

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