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

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):
        for y in range (0, row):
            var rand_value = int(rand_range(0, 2))
            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 (330 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 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.