How to add editor warnings for class members?

Godot Version

4.2.2.stable

Question

I’m trying to get the editor to show a warning on a node when the collision layer and masks have a specific configuration. I know this is easily done via setters when doing it for a custom variable but I’m not sure how to do it for a class member.

I did find this thread but I’ll be honest, I have no idea what the answers are saying and I haven’t been able to get it working.

Well, I have made it somewhat work by calling update_configuration_warnings() on _process() but doing it this way feels icky, and also only updates the warnings when changing to a different scene and then back anyway, not in real time.

Thanks!

Hey!

I found this topic that might be what you are looking for!

Hope that was it, good luck!

Thanks, but I’m not sure that answers my question, as it explains how to hook variables declared by the user, not variables that already exist in the class.

I can see that it is possible to call the update_configuration_warnings() from a variable setter, but it’s not possible (afaik) to add a setter for a variable that already exists, in my particular case, an Area3D’s collision_layer.

You can know when a variable is set with Object._set()

Example:


@tool
extends Area3D


func _get_configuration_warnings() -> PackedStringArray:
	var warnings = []
	if collision_layer == 2:
		warnings.push_back('Collision layer warning')
	if collision_mask == 2:
		warnings.push_back('Collision mask warning')

	return warnings


func _set(property: StringName, value: Variant) -> bool:
	match property:
		"collision_layer":
			update_configuration_warnings()
		"collision_mask":
			update_configuration_warnings()

	# Always return false as we aren't doing anything with the variables
	return false

3 Likes

Alternatively this works:

@tool
extends Area3D

func _get_configuration_warnings():
	if collision_layer != 2:
		return ["Collision layer should be set to 2"]
	return []


func _validate_property(property: Dictionary):
	if property.name == "collision_layer":
		update_configuration_warnings()
2 Likes

Thanks!

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