From 3f50954ced85474b3f92d26dbe7bd634f873b378 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Mon, 5 Oct 2020 23:40:50 -0400 Subject: Add generic support to PackedScene.Instance --- .../Core/Extensions/PackedSceneExtensions.cs | 27 ++++++++++++++++++++++ .../glue/GodotSharp/GodotSharp/GodotSharp.csproj | 1 + 2 files changed, 28 insertions(+) create mode 100644 modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/PackedSceneExtensions.cs diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/PackedSceneExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/PackedSceneExtensions.cs new file mode 100644 index 0000000000..763f470504 --- /dev/null +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/PackedSceneExtensions.cs @@ -0,0 +1,27 @@ +namespace Godot +{ + public partial class PackedScene + { + /// + /// Instantiates the scene's node hierarchy, erroring on failure. + /// Triggers child scene instantiation(s). Triggers a + /// `Node.NotificationInstanced` notification on the root node. + /// + /// The type to cast to. Should be a descendant of Node. + public T Instance(PackedScene.GenEditState editState = (PackedScene.GenEditState)0) where T : class + { + return (T)(object)Instance(editState); + } + + /// + /// Instantiates the scene's node hierarchy, returning null on failure. + /// Triggers child scene instantiation(s). Triggers a + /// `Node.NotificationInstanced` notification on the root node. + /// + /// The type to cast to. Should be a descendant of Node. + public T InstanceOrNull(PackedScene.GenEditState editState = (PackedScene.GenEditState)0) where T : class + { + return Instance(editState) as T; + } + } +} diff --git a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj index 86a16c17f1..7c1a23d510 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj +++ b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj @@ -30,6 +30,7 @@ + -- cgit v1.2.3