how randomize TSCN scene in a push of a button

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

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

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

:bust_in_silhouette: Reply From: jgodfrey

You’re probably looking for something like this:

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

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

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

szccornish | 2020-12-16 19:12

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

szccornish | 2020-12-16 19:17

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…

jgodfrey | 2020-12-16 19:47