Updating 2D image of dice result in realtime - Paper 2D, UMG or HUD?

Hey all, I’m in the process of creating a dice based game and want to go from displaying the dice results as an integer on screen to a 2D image of the dice. I’ve tried normal HUD and UMG so far, in UMG I’m not sure how to swap the displayed image in real-time and for each instance of dice. I’ve tried laying multiple UMG images on top of each other and using ZOrder to choose which is on top, this hasn’t worked though.

As for ‘traditional’ HUD I have a working implementation but only for a single die, I can’t get multiple instances of the resulting image to display. I’ve tried using a loop to call the function in which the image is drawn but that just updates the original instance.

It’s just come across my mind that maybe I should be using Paper 2D for this sort of HUD element, but I though I’d ask here first before I spend the time implementing it.

Many Thanks

I would recommend using the UMG, HUD is outdated and Paper2D is a plugin that you probably don’t need in the project.

Changing active texture is actually quite simple: add Image to you UMG widget, and expose it (add name and click the box). Then, inside graph editor, get reference to that UMG element and SetBrushFromTexture to swap the textures (I am talking from memory, names could be a bit different).

I hope this helps

Oh my that worked! I’ve tried many different implementation I’ve found online and this is the only one to work and is by far the simplest. I must have missed something some where haha. Thanks a lot.

Do you mind me asking one more question?

There are 6 dice, each being an instance of class ‘SingleDie’. I want to display the 2D result for each die separately, I can use GetAllActorsOfClass and ForEachLoop but the problem is that each time the dice are rolled the actor names change. For example the first roll of dice are called SingleDie1 through SingleDie6 then the second roll is SingleDie7 through SingleDie12 etc.

How can I assign each UMG image representation to each die.

Thanks again.

You should never refer to name of the instance, as they can be changed. In each class (blueprint or C++), create a variable (like dice index) and use that instead of the name to identify. You can still use foreach, but I would recommend using it only in construct because it is slow if you do it each frame. There, you can filter them by index.

Alternatively (depending on you design), widgets could be spawned by the SingleDie using a reference to the correct one. This is a strategy if behaviour, and positioning, is known ahead of time.

Thanks for the answer, could you elaborate on the first method if you have time? I am using the same method of spawning the dice as the PuzzleGrid template included in UE4.

As for the second method, I am currently using a single UMG Widget to store all aspects of the HUD. Would this method involve have separate widgets for each die? Each widget having just the UMG Image and no other HUD element.

I am still very new to UE4 so I am sorry for the perhaps stupid questions, Thanks.

EDIT: Perhaps just this in sequence after all the dice have been created?

Yes, that could definitely work. Here, you assign unique IDs. In your widgets, you have 6 images and you can do the same loop to figure out which die class goes with which image (again for loop and branch where die index and “widget index” are compared). Just note that every time you roll dices, you need to assign those indices that make them unique. Also, you should destroy previous rolls before you assign as it might happen that all are alive. You can also reuse rolls (the same class, have a function Reroll that creates a new value/changed 3d representation).

Apart from that, just make something that works I guess. There is no right or wrong here, especially for simple games that do not need so many performance optimizations.

Okay thanks a lot for the help. I think this question can be closed. :slight_smile: