I´ve add your example code and it works but with only 1 role showing

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Linksy_CZ

So your answered my question (thanks), but it won´t code.
I wan´t to add roles with one label that will be randomized like in among us. Same amount roles. (Lan Multiplayer)

extends Node2D

var strList = ["Imposter", "Crewmate", "Crewmate", "Doctor", "Imposter"]
var rng = RandomNumberGenerator.new()


var ix = rng.randi_range(0,strList.size()-5)    
var randomStr  = strList[ix]

func _ready() -> void:
    rng.randomize()
        $Label.text =randomStr

And it still won´t work

The example provided in the other question (by @godot_dev_) gave you the basic mechanism to randomly select a string from an array. In the above code, you’ve elected to use that mechanism in the _ready() function, which by design, is only called once. So, that explains why you only see 1 role being displayed.

You’ll need to decide where/when to use the code to fit your game design. I’d probably create a function that selects and returns a new random string each time it’s called. That way, you can call it whenever you need a new string.

jgodfrey | 2023-06-13 17:31

Thanks but what machanic should i use?

Linksy_CZ | 2023-06-13 18:19

Oh i wan´t to show only one role at once. But when i refresh the game window it is the same(I try it like 20 times).

Linksy_CZ | 2023-06-13 18:48

:bust_in_silhouette: Reply From: PAndras

You also have to use rng.randomize before you use rng.randi_range. That is the function that “shuffles the deck”.
randomStr in the code above is a variable which got a value only once.
I also don’t understand why’s there -5 next to strList.size().
You also have to write a bit more complex algorithm if you want to specify how many of a given role are there.

Thanks is this code better? (It is showing errors.).

extends Node2D

var strList = ["Imposter", "Crewmate", "Crewmate", "Doctor", "Imposter"]
var rng = RandomNumberGenerator.new()


rng.randomize()
var ix = rng.randi_range(0,strList.size()-1)    
var randomStr = strList[ix]

func _ready() -> void:
    rng.randomize()
    $Label.text = randomStr

Linksy_CZ | 2023-06-13 18:41

you can’t just have a function call rng.randomize() outside a function. That is likely causing the error

godot_dev_ | 2023-06-13 22:30

The code below should help guide you. When the node enters the scene, a random label should be displayed. By putting the logic in a function called displayRandomString, you can now change the displayed label by calling that function. This way, if a player presses a button for example, you just call displayRandomString()

extends Node2D

var strList = ["Imposter", "Crewmate", "Crewmate", "Doctor", "Imposter"]
var rng = RandomNumberGenerator.new()
func _ready() -> void:
    rng.randomize()
    displayRandomString()

func displayRandomString():
    var ix = rng.randi_range(0,strList.size()-1)    
    var randomStr = strList[ix]
    $Label.text = randomStr

godot_dev_ | 2023-06-13 22:40