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:
There exists an array “Sprites” (type Object Reference) of a bunch of Paper Sprite object references.
A Random Integer in Range selects a number to serve as a key for the next …
… Get (a copy) node. This item from this array is then …
… 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:
For the integer key “5” the sprite asset “5UHD” is indeed the monkey – everything fine.
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:
Index 12 is indeed the sprite asset “12UHD” – but that is actually this:
while the skull displayed is “0UHD” instead:
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].)
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.