0 votes

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()])
Godot version Godot v3.4
in Engine by (19 points)

2 Answers

+1 vote
Best answer

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.

by (1,697 points)
selected by

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

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!

0 votes

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 waittime as your 10 secs, turn autostart off and one-shot to false. Connect it , and change quote ontimeout.

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

by (5,132 points)

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...

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 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 webmaster@godotengine.org with your username.