Plasma Engine  2.0
Loading...
Searching...
No Matches
plCollectionResource Class Reference

An plCollectionResource is used to tell the engine about resources that it should preload in the background. More...

#include <CollectionResource.h>

Inheritance diagram for plCollectionResource:

Public Member Functions

void RegisterNames ()
 Registers the named resources in the collection with the plResourceManager, such that they can be loaded by those names.
 
void UnregisterNames ()
 Removes the registered names from the plResourceManager.
 
bool PreloadResources (plUInt32 uiNumResourcesToPreload=plMath::MaxValue< plUInt32 >())
 Puts up to the given number of resources for which a resource type could be found into the preload queue of the plResourceManager.
 
bool IsLoadingFinished (float *out_pProgress=nullptr) const
 Returns true if all resources added for preloading via PreloadResources have finished loading. if out_progress is defined:
 
const plCollectionResourceDescriptorGetDescriptor () const
 Returns the resource descriptor for this resource.
 
plArrayPtr< const plTypelessResourceHandleGetPreloadedResources () const
 Returns the current list of resources that have already been added to the preload list. See PreloadResources().
 
- Public Member Functions inherited from plResource
PL_ALWAYS_INLINE const plStringGetResourceID () const
 Returns the unique ID that identifies this resource. On a file resource this might be a path. Can also be a GUID or any other scheme that uniquely identifies the resource.
 
PL_ALWAYS_INLINE plUInt64 GetResourceIDHash () const
 Returns the hash of the unique ID.
 
void SetResourceDescription (plStringView sDescription)
 The resource description allows to store an additional string that might be more descriptive during debugging, than the unique ID.
 
const plStringGetResourceDescription () const
 The resource description allows to store an additional string that might be more descriptive during debugging, than the unique ID.
 
const plStringGetResourceIdOrDescription () const
 The returns the resource description, if available, otherwise the resource ID.
 
PL_ALWAYS_INLINE plResourceState GetLoadingState () const
 Returns the current state in which this resource is in.
 
PL_ALWAYS_INLINE plUInt8 GetNumQualityLevelsDiscardable () const
 Returns the current maximum quality level that the resource could have.
 
PL_ALWAYS_INLINE plUInt8 GetNumQualityLevelsLoadable () const
 Returns how many quality levels the resource may additionally load.
 
float GetLoadingPriority (plTime now) const
 Returns the priority that is used by the resource manager to determine which resource to load next.
 
plResourcePriority GetPriority () const
 Returns the current resource priority.
 
void SetPriority (plResourcePriority priority)
 Changes the current resource priority.
 
PL_ALWAYS_INLINE const plBitflags< plResourceFlags > & GetBaseResourceFlags () const
 Returns the basic flags for the resource type. Mostly used the resource manager.
 
PL_ALWAYS_INLINE const MemoryUsageGetMemoryUsage () const
 Returns the information about the current memory usage of the resource.
 
PL_ALWAYS_INLINE plTime GetLastAcquireTime () const
 Returns the time at which the resource was (tried to be) acquired last. If a resource is acquired using plResourceAcquireMode::PointerOnly, this does not update the last acquired time, since the resource is not acquired for full use.
 
PL_ALWAYS_INLINE plInt32 GetReferenceCount () const
 Returns the reference count of this resource.
 
PL_ALWAYS_INLINE const plTimestampGetLoadedFileModificationTime () const
 Returns the modification date of the file from which this resource was loaded.
 
PL_ALWAYS_INLINE plUInt32 GetCurrentResourceChangeCounter () const
 Returns the current value of the resource change counter. Can be used to detect whether the resource has changed since using it last time.
 
PL_ALWAYS_INLINE void IncResourceChangeCounter ()
 Allows to manually increase the resource change counter to signal that dependent code might need to update.
 
virtual void ResetResource ()
 If the resource has modifications from the original state, it should reset itself to that state now (or force a reload on itself).
 
void PrintHandleStackTraces ()
 Prints the stack-traces for all handles that currently reference this resource.
 
- Public Member Functions inherited from plReflectedClass
virtual const plRTTIGetDynamicRTTI () const
 
bool IsInstanceOf (const plRTTI *pType) const
 Returns whether the type of this instance is of the given type or derived from it.
 
template<typename T >
PL_ALWAYS_INLINE bool IsInstanceOf () const
 Returns whether the type of this instance is of the given type or derived from it.
 

Private Member Functions

virtual plResourceLoadDesc UnloadData (Unload WhatToUnload) override
 Requests the resource to unload another quality level. If bFullUnload is true, the resource should unload all data, because it is going to be deleted afterwards.
 
virtual plResourceLoadDesc UpdateContent (plStreamReader *Stream) override
 Called whenever more data for the resource is available. The resource must read the stream to update it's data.
 
virtual void UpdateMemoryUsage (MemoryUsage &out_NewMemoryUsage) override
 This function must be overridden by all resource types.
 

