Class ObjectRef<T>
- Namespace
- Cobilas.GodotEngine.Utility
- Assembly
- com.cobilas.godot.utility.dll
Represents a generic reference to a specific type of Godot Node with automatic path resolution and node caching.
[Serializable]
public class ObjectRef<T> : ObjectRef where T : Node
Type Parameters
TThe type of Godot Node this reference points to, must inherit from Godot.Node.
- Inheritance
-
ObjectRef<T>
- Inherited Members
Remarks
This generic class provides type-safe implicit conversions between NodePath, string, and the specific Node type T, making it easier to work with typed node references in Godot while maintaining type safety.
Constructors
ObjectRef()
Initializes a new instance of the ObjectRef<T> class with empty path and null node.
public ObjectRef()
ObjectRef(NodePath)
Initializes a new instance of the ObjectRef<T> class with the specified path.
protected ObjectRef(NodePath path)
Parameters
pathNodePathThe NodePath to initialize the reference with.
Properties
Value
Gets the typed node instance resolved from the path.
public T? Value { get; }
Property Value
- T
The resolved Node instance of type T.
Operators
implicit operator NodePath(ObjectRef<T>?)
Implicitly converts an ObjectRef<T> to a Godot.NodePath.
public static implicit operator NodePath(ObjectRef<T>? obj)
Parameters
objObjectRef<T>The ObjectRef to convert.
Returns
- NodePath
The NodePath contained in the ObjectRef.
Exceptions
- ArgumentNullException
Thrown when obj is null.
implicit operator string(ObjectRef<T>?)
Implicitly converts an ObjectRef<T> to a string.
public static implicit operator string(ObjectRef<T>? obj)
Parameters
objObjectRef<T>The ObjectRef to convert.
Returns
- string
The string representation of the NodePath.
Exceptions
- ArgumentNullException
Thrown when obj is null.
implicit operator T(ObjectRef<T>?)
Implicitly converts an ObjectRef<T> to the specified Node type T.
public static implicit operator T(ObjectRef<T>? obj)
Parameters
objObjectRef<T>The ObjectRef to convert.
Returns
- T
The resolved Node instance of type T.
Remarks
This operator automatically resolves the node from the path and caches it for future use. If the node's path has changed, it will re-resolve the node from the current path. The node is cast to type T, ensuring type safety.
Exceptions
- ArgumentNullException
Thrown when obj is null.
implicit operator ObjectRef<T>(NodePath?)
Implicitly converts a Godot.NodePath to an ObjectRef<T>.
public static implicit operator ObjectRef<T>(NodePath? obj)
Parameters
objNodePathThe NodePath to convert.
Returns
- ObjectRef<T>
A new ObjectRef containing the specified NodePath.
Exceptions
- ArgumentNullException
Thrown when obj is null.
implicit operator ObjectRef<T>(string?)
Implicitly converts a string to an ObjectRef<T>.
public static implicit operator ObjectRef<T>(string? obj)
Parameters
objstringThe string representing a NodePath to convert.
Returns
- ObjectRef<T>
A new ObjectRef containing the specified NodePath.
Exceptions
- ArgumentNullException
Thrown when obj is null.