0 votes

I have two buttons.

The first button generates a matrix with random numbers and sets the tiles on a tilemap.

The second resets the matrix values to 0 and redraws the tilemap.

However every time I press the first button it draws the same tilemap not a map with a new set of random values.

extends Node2D

var matrix = []

var column = 40
var row = 40

onready var button0 = get_node("Button")
onready var button1 = get_node("Button1")

func _ready():
    button0.connect("pressed", self, "draw")
    button1.connect("pressed", self, "clear")

func draw():
    generate_matrix()

func clear():
    for x in range(0, column):
        for y in range(0, row):
            get_node("TileMap").set_cellv(Vector2(x, y), 0)

func generate_matrix():
    for x in range(0, column):
        matrix.append([])
        for y in range (0, row):
            randomize()
            var rand_value = int(rand_range(0, 2))
            matrix[x].append(rand_value)
            var tile_value = matrix[x][y]
            get_node("TileMap").set_cellv(Vector2(x, y), tile_value)

Am I just using "randomize()" incorrectly?

in Engine by (12 points)

Actually this was happening because I messed up the matrix.

I added matrix = [] to the beginning of the generate_matrix function so it would reset properly.

1 Answer

+1 vote

I'm not sure how it works in godot but in python you put randomize() at start of your program.

by (329 points)
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.