Random Buttons

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

How do I randomize a button’s position by clicking on a button?

we can’t see the picture

sarapan_malam | 2022-09-13 15:37

:bust_in_silhouette: Reply From: Pomelo

You should read Random Number Generation.

First you generate a random number between whatever values you want, and then you use that data to change the position of whatever you want.

Example, lets say you use randf() which returns you a random float between 0.0 and 1.0. You use it twice and then you multiply each number by the width and height of your screen. Finnaly you set the position to those numbers.

:bust_in_silhouette: Reply From: sarapan_malam
func _on_Randomize_pressed() -> void:
  randomize()
  var index_list = range($"%ButtonContainer".get_child_count())
  index_list.shuffle()

  for index in index_list.size():
    var button = $"%ButtonContainer".get_child(index_list[index])
    $"%ButtonContainer".move_child(button, index)

random buttons

source code