context variable for dropdown menu

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By trumpetfish1

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!

:bust_in_silhouette: Reply From: volzhs

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.