Pick random quotes set on a timer

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

Hello, complete newbie here.

I’m making a 2D platformer - fairly standard stuff. Everything I have managed to make so far has been an amalgamation of different tutorials from all over and thus I have no idea if what I’m attempting now is even the right way to go about it.
If you know a better solution, I’m all ears.

But for the time being, I have made a label where I want quotes to be displayed. On the label script I have an array of the quotes I want and I have managed to make the first quote be picked at random and appear in the label in the scene when the game runs.

What I can’t figure out is how to have the quote be replaced by another quote after some time has passed, let’s say 10 seconds for example.
All my attempts with the timer function has either made no quotes appear or straight up made the game crash.

This is what my current (working) label script looks like:

extends Label

var Quote = ["quote1", "quote2", "quote3", "quote4"]

func _ready():
	randomize()
	text = (Quote[randi()%Quote.size()])
:bust_in_silhouette: Reply From: Inces

There is a thing called yield() , but it is always better to use Timers. You must have had problems with understanding basics of Timer. Just set wait_time as your 10 secs, turn autostart off and one-shot to false. Connect it , and change quote on_timeout.

You could also show the code revolving your bad events with Timers :).

Thank you very much for the answer!

You are right, I have know understanding of the Timer (or much of any Godot or scripting, really, I’m just stumbling my way through).
I have deleted and re-written the ‘bad events’ so many times by this point but this is what it looks like currently;

onready var TimerReload = get_node("TimerReloadQuote")

#func _ready():
#	randomize()
#	text = (Quote[randi()%Quote.size()])

func _on_TimerReloadQuote_timeout():
#	TimerReload.set_wait_time(10)
#	TimerReload.start()
	randomize()
	text = (Quote[randi()%Quote.size()])

With the Timeout function it just doesn’t load the quotes anymore…

Veorir | 2021-12-11 18:53

:bust_in_silhouette: Reply From: DaddyMonster

Add a Timer as a child of your Label. With the timer node selected, look at the inspector panel on the right set the time to 10 seconds (or you can send in the time when you start as below), click the “node” tab and double click on timeout(). Then select the Label node with your script. I’ll automatically add func _on_Timer_timeout() onto the script.

So now just this code:

extends Label

var quote = ["quote1", "quote2", "quote3", "quote4"]

func _ready():
    set_text()
    $Timer.start(10)

func set_text():
    randomize()
    text = quote[randi()%quote.size()]

func _on_Timer_timeout():     
    set_text() 
	$Timer.start(10)

Really interesting approach to taking the modulus you’ve got there, I would have made a random number in the range of the size and subtracted 1 but I like the innovative way you tackled it. Note, variable names are snake_case in gdscript though.

Thank you, that worked (when I wrote set_text(Quote))!

Veorir | 2021-12-11 19:03

Oh of course, silly me, Label has its own text setter method of that name. Glad you got it working and good luck with your project!

DaddyMonster | 2021-12-13 18:44