Random Pirates: UI image display inconsistent – why?

Hi,

in UE 5.3.2, I’m trying to programmatically set the Brush of a UI Image element to an asset from an array, but get weirdly random results:

  1. There exists an array “Sprites” (type Object Reference) of a bunch of Paper Sprite object references.
  2. A Random Integer in Range selects a number to serve as a key for the next …
  3. Get (a copy) node. This item from this array is then …
  4. … handed over to Set Brush Resource Object, so that the targeted Image element displays that sprite – see Blueprint below.

Expected Result

As long as I hard-code the key integer, or limit the Random Int in Range to a single number, everything works as expected: the sprite corresponding to the key gets displayed:


grafik
For the integer key “5” the sprite asset “5UHD” is indeed the monkey – everything fine.
grafik

Actual Result

But as soon as I open up the range of the Random Int in Range to > 1, the Print debug still says the right item from the array is being retrieved, but another, seemingly random from the array, is being displayed:


grafik
Index 12 is indeed the sprite asset “12UHD” – but that is actually this:
grafik
while the skull displayed is “0UHD” instead:
grafik

I’ve bashed my head against this for days now; redid the most basic version in a new Blueprint – but the inconsistency is reproducible. ; )

Do you have any idea why this happens?
Do I choose the wrong way to Set the sprite to the image element?
Or would you know of a way how to do this correctly?

(I do need to randomly pick or calculate which sprite to display during runtime; and I’d like to keep the sprites because they come from a sprite atlas texture. So far I haven’t figured out how I could use Set Brush from Atlas Interface: how to address the Atlas Region input [type Slate Texture Atlas Interface Interface].)

Any help is greatly appreciated! : )

You evaluate it twice:

Each time can generate a different number. Generate once, store result, use result:

image

2 Likes

Your code should work. Are you setting the brush resource somewhere else?
Made a setup that mirrors your code (5.3) and it works

1 Like

Well, that’s definitely helpful, thanks! Right now the horizontal line leads off to where other calculations are done to another image. That’s part is not exec connected to anything, though; the only thing following the Set Brush exec is a Print.

Thanks for the trouble! I do get random graphics displayed, too – just not the ones that Print says come out of the Get node, but randomly different ones.

You retrieve one random and print another, different random. Make sure you observe the actual results of the original random generation. This may help guide you to the original issue which I did not get, actually. That’s the only things that jumps out.

Like Everynone mentions you need to cache the output of the random generator to get the generated output otherwise you will get two different random numbers out of it (think of it as firing two instances of the randomization).

Generate it once => put the resulting number in a variable => use variable to set and evaluate the outcome.

1 Like

OMG! You’re my hero! Thank you so much! That solves it!!!

1 Like

Thank you, too! Thinking about it I now even start to understand it. :see_no_evil:

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