AbsolutePickle42
AbsolutePickle42

Reputation: 61

Godot: background loading not functioning

I am trying to get the background of one of my scenes to randomly change whenever I press a button. I have a list of three different images, but I cannot get it to select something randomly from the list. Here is my code:

extends Node2D

onready var scene1 = preload("res://apocalypse1.jpg")
onready var scene2 = preload("res://apocalypse2.jpg")
onready var scene3 = preload("res://apocalypse3.jpg")

var possible_backgrounds = [scene1, scene2, scene3]
var background = 0

var choose_new_background = false

var M_A_P = preload("res://MAP.tscn").instance()

# Called when the node enters the scene tree for the first time.
func _ready():
    pass # Replace with function body.


func _process(delta):
    if choose_new_background:
        background = possible_backgrounds[randi() % possible_backgrounds.size()]
        $Background.texture = background
        choose_new_background = false

func _on_MAPOpen_pressed():
    remove_child($MAPOpen)
    add_child(M_A_P)

I am new to the whole game development scene, so I may just be overlooking something. Any help would be appreciated.

Upvotes: 0

Views: 252

Answers (1)

Theraot
Theraot

Reputation: 40315

First off: Randomize.

You need to seed the random number generator. Which you would do by calling randomize. You don't need, and in fact should not call it every time, instead call it once at the start. So calling it on _ready should work:

func _ready():
    randomize()

It should seed the random number generator using the time at which it was called. Which results in a different sequence of pseudo-random numbers.


Second: Type technicalities.

You initialize background with 0, which is an int:

var background = 0

And then set it to a Texture:

background = possible_backgrounds[randi() % possible_backgrounds.size()]

It will work since background is variant. However, if mean it to be a Texture, you can declare it as a Texture:

var background:Texture

Finally: Random technicalities.

Picking from an array like this possible_backgrounds[randi() % possible_backgrounds.size()] will work. And the distribution will be good enough for some game related uses... But the distribution is wrong. It is skewed to the start more or less depending on the size of the array.

There is a better way to do it if you care about the distribution:

var random := RandomNumberGenerator.new()

func _ready() -> void:
    random.randomize()

func _process(delta):
    if choose_new_background:
        background = possible_backgrounds[
            random.randi_range(0, possible_backgrounds.size() - 1)
        ]
        # …

For future reference: in Godot 4 there is a randi_range method in global scope, so you don't need to declare a RandomNumberGenerator to use it.


Addendum: in Godot 4 you can use the pick_random method of the Array instead:

func _ready() -> void:
    randomize()

func _process(delta):
    if choose_new_background:
        background = possible_backgrounds.pick_random()
        # …

Upvotes: 0

Related Questions