0 votes

I would like to have the contents of my OptionButton load from different dictionaries depending upon from which scene it is loaded. If loaded from the "Beginner" scene (with both (res://Beginner.tscn) and Node name "Beginner", it will have beginner items, if from the "Advanced" scene, advanced items. These menus are never loaded at the same time.

extends OptionButton

var context = Node.get_tree().get_root().get_node()

func _ready():
    add_items()
    pass

func add_items():
    if context == "Beginner":
        self.add_item("beginner item")
    if context == "Advanced":
        self.add_item("advanced item")

Currently am getting 'Invalid call. Nonexistant function 'get_tree' in base 'GDScriptNativeClass', and the list remains unpopulated.
If I take out "Node.", it cant "get root in bade 'null instance'"...
Essentially, it's not even getting the tree... Not sure if the debugger is referring to Native language, or the that some things just cant be done in certain nodes... THX!

in Engine by (12 points)

1 Answer

0 votes

get_tree() is not static method, so Node.get_tree() is not vaild expression.

and get_tree() returns correct SceneTree reference only when the node is added to scene tree.

one more thing, get_node() requires a node path.
so, path should be provided or use another method like get_child(0)

onready var context = get_tree().get_root().get_child(0)

or

var context

func _ready():
    context = get_tree().get_root().get_child(0)

both examples will work.

by (9,688 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.