Code that only runs in specific Godot versions

Godot Version

4.3 to 4.6

Question

I have a tool that I want to use in Godot 4.3 and 4.6. There is a new class (EditorDock) in 4.6 that I can use to add some nice-to-have features. Obviously, if I add it to my code, it stops compiling in 4.3.

var dock = EditorDock.new()

I’m looking for alternatives to work-around this automatically, for example by using Engine.get_version_info() to check the current version, and only then calling dynamic code.

I tried to use the Expression class, but if I try to evaluate “EditorDock.new()” it fails, and as per this GitHub issue, it seems expressions don’t have access to class names.

Did you try solutions provided in this #9160 issue you linked? If you have and it didn’t work - what was the problem there?

By the way, what’s the reason you want to make this work for both 4.3 and 4.6, if the feature is available in 4.6 only anyway?

I had a similar problem where I wanted to detect if a plugin was available. I ended up with the following solution:

	if get_tree().root.has_node("Sound"):
		var sound: Variant = get_tree().root.get_node("Sound")
		sound.play_ui_sound(sound.get_sound("button_pressed"))

What you can do is this:

	var version: Dictionary = Engine.get_version_info()
	if version.major == 4:
		if version.minor >= 6:
			print("Do 4.6 Stuff")
		elif version.minor >= 3:
			print("Do 4.3 Stuff")

The problem is that unless you know where the script is stored in the system and have a path for it, there’s no way to call new() on a class using a StringName for a class. Without that, you run into the problem you’re seeing.

I think the best thing you can do is create a 4.3 version and release it, and then a 4.6 version.

1 Like

Well, I didn’t like the solution in that issue (which is only one as far as I can tell), it looks easy to break, but your comment made me reconsider, and it actually worked:

This is the code, in case it helps anyone:

	var editor_dock_script : GDScript = GDScript.new()
	editor_dock_script.set_source_code("static func eval(): return EditorDock")
	var error = editor_dock_script.reload()
	if error == OK:
		_dock = editor_dock_script.eval().new()
		_dock.default_slot = _dock.DOCK_SLOT_BOTTOM
		_dock.set_available_layouts(_dock.DOCK_LAYOUT_HORIZONTAL | _dock.DOCK_LAYOUT_FLOATING);
		_dock.add_child(_hub_dock)
		call("add_dock", _dock)
		_dock.open()

The only problem is that, in versions less than 4.6, it throws an error at load time:

gdscript://-9223353397183162037.gd:1 - Parse Error: Identifier "EditorDock" not declared in the current scope.

So I will do what dragonforge-dev said and run this conditionally.

To answer your question, it is not that I’m trying to make the feature to work in 4.3 and 4.6, I simply support my plugin from version 4.3, and even when making floating panels is a nice feature in 4.6, I don’t consider it critical enough to stop supporting 4.3 just for that.

2 Likes