-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from theowiik:instanting-improvements
Add a SceneGetter util
- Loading branch information
Showing
6 changed files
with
62 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace GodotSharper.Instancing; | ||
|
||
/// <summary> | ||
/// Attribute used to mark a class as a scene and specify its path. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class)] | ||
public sealed class SceneAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SceneAttribute" /> class with the specified path. | ||
/// </summary> | ||
/// <param name="path">The path of the scene.</param> | ||
public SceneAttribute(string path) | ||
{ | ||
Path = path; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the path of the scene. | ||
/// </summary> | ||
public string Path { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Reflection; | ||
using Godot; | ||
|
||
namespace GodotSharper.Instancing; | ||
|
||
/// <summary> | ||
/// A static class that provides a method to get the path of a scene associated with a given type. | ||
/// </summary> | ||
public static class SceneGetter | ||
{ | ||
/// <summary> | ||
/// Gets the path of a scene associated with the specified type. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the node associated with the scene.</typeparam> | ||
/// <returns>The path of the scene associated with the specified type.</returns> | ||
/// <exception cref="ArgumentException">Thrown when the specified type does not have a SceneAttribute.</exception> | ||
public static string GetPath<T>() | ||
where T : Node | ||
{ | ||
var type = typeof(T); | ||
var attribute = type.GetCustomAttribute<SceneAttribute>(); | ||
|
||
if (attribute == null) | ||
{ | ||
throw new ArgumentException($"Type {type} does not have a {nameof(SceneAttribute)}"); | ||
} | ||
|
||
return attribute.Path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters