hello, i'm not sure to be in the right place, so sorry if i'm "lost" hm...
So, i would like to load text for my project in labels, randomly.

For example, i have 1 label in first scene and if my text file has these words (1 by line or separate by a space) :
hello
world
cat
town

i want to load randomly one of these word, in the label :
world (random choice ;D)

So, actually i can do this :

  1. display random words in label named "zemot"
    func _ready() -> void:
    	var label_text_res = "res://data.txt"
    	var file = FileAccess.open(label_text_res, FileAccess.READ)
    	var text = file.get_as_text()
    	$data01.text = text
  1. load a text file in a label
    func _ready():
    	initial_position = position
    	var vrai = ["maison","chaton","voiture","cool"];
    	randomize()
    	$zemot.text = vrai[randi()%vrai.size()]

But i don't know how to mix this two techniques. Any ideas?
I continue to search ; thank you for your help...

vegetalain

    Split the text you receive from get_as_text into individual lines (stored in an array). String even has a split function. That one should do the job.

    Or you can load the file line by line (get_pascal_string) and store these into an array.

    vegetalain You don't need to call randomize(), the engine does that automatically. Also use my_array.pick_random() for picking a random element from an Array as it is less error prone.

    Other than that, yeah, as the others already said: put all your words into an Array, either by splitting the file content or reading it line by line.

      edit: this post is wrong, ignore or laugh and then ignore.
      a momentary cranial malfunction erased my memories of how randi() with a modulator works.

      Toxe Also use my_array.pick_random() for picking a random element

      if you're allergic to that, at least use array[ randi() % array.size() - 1 ]
      using the size of the array as an index would exceed its bounds because computers are stupid and want to count from 0.

      IMO making the file csv/excel and load with csv plugin and storing it in an array is probably easier.

      Cool thank you for all your answers, it works ! I will work with these advice for my project 😉

      Have a nice weekend

      `
      func _ready() -> void:
      initial_position = position

      var file = FileAccess.open("res://donnees/motsvrais.txt", FileAccess.READ)
      var text = file.get_as_text()
      var some_array = str(text).split(",", true)
      
      var chrg0 = some_array[0]
      var chrg1 = some_array[1]
      var chrg2 = some_array[2]
      var mvrai = [chrg0,chrg1,chrg2];
      
      randomize()
      $zemot.text = mvrai[randi()%mvrai.size()]

      `