Additional Inherited Members

- Static Public Member Functions inherited from plNoBase
static const plRTTIGetStaticRTTI ()
 
- Public Attributes inherited from plResource
plEvent< const plResourceEvent &, plMutexm_ResourceEvents
 
- Protected Types inherited from plResource
enum class  DoUpdate { OnMainThread , OnAnyThread }
 
enum class  Unload { AllQualityLevels , OneQualityLevel }
 
- Protected Member Functions inherited from plResource
 plResource (DoUpdate ResourceUpdateThread, plUInt8 uiQualityLevelsLoadable)
 Default constructor.
 
virtual ~plResource ()
 virtual destructor.
 
MemoryUsageModifyMemoryUsage ()
 Non-const version for resources that want to write this variable directly.
 
void SetIsReloadable (bool bIsReloadable)
 Call this to specify whether a resource is reloadable.
 
void SetHasLoadingFallback (bool bHasLoadingFallback)
 Used internally by the code injection macros.
 

Detailed Description

An plCollectionResource is used to tell the engine about resources that it should preload in the background.

Collection resources can be used to improve the user experience by ensuring data is already (more likely) available when it is needed. For instance when a player walks into a longer corridor, a collection resource can be triggered to preload the data that will be needed when he reaches the door at the end of the corridor. In scenes this can be achieved using an plCollectionComponent.

Collection resources can also be used to query how much percent of the references resources are already loaded, which enables building progress bars or allowing functionality only after all necessary data is available.

Finally, collections can also be used to register resources with a nice name, such that it can be referenced in code by that name. This is useful if you want to reference some asset in code with a readable resource ID, and may want to change which resource is referenced at a later time. For example the nice name "Avatar" could point to some dummy mesh at the beginning of the project, and be updated to point to something else later, without the need to change the code.

Note
Once plCollectionResource::PreloadResources() has been triggered, the resource will hold a handle to all referenced resources, meaning those resources cannot get unloaded anymore. Make sure to discard the collection resource if the data it references should get freed up.

Member Function Documentation

◆ IsLoadingFinished()

bool plCollectionResource::IsLoadingFinished ( float * out_pProgress = nullptr) const

Returns true if all resources added for preloading via PreloadResources have finished loading. if out_progress is defined:

  • Assigns a value between 0.0 and 1.0 representing how many of the collection's resources are in a loaded state at the moment.
  • Always assigns 1.0 if all resources are in a loaded state.
  • Always assigns 1.0 if the collection contains no resources, or PreloadResources() was not triggered previously. Note: the progress can reach at maximum the fraction of resources that have been queued for preloading via PreloadResources. The progress will only reach 1.0 if all resources of this collection have been queued via PreloadResources and finished loading.

◆ PreloadResources()

bool plCollectionResource::PreloadResources ( plUInt32 uiNumResourcesToPreload = plMath::MaxValue<plUInt32>())

Puts up to the given number of resources for which a resource type could be found into the preload queue of the plResourceManager.

This has to be called manually. It will return false if no more resources can be queued for preloading. This can be used as a workflow where PreloadResources and IsLoadingFinished are called repeadedly in tandem, so only a smaller fraction of resources gets queued and waited for, to allow simple resource load-balancing.

◆ RegisterNames()

void plCollectionResource::RegisterNames ( )

Registers the named resources in the collection with the plResourceManager, such that they can be loaded by those names.

Note
This has to be called MANUALLY on collection resources, they do NOT do this automatically when loaded. Since resources are streamed, there is no guaranteed point in time when those names would be registered, which would introduce timing issues, where sometimes the name is registered when it is used and sometimes not. By calling this manually, the point in time where this is done is fixed and guaranteed.

Calling this twice has no effect.

◆ UnloadData()

plResourceLoadDesc plCollectionResource::UnloadData ( Unload WhatToUnload)
overrideprivatevirtual

Requests the resource to unload another quality level. If bFullUnload is true, the resource should unload all data, because it is going to be deleted afterwards.

Implements plResource.

◆ UnregisterNames()

void plCollectionResource::UnregisterNames ( )

Removes the registered names from the plResourceManager.

Calling this twice has no effect.

◆ UpdateContent()

plResourceLoadDesc plCollectionResource::UpdateContent ( plStreamReader * pStream)
overrideprivatevirtual

Called whenever more data for the resource is available. The resource must read the stream to update it's data.

pStream may be nullptr in case the resource data could not be found.

Implements plResource.

◆ UpdateMemoryUsage()

void plCollectionResource::UpdateMemoryUsage ( MemoryUsage & out_NewMemoryUsage)
overrideprivatevirtual

This function must be overridden by all resource types.

It has to compute the memory used by this resource. It is called by the resource manager whenever the resource's data has been loaded or unloaded.

Implements plResource.


The documentation for this class was generated from the following files: