Documentation
¶
Overview ¶
Resource is the base class for all Godot-specific resource types, serving primarily as data containers. Since they inherit from graphics.gd/classdb/RefCounted, resources are reference-counted and freed when no longer in use. They can also be nested within other resources, and saved on disk. graphics.gd/classdb/PackedScene, one of the most common [graphics.gd/classdb/Object]s in a Godot project, is also a resource, uniquely capable of storing and instantiating the [graphics.gd/classdb/Node]s it contains as many times as desired.
In GDScript, resources can loaded from disk by their Instance.ResourcePath using [graphics.gd/classdb/@GDScript.Instance.Load] or [graphics.gd/classdb/@GDScript.Instance.Preload].
The engine keeps a global cache of all loaded resources, referenced by paths (see graphics.gd/classdb/ResourceLoader.HasCached). A resource will be cached when loaded for the first time and removed from cache once all references are released. When a resource is cached, subsequent loads using its path will return the cached reference.
Note: In C#, resources will not be freed instantly after they are no longer in use. Instead, garbage collection will run periodically and will free resources that are no longer in use. This means that unused resources will remain in memory for a while before being removed.
Index ¶
- func Duplicate[T Any](res T) T
- func GenerateSceneUniqueId() string
- func Load[T Any, P string | Path.ToResource](path_to_resource P) T
- type Advanced
- type Any
- type DeepDuplicateMode
- type Expanded
- type Extension
- type ID
- type Implementation
- type Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) AsResource() Instance
- func (self Instance) Duplicate() Instance
- func (self Instance) DuplicateDeep() Instance
- func (self Instance) EmitChanged()
- func (self Instance) GetIdForPath(path string) string
- func (self Instance) GetRid() ID
- func (self Instance) IsBuiltIn() bool
- func (self Instance) OnChanged(cb func(), flags ...Signal.Flags)
- func (self Instance) OnSetupLocalToSceneRequested(cb func(), flags ...Signal.Flags)
- func (self Instance) ResetState()
- func (self Instance) ResourceLocalToScene() bool
- func (self Instance) ResourceName() string
- func (self Instance) ResourcePath() string
- func (self Instance) ResourceSceneUniqueId() string
- func (self Instance) SetIdForPath(path string, id string)
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) SetPathCache(path string)
- func (self Instance) SetResourceLocalToScene(value bool)
- func (self Instance) SetResourceName(value string)
- func (self Instance) SetResourcePath(value string)
- func (self Instance) SetResourceSceneUniqueId(value string)
- func (self Instance) SetupLocalToScene()
- func (self Instance) TakeOverPath(path string)
- func (self Instance) Virtual(name string) reflect.Value
- type Interface
- type UID
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Duplicate ¶
func Duplicate[T Any](res T) T
Duplicate this resource, returning a new resource with its exported or PROPERTY_USAGE_STORAGE properties copied from the original.
func GenerateSceneUniqueId ¶
func GenerateSceneUniqueId() string
Generates a unique identifier for a resource to be contained inside a graphics.gd/classdb/PackedScene, based on the current date, time, and a random value. The returned string is only composed of letters (a to y) and numbers (0 to 8). See also Instance.ResourceSceneUniqueId.
Types ¶
type Advanced ¶
type Advanced = class
Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.
type DeepDuplicateMode ¶
type DeepDuplicateMode int //gd:Resource.DeepDuplicateMode
const ( // No subresorces at all are duplicated. This is useful even in a deep duplication to have all the arrays and dictionaries duplicated but still pointing to the original resources. DeepDuplicateNone DeepDuplicateMode = 0 // Only subresources without a path or with a scene-local path will be duplicated. DeepDuplicateInternal DeepDuplicateMode = 1 // Every subresource found will be duplicated, even if it has a non-local path. In other words, even potentially big resources stored separately will be duplicated. DeepDuplicateAll DeepDuplicateMode = 2 )
type Expanded ¶
func (Expanded) Duplicate ¶
Duplicates this resource, returning a new resource with its exported or [PropertyUsageStorage] properties copied from the original.
If 'deep' is false, a shallow copy is returned: nested slice, data structure, and graphics.gd/classdb/Resource properties are not duplicated and are shared with the original resource.
If 'deep' is true, a deep copy is returned: all nested arrays, dictionaries, and packed arrays are also duplicated (recursively). Any graphics.gd/classdb/Resource found inside will only be duplicated if it's local, like DeepDuplicateInternal used with Instance.DuplicateDeep.
The following exceptions apply:
- Subresource properties with the [PropertyUsageAlwaysDuplicate] flag are always duplicated (recursively or not, depending on 'deep').
- Subresource properties with the [PropertyUsageNeverDuplicate] flag are never duplicated.
Note: For custom resources, this method will fail if graphics.gd/classdb/Object.Instance.Init has been defined with required parameters.
Note: When duplicating with 'deep' set to true, each resource found, including the one on which this method is called, will be only duplicated once and referenced as many times as needed in the duplicate. For instance, if you are duplicating resource A that happens to have resource B referenced twice, you'll get a new resource A' referencing a new resource B' twice.
func (Expanded) DuplicateDeep ¶
func (self Expanded) DuplicateDeep(deep_subresources_mode DeepDuplicateMode) Instance
Duplicates this resource, deeply, like Instance.Duplicate(true), with extra control over how subresources are handled.
'deep_subresources_mode' must be one of the values from DeepDuplicateMode.
type Extension ¶
Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this [Extension]See Interface for methods that can be overridden by T.
func (*Extension[T]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
func (*Extension[T]) AsResource ¶
type ID ¶
ID that uniquely identifies a resource.
func AllocateID ¶
func AllocateID() ID
New allocates a unique ID which can be used by the implementation to construct an RID. This is used mainly from native extensions to implement servers.
type Implementation ¶
type Implementation = implementation
Implementation implements Interface with empty methods.
type Instance ¶
Instance of the class with convieniently typed arguments and results.
var Nil Instance
Nil is a nil/null instance of the class. Equivalent to the zero value.
func (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) AsResource ¶
func (Instance) Duplicate ¶
Duplicates this resource, returning a new resource with its exported or [PropertyUsageStorage] properties copied from the original.
If 'deep' is false, a shallow copy is returned: nested slice, data structure, and graphics.gd/classdb/Resource properties are not duplicated and are shared with the original resource.
If 'deep' is true, a deep copy is returned: all nested arrays, dictionaries, and packed arrays are also duplicated (recursively). Any graphics.gd/classdb/Resource found inside will only be duplicated if it's local, like DeepDuplicateInternal used with Instance.DuplicateDeep.
The following exceptions apply:
- Subresource properties with the [PropertyUsageAlwaysDuplicate] flag are always duplicated (recursively or not, depending on 'deep').
- Subresource properties with the [PropertyUsageNeverDuplicate] flag are never duplicated.
Note: For custom resources, this method will fail if graphics.gd/classdb/Object.Instance.Init has been defined with required parameters.
Note: When duplicating with 'deep' set to true, each resource found, including the one on which this method is called, will be only duplicated once and referenced as many times as needed in the duplicate. For instance, if you are duplicating resource A that happens to have resource B referenced twice, you'll get a new resource A' referencing a new resource B' twice.
func (Instance) DuplicateDeep ¶
Duplicates this resource, deeply, like Instance.Duplicate(true), with extra control over how subresources are handled.
'deep_subresources_mode' must be one of the values from DeepDuplicateMode.
func (Instance) EmitChanged ¶
func (self Instance) EmitChanged()
Emits the Instance.OnChanged signal. This method is called automatically for some built-in resources.
Note: For custom resources, it's recommended to call this method whenever a meaningful change occurs, such as a modified property. This ensures that custom [graphics.gd/classdb/Object]s depending on the resource are properly updated.
func (Instance) GetIdForPath ¶
From the internal cache for scene-unique IDs, returns the ID of this resource for the scene at 'path'. If there is no entry, an empty string is returned. Useful to keep scene-unique IDs the same when implementing a VCS-friendly custom resource format by extending graphics.gd/classdb/ResourceFormatLoader and graphics.gd/classdb/ResourceFormatSaver.
Note: This method is only implemented when running in an editor context. At runtime, it returns an empty string.
func (Instance) GetRid ¶
Returns the [Resource.ID] of this resource (or an empty RID). Many resources (such as graphics.gd/classdb/Texture2D, graphics.gd/classdb/Mesh, and so on) are high-level abstractions of resources stored in a specialized server (graphics.gd/classdb/DisplayServer, graphics.gd/classdb/RenderingServer, etc.), so this function will return the original [Resource.ID].
func (Instance) IsBuiltIn ¶
Returns true if the resource is saved on disk as a part of another resource's file.
func (Instance) OnSetupLocalToSceneRequested ¶
func (Instance) ResetState ¶
func (self Instance) ResetState()
Makes the resource clear its non-exported properties. See also [Interface.ResetState]. Useful when implementing a custom resource format by extending graphics.gd/classdb/ResourceFormatLoader and graphics.gd/classdb/ResourceFormatSaver.
func (Instance) ResourceLocalToScene ¶
func (Instance) ResourceName ¶
func (Instance) ResourcePath ¶
func (Instance) ResourceSceneUniqueId ¶
func (Instance) SetIdForPath ¶
In the internal cache for scene-unique IDs, sets the ID of this resource to 'id' for the scene at 'path'. If 'id' is empty, the cache entry for 'path' is cleared. Useful to keep scene-unique IDs the same when implementing a VCS-friendly custom resource format by extending graphics.gd/classdb/ResourceFormatLoader and graphics.gd/classdb/ResourceFormatSaver.
Note: This method is only implemented when running in an editor context.
func (Instance) SetPathCache ¶
Sets the resource's path to 'path' without involving the resource cache. Useful for handling [ResourceFormatLoader.CacheMode] values when implementing a custom resource format by extending graphics.gd/classdb/ResourceFormatLoader and graphics.gd/classdb/ResourceFormatSaver.
func (Instance) SetResourceLocalToScene ¶
func (Instance) SetResourceName ¶
func (Instance) SetResourcePath ¶
func (Instance) SetResourceSceneUniqueId ¶
func (Instance) SetupLocalToScene ¶
func (self Instance) SetupLocalToScene()
Calls [Interface.SetupLocalToScene]. If Instance.ResourceLocalToScene is set to true, this method is automatically called from graphics.gd/classdb/PackedScene.Instance.Instantiate by the newly duplicated resource within the scene instance.
func (Instance) TakeOverPath ¶
Sets the Instance.ResourcePath to 'path', potentially overriding an existing cache entry for this path. Further attempts to load an overridden resource by path will instead return this resource.
type Interface ¶
type Interface interface { // Override this method to customize the newly duplicated resource created from [graphics.gd/classdb/PackedScene.Instance.Instantiate], if the original's [Instance.ResourceLocalToScene] is set to true. // // Example: Set a random damage value to every local resource from an instantiated scene: // // // // extends Resource // // // // var damage = 0 // // // // func _setup_local_to_scene(): // // damage = randi_range(10, 40) // // SetupLocalToScene() // Override this method to return a custom [Resource.ID] when [Instance.GetRid] is called. GetRid() ID // For resources that store state in non-exported properties, such as via [graphics.gd/classdb/Object.Instance.ValidateProperty] or [graphics.gd/classdb/Object.Instance.GetPropertyList], this method must be implemented to clear them. ResetState() // Override this method to execute additional logic after [Instance.SetPathCache] is called on this object. SetPathCache(path string) }