Skip to content

Instantly share code, notes, and snippets.

@MarianoGnu
Created December 2, 2018 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarianoGnu/3c3bd5e58bb2cea85354bda97de1cf2a to your computer and use it in GitHub Desktop.
Save MarianoGnu/3c3bd5e58bb2cea85354bda97de1cf2a to your computer and use it in GitHub Desktop.
extends Node
const src_path : String = "res://game_data";
var data : Dictionary = {}
func _init():
print("initialize_game_data")
load_data_from_files(src_path)
func load_data_from_files(_path : String):
var dir : Directory = Directory.new();
assert(dir.open(_path) == OK);
assert(dir.list_dir_begin() == OK);
var file_name : String = dir.get_next();
while (file_name != ""):
if (file_name == "."):
pass;
elif (file_name == ".."):
pass;
elif dir.current_is_dir():
load_data_from_files(_path + "/" + file_name);
else:
if(file_name.get_extension().to_lower() == "json"):
read_file(_path + "/" + file_name);
file_name = dir.get_next();
func read_file(_path : String):
var f : File = File.new();
# Get data name from folder
var data_name : String = _path.get_base_dir().get_file()
if !data.keys().has(data_name):
data[data_name] = {};
assert(f.open(_path, f.READ) == OK)
var content = f.get_as_text();
# All files are arrays of one or many objects
var dict : Array = parse_json(content);
# Stores data on main dictionary
for obj in dict:
data[data_name][obj.id] = obj;
func filter_data (source_dict : Dictionary, filter : Dictionary):
var result : Dictionary = {}
for src_key in source_dict.keys():
var valid : bool = true;
for f_key in filter.keys():
if (!source_dict[src_key].keys().has(f_key)):
valid = false;
break;
elif (source_dict[src_key][f_key] != filter[f_key]):
valid = false;
break;
if (valid):
result[src_key] = source_dict[src_key]
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment