Array data disappearing after loop scope?

Godot Version

4.4.1

Question

I have this array of strings declared at the top of my script:

var log_strings:Array[String] = []

In one of this script’s functions, I load a file line-by-line into this array:

func read_file(filepath:String) -> void:
	log_file = FileAccess.open(filepath, FileAccess.READ)
	log_strings.clear()
	
	while not log_file.eof_reached():
		log_strings.append(log_file.get_line())
		
	log_file.close()

If I put a breakpoint in that loop and step through it, it is correctly reading lines from the file, and I see that log_strings is correctly expanding as it takes in lines.

However, if I put a breakpoint outside the loop and let it run to completion, the array is now empty. I’m very confused as to what could be causing this. Any advice would be appreciated!

Can you recreate this behavior by this code on its own and, for example, calling the function once?

1 Like

The problem is going to be in code you aren’t showing as everything you show and describe here should work.

Yeah – in fact, this is in an application that I have barely just started, so the grand total code for this script is quite slim already. I’ll copy the entire thing. The “on_open_file_selected” function is attached via signals to the FileDialog’s file_selected() signal.

I’ve put some print statements around to try and figure out what’s going on. In this example, the “read num lines” at the end of the read_file function shows the appropriate # of lines (~13k for the file I’m selecting).

extends Control

var preload_file_dialogue = preload("res://UI/SCN_File_Dialog.tscn")
var instance_file_dialogue:FileDialog
var log_pathname:String
var log_file:FileAccess
var log_strings:Array[String] = []

func on_file_open_pressed() -> void:
	if instance_file_dialogue == null:
		instance_file_dialogue = preload_file_dialogue.instantiate()
		instance_file_dialogue.file_selected.connect(on_file_open_file_selected)
		instance_file_dialogue.confirmed.connect(on_file_open_confirmed)
		add_child(instance_file_dialogue)
	else:
		instance_file_dialogue.visible = true

func on_file_open_file_selected(filepath:String) -> void:
	log_pathname = filepath
	read_file(log_pathname)
	
func read_file(filepath:String) -> void:
	log_file = FileAccess.open(filepath, FileAccess.READ)
	log_strings.clear()
	
	var num_lines:int = 0
	while not log_file.eof_reached():
		log_strings.append(log_file.get_line())
		num_lines += 1
		
	log_file.close()
	print("read " + str(num_lines) + " lines")

I would believe that if there was much else to show :slight_smile: I’ve pasted the entire script in a reply to another comment above.

Try it with a smaller file. Or breach the appending prematurely; like say about 10 lines in. And then check the array. My thought is that this is a memory issue.

while not log_file.eof_reached():
		log_strings.append(log_file.get_line())
		num_lines += 1
		if num_lines > 10:
        		break

My only other thought on this is how sure are you that the (13k lines) array is empty?

Think this is related to the debugger issue:

This shouldn’t be an issue for you however, unless you’re getting errors down the line.

Brawlawnzo, thank you so much! That’s exactly the issue. I was trying to verify that everything was working via the debugger before moving forward, but if I had just tried to use the data it would have worked.

In the debugger, it shows the array as null, but if I do something like

print("array size: " + str(log_strings.size()))

after the loop, it does indeed show the correct # of elements. So everything is working fine, the debugger just can’t look at it, which is fine for my purposes.

Thank you again!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.