{"name":"Arma 3: Tree View Path","title":"Arma 3: Tree View Path","type":"topic","url":"https://community.bistudio.com/wiki/Arma_3:_Tree_View_Path","revid":338770,"updated":"2023-04-05T16:50:45Z","aliases":["Tree View Path"],"markdown":"Tree View path for referencing Tree View item is not the same as the path used for adding items to the Tree View.\nThis may cause quite a bit of confusion as to the usage of the Tree View commands.\nIn short, when adding new items to the Tree View, one needs to give the path of the parent item to which the item is added and not the desired path for the new item.\nThe command will then return index of added item relative to the parent item. If parent item doesn't exist (happens when item is added to the main trunk of the tree), the parent path needs to be an empty array `[]`.\n\nThis is only relevant to the tvAdd command. All other Tree View commands reference the actual item path. So here are 2 examples for tvAdd and tvDelete:\n\n```sqf\n_index1   = _ctrl tvAdd [[], \"Item1\"];        // adds \"Item1\" to main branch. Item path [_index1] or [0]  \n_index1_1 = _ctrl tvAdd [[0], \"Item1_1\"];    // adds \"Item1_1\" to \"Item1\". Item path [0,_index1_1] or [0,0]\n_index1_2 = _ctrl tvAdd [[0], \"Item1_2\"];    // adds \"Item1_2\" to \"Item1\". Item path [0,_index1_2] or [0,1]\n_index2_1 = _ctrl tvAdd [[0,0], \"Item2_1\"];    // adds \"Item2_1\" to \"Item1_1\". Item path [0,0,_index2_1] or [0,1,0]\n```\n\nAs you can see, the parent item path is referenced in each tvAdd command. Let's look at tvDelete. The item path will be the actual path to the item.\n\n```sqf\n_ctrl tvDelete [0,0]; // deletes \"Item1_1\"\n_ctrl tvDelete [0,1]; // doesn't delete \"Item1_2\"\n```\n\nThe reason \"Item1_2\" is not deleted is because its path changed from [0,1] to [0,0] after we deleted \"Item1_1\".\n\n```sqf\n_ctrl tvDelete [0,0]; // deletes \"Item1_2\"\n```\n\nNow \"Item1_2\" is also deleted because the path for it is now correct.\n\nCode used to generate the Tree View thumbnail:\n\n```sqf\n_disp = findDisplay 46 createDisplay \"RscDisplayEmpty\";\n_bg = _disp ctrlCreate [\"RscText\", -1];\n_bg ctrlSetPosition [safeZoneX, safeZoneY, safeZoneW, safeZoneH];\n_bg ctrlSetBackgroundColor [0.5, 0.5, 0.5, 1];\n_bg ctrlCommit 0;\n_ctrl = _disp ctrlCreate [\"RscTree\", -1];\n_ctrl ctrlSetPosition [0, 0, 1, 1];\n_ctrl ctrlCommit 0;\n_ctrl ctrlSetFontHeight 0.03;\n_ctrl ctrlSetFont \"PuristaMedium\";\nfor \"_i\" from 0 to 1 do \n{\n    _color = [1, 1, 1, 1];\n    _ctrl tvAdd [[], format [\"Parent Path: %1 | Item Path: %2\", [], [_i]]];\n    _ctrl tvSetColor [[_i], _color];\n    for \"_j\" from 0 to 1 do \n    {\n        _color = [1, 0, 0, 1];\n        _ctrl tvAdd [[_i], format [\"Parent Path: %1 | Item Path: %2\", [_i], [_i, _j]]];\n        _ctrl tvSetColor [[_i, _j], _color];\n        for \"_k\" from 0 to 1 do \n        {\n            _color = [0, 1, 0, 1];\n            _ctrl tvAdd [[_i, _j], format [\"Parent Path: %1 | Item Path: %2\", [_i, _j], [_i, _j, _k]]];\n            _ctrl tvSetColor [[_i, _j, _k], _color];\n            for \"_n\" from 0 to 1 do \n            {\n                _color = [0, 0, 1, 1];\n                _ctrl tvAdd [[_i, _j, _k], format [\"Parent Path: %1 | Item Path: %2\", [_i, _j, _k], [_i, _j, _k, _n]]];\n                _ctrl tvSetColor [[_i, _j, _k, _n], _color];\n            };\n        };\n    };\n};\n\ntvExpandAll _ctrl;\n_ctrl ctrlAddEventHandler [\"TreeSelChanged\", { hint format [\"Item Path: %1\", _this select 1] }];\n```","sections":[{"heading":null,"level":1,"markdown":"Tree View path for referencing Tree View item is not the same as the path used for adding items to the Tree View.\nThis may cause quite a bit of confusion as to the usage of the Tree View commands.\nIn short, when adding new items to the Tree View, one needs to give the path of the parent item to which the item is added and not the desired path for the new item.\nThe command will then return index of added item relative to the parent item. If parent item doesn't exist (happens when item is added to the main trunk of the tree), the parent path needs to be an empty array `[]`.\n\nThis is only relevant to the tvAdd command. All other Tree View commands reference the actual item path. So here are 2 examples for tvAdd and tvDelete:\n\n```sqf\n_index1   = _ctrl tvAdd [[], \"Item1\"];        // adds \"Item1\" to main branch. Item path [_index1] or [0]  \n_index1_1 = _ctrl tvAdd [[0], \"Item1_1\"];    // adds \"Item1_1\" to \"Item1\". Item path [0,_index1_1] or [0,0]\n_index1_2 = _ctrl tvAdd [[0], \"Item1_2\"];    // adds \"Item1_2\" to \"Item1\". Item path [0,_index1_2] or [0,1]\n_index2_1 = _ctrl tvAdd [[0,0], \"Item2_1\"];    // adds \"Item2_1\" to \"Item1_1\". Item path [0,0,_index2_1] or [0,1,0]\n```\n\nAs you can see, the parent item path is referenced in each tvAdd command. Let's look at tvDelete. The item path will be the actual path to the item.\n\n```sqf\n_ctrl tvDelete [0,0]; // deletes \"Item1_1\"\n_ctrl tvDelete [0,1]; // doesn't delete \"Item1_2\"\n```\n\nThe reason \"Item1_2\" is not deleted is because its path changed from [0,1] to [0,0] after we deleted \"Item1_1\".\n\n```sqf\n_ctrl tvDelete [0,0]; // deletes \"Item1_2\"\n```\n\nNow \"Item1_2\" is also deleted because the path for it is now correct.\n\nCode used to generate the Tree View thumbnail:\n\n```sqf\n_disp = findDisplay 46 createDisplay \"RscDisplayEmpty\";\n_bg = _disp ctrlCreate [\"RscText\", -1];\n_bg ctrlSetPosition [safeZoneX, safeZoneY, safeZoneW, safeZoneH];\n_bg ctrlSetBackgroundColor [0.5, 0.5, 0.5, 1];\n_bg ctrlCommit 0;\n_ctrl = _disp ctrlCreate [\"RscTree\", -1];\n_ctrl ctrlSetPosition [0, 0, 1, 1];\n_ctrl ctrlCommit 0;\n_ctrl ctrlSetFontHeight 0.03;\n_ctrl ctrlSetFont \"PuristaMedium\";\nfor \"_i\" from 0 to 1 do \n{\n    _color = [1, 1, 1, 1];\n    _ctrl tvAdd [[], format [\"Parent Path: %1 | Item Path: %2\", [], [_i]]];\n    _ctrl tvSetColor [[_i], _color];\n    for \"_j\" from 0 to 1 do \n    {\n        _color = [1, 0, 0, 1];\n        _ctrl tvAdd [[_i], format [\"Parent Path: %1 | Item Path: %2\", [_i], [_i, _j]]];\n        _ctrl tvSetColor [[_i, _j], _color];\n        for \"_k\" from 0 to 1 do \n        {\n            _color = [0, 1, 0, 1];\n            _ctrl tvAdd [[_i, _j], format [\"Parent Path: %1 | Item Path: %2\", [_i, _j], [_i, _j, _k]]];\n            _ctrl tvSetColor [[_i, _j, _k], _color];\n            for \"_n\" from 0 to 1 do \n            {\n                _color = [0, 0, 1, 1];\n                _ctrl tvAdd [[_i, _j, _k], format [\"Parent Path: %1 | Item Path: %2\", [_i, _j, _k], [_i, _j, _k, _n]]];\n                _ctrl tvSetColor [[_i, _j, _k, _n], _color];\n            };\n        };\n    };\n};\n\ntvExpandAll _ctrl;\n_ctrl ctrlAddEventHandler [\"TreeSelChanged\", { hint format [\"Item Path: %1\", _this select 1] }];\n```"}],"categories":["Arrays","Introduced with Arma 3 version 0.74"],"groups":["Scripting"]}