I want to add child nodes of certain types when I create a new instance of my class.
I haven’t found any notifications or other ways to achieve this.
Basically I want something like this run when I create a new scene instance in editor. But currently it works only when the scene is run.
public override void _Notification(int what)
{
if (what == NotificationSceneInstantiated)
{
if (Health == null && GetNodeOrNull<Health>("Health") == null)
{
Health = new Health();
AddChild(Health);
}
}
}
And [Tool] seems not to work for this?
I know I could simply add those nodes manually, but I’m lazy.
1 Like
Moreus
July 21, 2024, 9:18am
2
for [Tool] maybe better use
public override string[] _GetConfigurationWarnings()
this should work in editor,
I don’t know what is health but maybe is better make resource instead node??
@trizZzle
if you declare your resource with new() you will have not null resource by default.
while your class
is Health : Resource
[export] Health health = new();
resources work better when have [GlobalClass] without you can put any resource in inspector what not really make sense for C#
Do you mean you don’t see healh in the visual tree in the editor? if that’s your problem do something like this:
Health = new Health();
AddChild(Health);
health.Owner = this;<=======have to be assigned after addchild
Moreus
August 1, 2024, 7:34pm
4
I believe after'AddChild(Health);
health.Owner == this
will be true
after ‘AddChild(Health);’
‘health.Owner == this’ will be false as ‘Owner’ will be null
1 Like
I mean, do you want a warning or you want to automatically create the Node if it doesn’t exist?
Here’s how to create custom warnings for scenes.
Godot Version
4.2.stable
Question
I would like to report my own node configuration warnings, but following documentation exactly and making a simple prototype, I have never gotten it to work. What am I missing?
My scene is a single Node2D with this attached script. When the set_me variable is changed in the editor, the configuration should go off accordingly. Even just returning a warning with no conditionals in _get_configuration_warning() does not work.
@tool
extends Node2D
@export var s…
Or, in that method (_get_configuration_warnings) you could instantiate the missing node if you find it’s not assigned instead of sending a warning.
2 Likes
verry intresting thank you