Prefab Data – Arma Reforger

From Bohemia Interactive Community
Jump to navigation Jump to search
(Page creation)
 
m (Fix)
 
Line 1: Line 1:
A '''Prefab Data''' is an object storing variables which are shared among all of the instances of the given prefab.
A '''Prefab Data''' is an object storing variables which are shared among all of the instances of the given prefab.


{{Feature|informative|This concept works for both entities and components, where we then talk about '''Component Data''' and use the <enfusion inline>GetComponentData()</enfusion> method instead of the <enfusion inline>GetPrefabData()</enfusion> one.}}
{{Feature|informative|This concept works for both entities and components, where we then talk about '''Component Data''' and use the <enforce inline>GetComponentData()</enforce> method instead of the <enforce inline>GetPrefabData()</enforce> one.}}


* Example 1: set a value that works for all instances of this Prefab (e.g max health, fuel capacity, etc.)
* Example 1: set a value that works for all instances of this Prefab (e.g max health, fuel capacity, etc.)
Line 13: Line 13:
** Are there going to be 10 instances of this object? If so, this is probably not worth it, unless it has a 100 variables which can be shared.
** Are there going to be 10 instances of this object? If so, this is probably not worth it, unless it has a 100 variables which can be shared.
** Are there going to be 100+ instances of this object with multiple variables which can be shared? Totally worth it to use prefab data.
** Are there going to be 100+ instances of this object with multiple variables which can be shared? Totally worth it to use prefab data.
* How often do I access these variables, do I need these values every frame? If so, maybe then the performance consumption of getting & casting prefab data is not worth it.
* How often do I access these variables, do I need these values every frame? If so, maybe then the performance consumption of getting and casting prefab data is not worth it.
** I need these variables only for occasional calculations happening on, for example, collisions, user clicking a button or on periodic check. No worries here, for the performance difference is not noticeable
** I need these variables only for occasional calculations happening on, for example, collisions, user clicking a button or on periodic check. No worries here, for the performance difference is not noticeable
** I need these variables each frame on each of my 30 instances. In such case consider storing the prefab data as a member variable (8 bytes for pointer), but only if more memory than that is saved by moving other member variables there - moving 3 and more (non-bool) variables should be enough to compensate for it.
** I need these variables each frame on each of my 30 instances. In such case consider storing the prefab data as a member variable (8 bytes for pointer), but only if more memory than that is saved by moving other member variables there - moving 3 and more (non-bool) variables should be enough to compensate for it.
Line 24: Line 24:
All that is needed is to put the variable in {{hl|SCR_MyEntity'''Class'''}} insted of {{hl|SCR_MyEntity}}.
All that is needed is to put the variable in {{hl|SCR_MyEntity'''Class'''}} insted of {{hl|SCR_MyEntity}}.


*Class classes are required for all Entities and Components, so no more preparation is required.
<nowiki/>{{hl|*'''Class'''}} classes are required for all Entities and Components, so no more preparation is required.


{| class="wikitable valign-top"
{| class="wikitable valign-top"
Line 42: Line 42:
class SCR_AIGroup : ChimeraAIGroup
class SCR_AIGroup : ChimeraAIGroup
{
{
[Attribute("420")]
[Attribute("42")]
protected int m_iValue;
protected int m_iValue;
Line 56: Line 56:
class SCR_AIGroupClass : ChimeraAIGroupClass
class SCR_AIGroupClass : ChimeraAIGroupClass
{
{
[Attribute("420")]
[Attribute("42")]
int m_iValue;
int m_iValue;
}
}

Latest revision as of 00:36, 31 March 2024

A Prefab Data is an object storing variables which are shared among all of the instances of the given prefab.

This concept works for both entities and components, where we then talk about Component Data and use the GetComponentData() method instead of the GetPrefabData() one.
  • Example 1: set a value that works for all instances of this Prefab (e.g max health, fuel capacity, etc.)
  • Example 2: precalculate a value to not set it manually. The calculation will happen within a script callback that instanciates the variable when the first Prefab instance is created and store the value in the Prefab Data. e.g kinetic energy in a given velocity: Parameter velocity is 40 km/h, another parameter is object's mass, 1050 kg. These two variables allow to precalculate the kinetic energy threshold above which damage will be dealt. This result is the same for all the Prefab instances.


Requirements

Before moving instance variables to prefab data, please consider the following:

  • How many instances of the object (entity/component) will there be in the map and will the saved memory be worth the increased performance consumption?
    • Are there going to be 10 instances of this object? If so, this is probably not worth it, unless it has a 100 variables which can be shared.
    • Are there going to be 100+ instances of this object with multiple variables which can be shared? Totally worth it to use prefab data.
  • How often do I access these variables, do I need these values every frame? If so, maybe then the performance consumption of getting and casting prefab data is not worth it.
    • I need these variables only for occasional calculations happening on, for example, collisions, user clicking a button or on periodic check. No worries here, for the performance difference is not noticeable
    • I need these variables each frame on each of my 30 instances. In such case consider storing the prefab data as a member variable (8 bytes for pointer), but only if more memory than that is saved by moving other member variables there - moving 3 and more (non-bool) variables should be enough to compensate for it.


Code Example

Making a prefab data stored variable from a member variable is really easy.

All that is needed is to put the variable in SCR_MyEntityClass insted of SCR_MyEntity.

*Class classes are required for all Entities and Components, so no more preparation is required.

Before After

// prefab data class class SCR_AIGroupClass : ChimeraAIGroupClass { } // regular functional class class SCR_AIGroup : ChimeraAIGroup { [Attribute("42")] protected int m_iValue; int GetValue() { return m_iValue; } }

// prefab data class class SCR_AIGroupClass : ChimeraAIGroupClass { [Attribute("42")] int m_iValue; } // regular functional class class SCR_AIGroup : ChimeraAIGroup { int GetValue() { SCR_AIGroupClass prefabData = SCR_AIGroupClass.Cast(GetPrefabData()); if (!prefabData) return -1; // assuming -1 is an invalid value return prefabData.m_iValue; } }