Topic was automatically imported from the old Question2Answer platform.
Asked By
jelly
Old Version
Published before Godot 3 was released.
I am a little unclear as to how work the Tree node and the documentation on it is somewhat sparse. I would like to create a list that has multiple top level items, but as you see my second base item pushes out the first.
My code looks as such:
var a = get_node("tree").create_item()
a.set_text(0, "Base 0")
var b = get_node("tree").create_item( a )
b.set_text(0, "Child 0")
var c = get_node("tree").create_item( a )
c.set_text(0, "Child 1")
var aa = get_node("tree").create_item()
aa.set_text(0, "Base 1")
I would think that by not parenting the new item it would be a top level item but apparently this is not the case. I am wondering how to correct this and perhaps a little bit of clarity on how the system itself works. Thanks in advance!
EDIT: Updated the answer as I was doing it blindly.
You need to set_hide_root(true) and then create a dummy root to hold all items.
var root = get_node("tree").create_item()
get_node("tree").set_hide_root(true)
var a = get_node("tree").create_item(root)
a.set_text(0, "Base 0")
var b = get_node("tree").create_item( a )
b.set_text(0, "Child 0")
var c = get_node("tree").create_item( a )
c.set_text(0, "Child 1")
var aa = get_node("tree").create_item(root)
aa.set_text(0, "Base 1")
Thanks for the answer. That makes a lot of sense but I have not been able to get it to work. I now get Base 1 getting put in as a child of Base 0. When I print the result of the get_root() it brings back the Base 0, which as well makes sense, it’s just not quite what I am looking for.
jelly | 2016-07-05 03:09
Sorry, I had not tested it. I updated the answer with a proper solution.