Godot. Open a file

Submitted by tech4life on

In our games, it is sometimes essential to have resource files with data.

In this pill we are going to see how to open a file and read the data inside.

In our case the all_words.txt file is in the assets folder and is a list of words separated by commas.

To read it we first have to open the file indicating that we only want to read the data.

file = FileAccess.open("res://assets/all_words.txt", FileAccess.READ)

The next step is to convert the file to text with the get_as_text function

var file_opener = file.get_as_text()
		

Finally, in our case, now that we have all the text in a variable, we have converted it into an array with the split function, which converts a text into an array indicating the character by which we want to separate the elements of the text.

all_words_array=file_opener.split(",")

 

Nivel