0 votes

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 :)

Godot version latest
in Engine by (12 points)

1 Answer

0 votes

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()]
by (6,934 points)

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

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
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.