What's the best/optimal way to store a database (common between all the nodes)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By alexwbc
:warning: Old Version Published before Godot 3 was released.

The current pattern I do use is through arrays and uses a regular node script to store such values; but I guess that’s not a very optimal way, so what’s the best way to have store a database with npc stats, line of dialogues and such?). This is sort of what I do use as arrays.

#npc[id] = [npc's name, is friendly?, know's his name?]
npc[02] = ["Tom", true, false]

#dialogue = [npc id, player talk, npc reply, switch, open dialog[id]?,option]
dialogue[100] = [02, "Hey, hello!", "Oh, hello there; how's going?", true, 102, null]
dialogue[101] = [02, "I am fine. What's your name?", "My name is Tom; nice to meet you... emh, mr....?", false, null, 99]#option  99 will set npc[02][02] =true

This way doesn’t seem convenient for translation propose, and maybe to put things in separate files?

:bust_in_silhouette: Reply From: Ace-Dragon

Try looking at the dictionary data type in GDscript (it can store entries complete with stats)

:bust_in_silhouette: Reply From: dragoon

Godot already has a mechanism for internalization.

:bust_in_silhouette: Reply From: Bojidar Marinov

You can always make a custom file format, using the File and String classes (and probably RegEx). Also, you can use existing formats with ConfigFile (for .ini) and File+Dictionary::to_json (for.json).

If would still want to use GDScript, you can use Dictionary-es like @Ace-Dragon suggested, or you can make your own class with methods, constructors, etc. For example:

classes.gd:

class NPC:
    var is_friendly
    var name_known
    var name
    var dialogues = []
    func _init(_name = "Somebody", _is_friendly = true, _name_known = true):
        name = _name
        is_friendly = _is_friendly
        name_known = _name_known
    func add_dialogue(dialogue):
        dialogues.push_back(dialogue)
# ... Other classes here, like Dialogue, etc.

data.gd:

func construct_data():
    var tom = NPC.new("Tom", true, false)
    tom.add_dialogue(Dialogue.new("Hey, hello!", ...))

Well, class seems to be the best way to handle/extract variables from files; so I pick this one as answer.

alexwbc | 2016-03-08 11:08

BTW, to anybody who uses the Dictionary or .json suggestions, please, upvote @Ace-Dragon’s answer as well.

Bojidar Marinov | 2016-03-08 14:58