0 votes

I am unsure of how to set a timer so then a sprite will appear randomly to the user. Also is it possible to create a limit of time (eg after 5 seconds but before 20)?

in Engine by (60 points)

1 Answer

+2 votes
Best answer

I assume you have a scene, with a script attached, and a sprite as a child node of that scene.

So you can hide it in ready, wait some time and then put it visible again.

func _ready():
    $Sprite.visible = false
    var t = rand_range(5,20)
    $Sprite.visible = true

This code, first runs randomize to initialize a seed for random generation. Then hides the sprite, after that, it generates a rando number between 5 and 20 (for the limits you asked, you can change them). After that, it creates a timer, with the timeout generated randomly befor that, and wait until it times out.
After that, it puts the sprite visible again

by (3,505 points)
selected by

Thank you so much! The only thing that didn't work was the $Sprite.visible = false (vise versa) but that was fixed by just using hide() instead!

Thats strange... i tested with $Sprite.visible = false just now and it worked. IDK why is not working on your side.
Anyways, glad to help!

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.