• News

• Industries

• Learning & Support

• Community

• Marketplace

# How would you generate a random number then use this to decide something?

Hey guys,
I’m trying to figure something out, more as a learning process rather than something to actually use in a game, but I’m not really sure how to start with it - I’m a complete beginner with UE4 and Blueprints, so please help me in as basic a way as you can!

What I’m trying to do is have a trigger event (onActorBeginOverlap) generate a random number (an integer I’m guessing) between say 1 and 10, then use that number to decide something else. For a simple example I want the player to walk over a trigger, have it generate a random number, then depending on what that number is print a different message on screen.
So player->overlaps trigger ->number randomly generated->if number=7 then print “Banana Hammocks Rock!” etc…

I’ve tried to look over the content examples but can’t really get my head around why certain things are ordered the way they are in blueprint. Could someone please point me in the right direction to achieve this?

Thanks,

G

Rather that the lengthy text explanation, I just did a Blueprint quickly which works OK. There may be a more efficient way of doing it.

Hehe, good you made it before me!
You can also use a switch to perform different actions:

like so:

http://imageupper.com/s02/1/4/E1398891918649268_1.png

Case of thingy is what you need:

This is made for checking if random is equal to something, but you can do same with greater or less, or mix, just start with highest or lowest number then cut off results.

Or with integer you can make this cascade of compare int’s, collapse it all into macro then you get exec&integer as input and exec corresponding to index as output

PS: dam that switch on int is what i was about to do good to know that its there already.

If you want them to always do a text statement or something, you can also create an array and add each of the things that he can say to the array. Then you set the maximum range on the random number generator to LastIndex of the array, and read the item from the array using the random number. This way you can add new items easily without having to update a constant number.

Cheers,
Michael Noland

Brilliant, thank you for your suggestions guys! Once I get back to my machine with UE4 on I’ll give them a bash.

Very nice! Could this method be used for some type of random loot dropper? if so is there a way to change it to percentages instead of whole numbers.?

I like the random loot idea. I tried the “Switch on Int” method first time round and it worked perfectly for what I’m after, I like the fact that you can achieve things in blueprint multiple ways.
I’m just wondering if anyone could help me take this idea a bit further? I’m now thinking it would be good if I could log a number as being “used” when it is randomly chosen, so when the player runs over the trigger again the random number generator only used the “leftover” numbers.
So if it randomly chose number 2 the first time around, the next time the player triggered the onActorOverlap event it randomly chose from 1,3,4 and 5, and so on until all the numbers had been used.

You could use “Do Once” after each entry. I just tried it then and it seemed to work OK, but maybe someone else has a better method?

an array is possibly a neater way to handle that as it won’t clutter up your blueprint as much if you need to have many different items and makes adding more easy. instead of building and printing the string you would do what you want to do with that item and remove it from the array afterwards.