Class ResourceLoaderInstance
- Namespace
- Godot
- Assembly
- GodotSharp.dll
A singleton used to load resource files from the filesystem.
It uses the many ResourceFormatLoader classes registered in the engine (either built-in or from a plugin) to load files into memory and convert them to a format that can be used by the engine.
Note: You have to import the files into the engine first to load them using Load(string, string, CacheMode). If you want to load Images at run-time, you may use Load(string). If you want to import audio files, you can use the snippet described in Data.
[GodotClassName("ResourceLoader")]
public class ResourceLoaderInstance : GodotObject, IDisposable
- Inheritance
-
ResourceLoaderInstance
- Implements
- Inherited Members
Methods
AddResourceFormatLoader(ResourceFormatLoader, bool)
Registers a new ResourceFormatLoader. The ResourceLoader will use the ResourceFormatLoader as described in Load(string, string, CacheMode).
This method is performed implicitly for ResourceFormatLoaders written in GDScript (see ResourceFormatLoader for more information).
public void AddResourceFormatLoader(ResourceFormatLoader formatLoader, bool atFront = false)
Parameters
formatLoader
ResourceFormatLoaderatFront
bool
Exists(string, string)
Returns whether a recognized resource exists for the given path
.
An optional typeHint
can be used to further specify the Resource type that should be handled by the ResourceFormatLoader. Anything that inherits from Resource can be used as a type hint, for example Image.
Note: If you use TakeOverPath(string), this method will return true for the taken path even if the resource wasn't saved (i.e. exists only in resource cache).
public bool Exists(string path, string typeHint = "")
Parameters
Returns
GetDependencies(string)
Returns the dependencies for the resource at the given path
.
Note: The dependencies are returned with slices separated by ::
. You can use String.get_slice
to get their components.
for dep in ResourceLoader.get_dependencies(path):
print(dep.get_slice("::", 0)) # Prints UID.
print(dep.get_slice("::", 2)) # Prints path.
public string[] GetDependencies(string path)
Parameters
path
string
Returns
- string[]
GetRecognizedExtensionsForType(string)
Returns the list of recognized extensions for a resource type.
public string[] GetRecognizedExtensionsForType(string type)
Parameters
type
string
Returns
- string[]
GetResourceUid(string)
Returns the ID associated with a given resource path, or -1
when no such ID exists.
public long GetResourceUid(string path)
Parameters
path
string
Returns
HasCached(string)
Returns whether a cached resource is available for the given path
.
Once a resource has been loaded by the engine, it is cached in memory for faster access, and future calls to the Load(string, string, CacheMode) method will use the cached version. The cached resource can be overridden by using TakeOverPath(string) on a new resource for that same path.
public bool HasCached(string path)
Parameters
path
string
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
Load(string, string, CacheMode)
Loads a resource at the given path
, caching the result for further access.
The registered ResourceFormatLoaders are queried sequentially to find the first one which can handle the file's extension, and then attempt loading. If loading fails, the remaining ResourceFormatLoaders are also attempted.
An optional typeHint
can be used to further specify the Resource type that should be handled by the ResourceFormatLoader. Anything that inherits from Resource can be used as a type hint, for example Image.
The cacheMode
property defines whether and how the cache should be used or updated when loading the resource. See ResourceLoader.CacheMode for details.
Returns an empty resource if no ResourceFormatLoader could handle the file, and prints an error if no file is found at the specified path.
GDScript has a simplified @GDScript.load
built-in method which can be used in most situations, leaving the use of ResourceLoader for more advanced scenarios.
Note: If ProjectSettings.editor/export/convert_text_resources_to_binary
is true, @GDScript.load
will not be able to read converted files in an exported project. If you rely on run-time loading of files present within the PCK, set ProjectSettings.editor/export/convert_text_resources_to_binary
to false.
Note: Relative paths will be prefixed with "res://"
before loading, to avoid unexpected results make sure your paths are absolute.
public Resource Load(string path, string typeHint = "", ResourceLoader.CacheMode cacheMode = CacheMode.Reuse)
Parameters
path
stringtypeHint
stringcacheMode
ResourceLoader.CacheMode
Returns
LoadThreadedGet(string)
Returns the resource loaded by LoadThreadedRequest(string, string, bool, CacheMode).
If this is called before the loading thread is done (i.e. LoadThreadedGetStatus(string, Array) is not Loaded), the calling thread will be blocked until the resource has finished loading. However, it's recommended to use LoadThreadedGetStatus(string, Array) to known when the load has actually completed.
public Resource LoadThreadedGet(string path)
Parameters
path
string
Returns
LoadThreadedGetStatus(string, Array)
Returns the status of a threaded loading operation started with LoadThreadedRequest(string, string, bool, CacheMode) for the resource at path
. See ResourceLoader.ThreadLoadStatus for possible return values.
An array variable can optionally be passed via progress
, and will return a one-element array containing the percentage of completion of the threaded loading.
Note: The recommended way of using this method is to call it during different frames (e.g., in _Process(double), instead of a loop).
public ResourceLoader.ThreadLoadStatus LoadThreadedGetStatus(string path, Array progress = null)
Parameters
Returns
LoadThreadedRequest(string, string, bool, CacheMode)
Loads the resource using threads. If useSubThreads
is true, multiple threads will be used to load the resource, which makes loading faster, but may affect the main thread (and thus cause game slowdowns).
The cacheMode
property defines whether and how the cache should be used or updated when loading the resource. See ResourceLoader.CacheMode for details.
public Error LoadThreadedRequest(string path, string typeHint = "", bool useSubThreads = false, ResourceLoader.CacheMode cacheMode = CacheMode.Reuse)
Parameters
path
stringtypeHint
stringuseSubThreads
boolcacheMode
ResourceLoader.CacheMode
Returns
RemoveResourceFormatLoader(ResourceFormatLoader)
Unregisters the given ResourceFormatLoader.
public void RemoveResourceFormatLoader(ResourceFormatLoader formatLoader)
Parameters
formatLoader
ResourceFormatLoader
SetAbortOnMissingResources(bool)
Changes the behavior on missing sub-resources. The default behavior is to abort loading.
public void SetAbortOnMissingResources(bool abort)
Parameters
abort
bool