Resource Usage – Arma Reforger

From Bohemia Interactive Community
Jump to navigation Jump to search
(Page creation)
 
m (Lou Montana moved page Arma Reforger:BaseContainer Usage to Arma Reforger:Resource Usage without leaving a redirect: Name standard)
(No difference)

Revision as of 17:45, 15 November 2023

A BaseContainer object is a data container obtained from a Resource object. Due to how resources are managed, some safeties are required.


Good Practice

Resources are managed by the engine, outside of script's reach. If the script loses reference to a Resource object, the engine may dispose of it instantly or at some point in the future, deleting also its related BaseContainer objects. To avoid this, always keep a reference to the parent Resource.

Examples

Bad Example Good Example
static BaseContainer GetBaseContainer(ResourceName resourceName) { // the only reference to said Resource is here and will be lost at the end of the scope Resource resource = Resource.Load(resourceName); if (!resource.IsValid()) return null; return resource.GetResource().ToBaseContainer(); // resource's reference is dropped here // the returned BaseContainer may become null at any time, // even with a script's reference }
static BaseContainer GetBaseContainer(notnull Resource resource) { if (!resource.IsValid()) return null; return resource.GetResource().ToBaseContainer(); // resource's reference is managed by the method caller }
// GetBaseContainer being the above method BaseContainer baseContainer = GetBaseContainer(m_sPrefab); // not OK string name = baseContainer.GetClassName(); // may result in a null pointer exception
Resource resource = Resource.Load(m_sPrefab); BaseContainer baseContainer = GetBaseContainer(resource); // OK string name = baseContainer.GetClassName(); // fine as long as reference to resource is kept
N/A
// this is fine - a Managed instance is created, resource can be dropped // simplified, from SCR_BaseContainerTools.CreateInstanceFromPrefab() static Managed CreateInstanceFromPrefab(ResourceName prefab) { Resource resource = Resource.Load(prefab); if (!resource.IsValid()) return null; BaseContainer baseContainer = resource.GetResource().ToBaseContainer(); if (!baseContainer) return null; return BaseContainerTools.CreateInstanceFromContainer(baseContainer); // resource reference is dropped but the created instance will remain }