cirnogodot/Scripts/Tools.cs
2025-01-22 11:35:37 +01:00

24 lines
707 B
C#

using Godot;
namespace Cirno.Scripts;
public static class Tools
{
public static T CreateChild<T>(this Node2D node, PackedScene prefab) where T : Node2D
{
return CreateChild<T>(node, prefab, node.Position);
}
public static T CreateChild<T>(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D
{
if (prefab == null) return null;
var newInstance = prefab.Instantiate<T>();
node.GetParent().CallDeferred("add_child", newInstance);
// Need to use parent instead of owner because tilemap scenes have no owner
//node.Owner.CallDeferred("add_child", newInstance);
newInstance.Transform = node.GlobalTransform;
newInstance.Position = position;
return newInstance;
}
}