Skip to content

[Question] How to handle same key name ? #1

Description

@Manukineko

Hello, I just found your library recently and even if you write it three years ago, I have a question :D

I noticed that a key value should be unique for the find method to work efficiently. If not, the method will return the first occurence found in the tree.

How would you handle those use case :

weapon = new Tree()

// type
var sword = tree.add("sword", WEAPON.SWORD);
var axe = tree.add("axe", WEAPON.AXE);

// stats
var sword_attack = sword.add("attack", 10);
var sword_sprite = sword.add("sprite", spr_sword);

var axe_attack = axe.add("attack", 15);
var axe_sprite = axe.add("sprite", spr_axe);

Here each of the sword and axe tree got an attack and sprite key.

if I do weapon.find("attack"), it will obviously return the first one found in the list (probably the sword one since it was the first declared)
The proper way to get it is to chain the find method :

var _attack = weapon.find("sword").find("attack").value

But what if I don't know the number of level ? Or if I want to return all the attack value ? Or all the sword tree if, for instance, I have two weapon tree each cjild of right_hand and left_hand, themselves child of a character tree ?

The two way that come at the top of my head are:

  • a find_all method that would return an array of the relevent trees.
  • a find_from_parent where you would have also a parent parameter that would look one level up the tree (and/or recursively, like the find method does it ?)

Waht do you think ? Am I thinking too much about it ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions