0 votes

i have a question tho.. what if i want to make it a random tscn(i already made a bunch) in push of a button, i already put a script :

extends Node2D

var rumahKecil = ["res://TSCN/Rumah1.tscn","res://TSCN/Rumah2.tscn","res://TSCN/Rumah3.tscn"]

func onButtonpressed():
get
tree().change_scene("res://TSCN/Rumah2.tscn")

but im confuse to randomize it. thank you in advance.
i don't know what randomize to use ?

Godot version godot 3.2
in Engine by (62 points)

1 Answer

+1 vote
Best answer

You're probably looking for something like this:

var scene = rumahKecil[randi() % rumahKecil.size()]
get_tree().change_scene(scene)

That should pick one of the elements in your array, and then change to it.

by (10,914 points)
selected by

hi ! it works, but when i pressed it. it alwasy give me the scene 3. do you know why?

oh nvm.. i put randomize() in the top.. it works wonderfull.. thank you so muchh!!

Yep, calling randomize() is the correct solution. I meant to post that, but somehow missed it. And, you really just need to call it once for your entire application - like once in a _ready() function somewhere...

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.