Bypass setter function?

Godot Version

4.5 beta 1 (any Godot 4)

Question

I have a TextContainer with a property @export_multiline var text : String = "", which has a setter like this:

@export_multiline var text:String = "":
	set(new):
		if new == text:
			return
		text = new
		
		if last_text_handlers != text_handlers:
			update_parser()
			last_text_handlers = text_handlers
		
		# Applies the text visually (parses text into segments and instantiates Control nodes)
		_set_text(new)

The setter is here so that the text can be set - right in the editor.

Now I have another function, which tries to only set the text and not trigger the setter function…
But if I now remove the setter function, the in-editor functionality stops working.

Here is what this, what the setter method accomplishes:

This may not be the most elegant solution, but you could add a variable to keep track of when not to use the setter.
Example:

var _skip_setter := false
@export_multiline var text: String = "":
	set(new):
		if new == text:
			return
		text = new
		if _skip_setter:
			return
		# ...


func set_text_no_setter(new_text: String) -> void:
	_skip_setter = true
	text = new_text
	_skip_setter = false

Yea, that’s a fair solution. I ended up just checking in the setter function for a has_meta("apply_text") and get_meta("apply_text") == false and if so, skip the text application…

Thank you for the idea!

1 Like

You could also check if the script is running in editor or during runtime with Engine.is_editor_hint()

True!
In my case the is_editor_hint() does not work, since I am building an editor-plugin (which, as far as I can tell, cannot check wheather it is in active use or in editing mode).

But thank you for the recommendation either way!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.