Mass data storage

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

With the Godot’s built-in functions : is it possible to store a very large quantity of dictionnaries and arrays in a file and then being able to precisely search an entry and then only load this entry into a variable ?

I have thousands of arrays and dictionnaries I would like to store but I don’t want to have to load a whole file into a variable before being able to get the one I want.

Thanks

:bust_in_silhouette: Reply From: snk

EDIT : Okay, I managed to find a working solution. If any improvements to this come into your mind, feel free to share :

Here’s a database called database.csv I created with a spreadsheet software

value1;value2;value3;value4
string11;string12;string13;string14
string21;string22;string23;string24
string31;string32;string33;string34
string41;string42;string43;string44

The 1st example of how to call a value from this database :

func get_database_entry(line, file):
	var stored_line = ""
    file.seek(0)
	for i in range (2, line): 
          stored_line = file.get_line()
          pass
	stored_line = stored_line.split(";")
	return stored_line

func _ready():
	var file = File.new()
	file.open("res://database.csv", file.READ)
    var result = get_database_entry(4, file)
    print("column 2 at line 4 = ", result[1])
    file.close()
    pass

and the console shows : column 2 at line 4 = string32

The second example with a for loop to extract multiples values :

func get_database_entry(line, file):
	var stored_line = ""
    file.seek(0)
	for i in range (line): 
	    stored_line = file.get_line()
        pass
	stored_line = stored_line.split(";")
	return stored_line

func _ready():
	var file = File.new()
	file.open("res://database.csv", file.READ)
        for i in range(4):
           var result = get_database_entry(i+2, file)
           print("column 3 at line ", i+2,  " = ", result[2])
           pass
    file.close()
    pass

And the output is :
column 3 at line 2 = string13
column 3 at line 3 = string23
column 3 at line 4 = string33
column 3 at line 5 = string43