AI Dev-Log 4: NPC Definitions Part 2

Now it's time to actually create the nodes out of the code, for that purpose, let's stick to the Default example.

#Class ID will be assigned from 27 and on, in the order classes are declared
var CLASS_MISC = {
    "_color" : Color(.5,.5,.1,1),
}
var CLASS_CHARACTER =  {
    "_color" : Color(.5,.5,.1,1),
    "_object_type" : "Character",
    "_variables" : {
        "translation" : TYPE_VECTOR3,
        "type" : TYPE_STRING,
        "health" : TYPE_REAL,
        "shield" : TYPE_REAL,
        "target_location" : TYPE_VECTOR3}
    }

var _functions = {
    "function_name" : {
        "_category" : "Inhibitor/Action/Misc",
        "_code" : "<actual code>",
        "_input_ports" : [
            {"_label_title":"title","_type":TYPE_NIL},
            {"_label_title":"title2","_type":TYPE_NIL}
            ],
        "_output_ports" : [
            {"_label_title":"title","_type":TYPE_NIL},
            {"_label_title":"title2","_type":TYPE_NIL}
            ]
    } 
}

var _stimulus = {
    "signal_name" :{
        "_output_name": "name",
        "_output_type": "type"
    }
}
Currently, the editor uses a hard coded approach to the main nodes and a kind of dynamic one for custom nodes, all using a singleton that stores such nodes and also stores colour codes. We will change that to adapt our new definitions.

First, I'll define a function that will return a colour given a TYPE and an Array containing such colours.

It looks like this in the Nodes singleton
var Colors : Array = [
    Color(0,0,0,1),
    Color(0.2,0,0,1),
    Color(0.2,0.2,0,1),
    Color(0.2,0.2,0.2,1),
    Color(0.2,0.2,0.4,1),
    Color(0.2,0.4,0.4,1),
    Color(0.4,0.4,0.4,1),
    Color(0.4,0,0),
    Color(0.6,0.4,0.4,1),
    Color(0.6,0.6,0.4,1),
    Color(0.6,0.6,0.6,1),
    Color(0.6,0.6,0.8,1),
    Color(0.6,0.8,0.8,1),
    Color(0.8,0.8,0.8,1),
    Color(1,0.8,0.8,1),
    Color(1,1,0.8,1),
    Color(1,1,1,1),
    Color(1,0,1,1),
    Color(1,1,0,1),
    Color(0.5, 0.2, 0.2, 1),
    Color(0.1,0.6,0.1, 1),
    Color(0.6, 0.2, 0.7, 1),
    Color(0.9,0.1,0.9,1),
    Color(0.2,0,0.2,1),
    Color(0.7, 0, 0.5),
    Color(0.5, 0, 0.5),
    Color(0.2, 1, 0.1),
    Color(1,0.5,0.2,1)
    ] 

func Color(id) -> Color:
    return Colors[id]
We will further add to this array, so it'll remain as a var and not a constant. The colors were chosen with little consideration.

We will now start working on how to actually load the definitions, this is where the keywords actually play a bigger role.
First, we want to be sure the file was correctly written.

So we implement a function to quickly check if that's true:

func _is_format_correct() -> bool:
    if Definitions.get("_functions") == null or Definitions.get("_stimulus") == null:
        return false
    else:
        return true

And now, we start working on the function that loads the Definitions:

func _load_definitions() -> void:
    if not _is_format_correct():
        return
    var all_properties = Definitions.get_property_list()
    var Classes : Array = []
    for properties in all_properties:
        if properties.get("name").begins_with("CLASS_"):
            Classes.append(properties.get("name"))
    print(Classes)

This code snippet will give us only the names of the classes defined in NPCDefinitions. Currently prints:

[CLASS_MISC, CLASS_CHARACTER]

And this information will be quite useful to get the nodes set up. I'll cover that topic in the next post.