+7 votes

I'm trying to make a simple map generator for a Roguelite-like. I wanted to start randomly spawning one room, but for some reason with this code Rand.Range returns 2.176614 every time. If I comment out all the references to gameRunning it starts returning numbers between 0 and 3 like it's supposed to. Does anyone know why it does this?

var roomCount = 0
var gameRunning = false


func _ready():
    set_process(true)
    gameRunning = true

func _process(delta):
    if (gameRunning == true):
        print("start if loop")
        var number = rand_range(0,3)
        print(str(number))
        if number <= 1:
            var roomInstance = room1.instance()
            add_child(roomInstance)
            roomInstance.set_owner(self)
            roomInstance.set_pos(Vector2(0, 0))
            roomCount = 1
        elif number <= 2:
            var roomInstance = room2.instance()
            add_child(roomInstance)
            roomInstance.set_owner(self)
            roomInstance.set_pos(Vector2(0, 0))
            roomCount = 2
        elif number <= 3:
            var roomInstance = room3.instance()
            add_child(roomInstance)
            roomInstance.set_owner(self)
            roomInstance.set_pos(Vector2(0, 0))
            roomCount = 3
        print('Roomcount: ' + str(roomCount))
        gameRunning = false
asked Mar 3, 2016 in Engine by Crows (22 points)

2 Answers

+13 votes
Best answer

You should call randomize() function to generate a new seed each time.

answered Mar 4, 2016 by Shin-NiL (130 points)
selected Mar 5, 2016 by Crows

Indeed. Just call randomize() once in a _ready() function, likely in a global script or the script attached to your main scene.

Cool. Thanks. :)

+2 votes

For the float problem, you can use something like int(value) to make a variable only express itself in integers.

answered Mar 3, 2016 by Ace-Dragon (138 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.