Populate Optionbutton from txt or csv file

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

Hi,

First test

When I try this script all the lines adds as item in the dropdown list 26 times. What am I doing wrong?

extends OptionButton

onready var file = 'res://bilhandtering.txt'
func _ready():
load_file(file)
func load_file(file):
var f = File.new()
f.open(file, File.READ)
while not f.eof_reached():
	var line = f.get_line()
	for item in line:
		add_item(line)
f.close()
return

Second test

When I use csv below the dropdown list gets populated ok except for that there is a space gap of 32 options and then it repeat the first items in the csv file.

extends OptionButton

var LevelFileName="res://bilhandtering.csv"

func _ready():
var riga = []
var csv=File.new()
csv.open("res://bilhandtering.csv",File.READ)
riga=csv.get_csv_line(";")
while not csv.eof_reached():
	for item in riga:
		add_item(item)
	riga=csv.get_csv_line(";")
pass

Please show and examplse of the csv and or txt

as it looks like you’re using the for loop redundantly and it should be like this but hard to say without knowing how your files are structured

while not f.eof_reached():
    var line = f.get_line()
    add_item(line)
f.close()

Wakatta | 2021-01-22 21:29

Thanks Wakatta your solution worked for the txt file, the struckture of the txt file is line by line.
The for item in line: made the repeted input-

Jarle | 2021-01-23 00:36