GetChildren() doesn't return custom class when called in _GetConfigurationWarnings() C#

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

I have a custom global class in my project named WeaponComponent and another called Weapon. The node Weapon requires that it has one or more children of type WeaponComponent so I check whether or not they are present using my method GetWeaponComponents().

GetWeaponComponents() works as intended when called in _Ready() successfully finding the desired node/nodes, but when called in _GetConfigurationWarnings() it fails to find any instance of WeaponComponent even though it is present and instead identifes it as just a plain Node3D (which is the base class of WeaponComponent).

The Weapon class has been provided bellow:

[GlobalClass] [Tool]
public partial class Weapon : Node3D
	private List<WeaponComponent> _weaponComponents;

	public override void _Ready()
		_weaponComponents = GetWeaponComponents();

	private List<WeaponComponent> GetWeaponComponents()
		var components = new List<WeaponComponent>();
		foreach (var child in GetChildren(true))
			if (child is WeaponComponent weaponComponent)

		return components;

	public override string[] _GetConfigurationWarnings()
		var output = new List<string>();

		if (!GetWeaponComponents().Any())
			output.Add("Weapon requires at least one WeaponComponent");
		return output.ToArray();

Any help would be greatly appreciated!

Does WeaponComponent have the [Tool] attribute? Try adding it to see if it makes a difference. Maybe Godot simply isn’t loading your script on these nodes in the editor, therefore C# doesnt find them as such?

When you say it finds WeaponComponent in _Ready, do you mean by running the game? Something to keep in mind, is that _Ready will also run in the editor if you have [Tool]. So are WeaponComponent found in _Ready in the editor? (You can test that by reopening the scene to make _Ready run again).
If the problem also happens in _Ready in the editor, then perhaps it’s really how Godot works regarding the [Tool] attribute…

Zylann | 2023-07-07 12:49

Adding the [Tool] attribute fixed it. Thank you for your help!

dabatt | 2023-07-07 12:59

:bust_in_silhouette: Reply From: FullBatteryCharge

WeaponComponent also needs a [Tool] attribute to cast properly outside of play mode

This fixed the issue. Thank you very much!

dabatt | 2023-07-07 12:50