Random Text UMG

Hi guys!
I have a game (Block Forest) where you roll around as a ball. when you hit an enemy, you die. I was wondering (not critical or anything) if there was a way to do random text on my UMG game over widget.
Example:
First time you die it might say, “You were eaten by aliens”
Second time, “You exploded”
etc.
Thanks!

Hello,

Here’s how I set this up (granted, it’s not exactly the same as yours. I create my widget on a key press instead for testing purposes, so when you see the F key press, replace that with your death event.)

First, let’s set up the widget:

I added a simple Text component to my widget blueprint, and set Is Variable to true, as well as set Size To Content to true as you’ll see in the screenshot below. Also, keep in mind that you’ll need to set your canvas panel to Desired by selecting the Fill Screen tab at the top right of the viewport and changing it to Desired. Ensure that you’ve anchored your text how you’d like to do so as well. I anchored mine to the center for testing.

Next, you’ll want to set up the Event Graph of your Widget Blueprint. This is where your logic will take place. Since you checked the Is Variable box on your Text, you should see it in the Variables panel of the Details panel. Drag a reference into the graph and call SetText. Here’s the fun part, create a new variable of type String (or Text) and make it an array. Add as many elements as you’d like to this array. This is where you will put your game over messages.

Once you have done this, drag a reference to the array into your event graph, and pull off a Get node. Off of the integer input, add a Random Integer in Range node and set the Min and Max values to correspond with the number of elements in your array. Finally, connect this to the Set Text node.

The last step is to go into your character blueprint and actually create the widget. I used the FirstPersonCharacter blueprint since this was a test project, but you can use whatever you are using for your player character.

What I did was: On key press, or death event in your case, I created the widget, added it to the viewport, and then removed the widget after a short delay so it doesn’t remain on the screen (you may need to customize this part to fit your needs as well).

This should get you the random results you’re looking for as far as pulling different game over messages goes.

Let me know if you have any questions about the setup.

Have a great day!

1 Like

Wow nice answer, Good job.

Yes awesome I will try it. Thanks!

Worked perfectly first try. Thanks so much.