0 votes

so I'm creating a game that basically displays a customer, and the texture of the customer randomises every time the 'next costumer' button is pressed, i want to make it so that every time the 'next customer' button is pressed it randomises an integer between 1 and 10 and if the number is more than 1 then the photo of the costumer's ID will match the texture of customer and if the number is equal to 1 then it will display a different photo

my issue is every time i press the 'next customer' button no matter how many times it always seems to be the same photo as the customer's texture and never seems to display the incorrect one

my code so far:

var num = null

func _on_NXTButton_pressed():
    dd = ''
    mm = ''
    yy = ''
    _ready()
    variate_texture()
    num = randi() % (1-10)
    #$NXTButton.visible = false
    $Booth2D/PassSprite.visible = true
    $Person1.visible = true


func variate_texture():
    if text_array.size()>1:
        var texture_id: int = randi() % text_array.size()
        var chosen_texture: Texture = text_array [texture_id]
        new_text = chosen_texture
        $Person1.texture = new_text
        if new_text == Elisa and num <= 1:
            $Booth2D/PassSprite/PassPhotoSpr.texture = ElisaP
        elif new_text == Elisa and num > 1:
            $Booth2D/PassSprite/PassPhotoSpr.texture = EricP

could it be to do with the way im randomising the numbers ?

Godot version 3.5.1
in Engine by (19 points)
edited by

Edited to fix code formatting...

1 Answer

0 votes

make sure at the top to add(this resets the randomness seed)

randomize()

for example to get a random number you would do something like this

func get_random_number():
    randomize()
    return randi()%11
by (30 points)

Though, note, you really only need to call randomize() once per app instance (so, for example, in the _ready() function of the main scene). It certainly doesn't hurt to call it multiple times, but it's really not necessary.

perfectly stated jgodfrey ^^^
ziggy please mark correct if it worked out

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.