How do I get the computer to pick a random button widget

Hey there, I’ve just been trying to program a mechanic in unreal but I’m having some trouble figuring out how to get it to work properly. I’m trying to make a choice system where you have to try and pick the right answer to something. So when the game starts, the computer will pick one of the answers out of a list of them, and you have to try and click on the right one.

Each time the game ends / starts again, the computer picks a different answer. If you pick the right one, you win the game, but if you pick the wrong one then you lose and the game starts again. I’m basically struggling on how to get this random answer picking mechanic to work - I know you can use stuff in unreal that makes a random number, but I don’t know how to make this work with widget buttons instead. So far I can only really get the computer to pick a random number, instead of picking one of the widget buttons.

Sorry, I’m not too sure if I’ve explained this well ;; but any / all help and advice would be super appreciated, as I’m pretty new to programming and I’m having to make this for a class assignment.

You could add all of your buttons to a vertical (or horizontal) box, then at the top of the details panel, set your vertical box to “Is Variable”.

Go to the event graph and drag your vertical box variable and use the function “Get Child At”

Then for the index, use a randomly selected integer to pick a random button from your vertical box

something like this:

Ahh this is awesome, thank you so much!! :smiley:

I think the only part I’m stuck on now is figuring out how to get the win / lose conditions to work. I was thinking of getting it so you can click on the buttons and if you pick the right one then you win and it plays a video before taking you back to the main menu, but if you lose then it shows you a game over screen - although I’m not too sure how to get this to work ;;

Thank you so much for all the help though and sorry for the trouble.

yea that’s hard to give you a quick answer to that question. You could set that random integer as a variable, call it RandomButton, then when the user picks a button, get the button’s index, set it as a variable PickedButton, then you could set up a custom event that checks is PickedButton=RandomButton and then do whatever

Look up some tutorials on “ue4 branch” and “ue4 custom event”

That should get the ball rolling

Good luck on your game :slight_smile:

Sorry for the delay, but thank you so much for all the help either way, honestly you’ve already helped out a ton - especially with the blueprints image you showed me! :smiley: