How do I output a random number of images at once?

Hi,

I currently have a function which selects a random number of items from my data table. The items have a corresponding image. Within this function i have a “make array” which records the results

I want to display images of all the selected items however currently only the first item (out of 3 selections for example) is displayed. I think it involves using a for each loop of some sort but i dont know where to input it

Thanks alot!

That’d be the issue. Consider a local array, something along the lines of:

Essentially, rather than faffing around with randoms, we shuffle the array, grab the first x elements and return them. This way we do not need to worry about repetitions. Unless you desire / allow repetitions, ofc.


Ideally, you’d add a bounds check in case you try to pull more than the DT contains. May be irrelevant.

Hey Everynone,

thanks alot for replying! I might have not explained what I meant properly sorry I am completely new to using UE5. The function to pull the random items itself works but i cant seem to output the multiple results (images) at once. Instead only one appears which is the first item out of the list of randomly selected items

I am only able to embed one image per reply it seems so ive included the one i think needs tweaking?

Image widget

As above, use an array. What I suggested outputs multiple elements chosen at random from a DT.

In your example you’re asking for one only, so that’s what you get. You can’t tweak it, you need to script it fully.

Shuffle, ForLoop, get the first X elements. My example is wrapped in a function for convenience.

ah right ok so ive updated it now to match what you provided if you can have a quick glance? I didnt add the shuffle as i dont mind repetitions.

How do I now structure my widget to output the multiple results in terms of images? Sorry im a complete newbie, i appreciate all your help!

Instead of the Name variable, use the variable type you desire. Potion Image is, most likely, Texture2D.

I didnt add the shuffle as i dont mind repetitions.

There is no randomisation whatsoever now then. If you want randoms with repetitions, this needs to be done differently, get a Random element from the DT and add it to the array.

Ok cool so i think ive done that now if you can please have a look to see if i made any mistakes?

Also a strange thing is happening which im not sure why, if i have the return node connected i will be able to output the image however it is always 3 random selections (based on the print string). If I do not then it randomly selects as expected (e.g. i got 6 in the screenshot below) but no image will appear. I will be posting as a seperate reply as i cant attach more than one image

Return node connected result

Return node not connected example result

image

It would need to be closer to:

+

Do note that array is local to this function:

It only exists (is in scope) for as along as the function is running. The next time the function runs, there will be an empty array to work with, without the need to clear it first. I see you’re using it in your example, just wanted to highlight it.


And you can read more about arrays here:

Hey Everynone!

THANKS SO MUCH I finally got it to work with your help! Ive uploaded an image of the result where im now able to get multiple images at once to display. I added a print string just to check and its out putting those 4 images. I will upload my other widgets just to show u perhaps you can give me your opinion on whether I did it in a efficient way as i feel i am not.

Thanks again for all your patience, your annotation on your previous image explaining the process of how to set out the nodes really helped with my understanding :slight_smile:

This is my widget which i put together to output the image, essentially i have a bunch of “set brush from texture” for each image component. not sure if thats the best way to do it

Image widget

Widget which holds the images

main widget which displays everything

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