How to go about creating a pre-made sentence picker?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SammyHammy

Hi all! I’m pretty new to GDScript and I wanted to make a code where two buttons will be presented, clicking one chooses a random sentence already made from one category while the other button does the same with another category. The sentence picked from a selection would then go on to display itself to the user.

Any help is appreciated! Thank you in advance :slight_smile:

:bust_in_silhouette: Reply From: Wakatta

There are better ways to achieve this but this is as simple as it gets

var sentences = ["Hello World", 
                 "What does the fox say?", 
                 "Almost before we knew it, we had left the ground."]

func _on_button_one_pressed():
    randomize()
    $Text_node.text = sentences[randi() % sentences.size()]

Thanks for the help! I’ll be sure to study more about GDScript too:")

SammyHammy | 2021-10-30 00:29

Then the documentation is a great place to reference.

The above code in human readable format

#Create a list named sentences

#When the button_one node is pressed and its signal emitted
    #reseed random number generator
    #set text node text to a random entry in 
    #the sentences list using a random number up to the size of the sentences list

Wakatta | 2021-10-30 02:12