Difference between revisions of "Precaching Assets"

From Valve Developer Community
Jump to: navigation, search
 
m (Layout)
Line 1: Line 1:
 
[[Category:Programming]]
 
[[Category:Programming]]
To provide a smooth loading of assets for a typical game session, it is important that the engine do as much work as possible to load those assets before the session begins.  By doing this, the player’s experience is not disturbed by interruptions as the engine tries to cache assets in the middle of a session, often causing ? or delays in rendering.  To do this, entities must declare and precache the assets they intend to use before the session starts.  There are multiple utility functions in place to achieve this.
+
To provide a smooth loading of assets for a typical game session, it is important that the engine do as much work as possible to load those assets before the session begins.  By doing this, the player’s experience is not disturbed by interruptions as the engine tries to cache assets in the middle of a session, often causing “hitching�? or delays in rendering.  To do this, entities must declare and precache the assets they intend to use before the session starts.  There are multiple utility functions in place to achieve this.
  
 
=Precache()=
 
=Precache()=
Line 7: Line 7:
  
 
Assets are precached using the following functions:
 
Assets are precached using the following functions:
{|
+
===PrecacheModel()===
| '''PrecacheModel()'''
+
Used to precache a model.
|-
 
| Used to precache a model.
 
 
<pre>
 
<pre>
 
int CBaseEntity::PrecacheModel(
 
int CBaseEntity::PrecacheModel(
Line 21: Line 19:
 
'''Return Value'''
 
'''Return Value'''
 
* Reference index for the model.
 
* Reference index for the model.
|-
+
 
| '''PrecacheScriptSound()'''
+
 
|-
+
===PrecacheScriptSound()===
| Used to precache a sound script as declared in a sound script file.
+
Used to precache a sound script as declared in a sound script file.
 
<pre>
 
<pre>
 
void CBaseEntity::PrecacheScriptSound(
 
void CBaseEntity::PrecacheScriptSound(
Line 37: Line 35:
 
'''Return Value'''
 
'''Return Value'''
 
* None.
 
* None.
|-
+
 
| '''PrecacheInstancedScene()'''
+
 
|-
+
===PrecacheInstancedScene()===
| Used to precache a VCD file.
+
Used to precache a VCD file.
 
<pre>
 
<pre>
 
void PrecacheInstancedScene(
 
void PrecacheInstancedScene(
Line 51: Line 49:
 
'''Return Value'''
 
'''Return Value'''
 
* None.
 
* None.
|-
+
 
| '''UTIL_PrecacheOther()'''
+
 
|-
+
===UTIL_PrecacheOther()===
| Utility function that will call the <code>Precache()</code> function for an entity that will be referenced or created during the lifetime of the calling entity.  This is often used for weapons that will create a child entity (like a grenade) dynamically during the course of a session.
+
Utility function that will call the <code>Precache()</code> function for an entity that will be referenced or created during the lifetime of the calling entity.  This is often used for weapons that will create a child entity (like a grenade) dynamically during the course of a session.
 
<pre>
 
<pre>
 
void UTIL_PrecacheOther(
 
void UTIL_PrecacheOther(
Line 68: Line 66:
 
'''Return Value'''
 
'''Return Value'''
 
* None.
 
* None.
|-
+
 
| '''UTIL_PrecacheDecal()'''
+
 
|-
+
===UTIL_PrecacheDecal()===
| Used to precache a decal.
+
Used to precache a decal.
 
<pre>
 
<pre>
 
int UTIL_PrecacheDecal(
 
int UTIL_PrecacheDecal(
Line 85: Line 83:
 
'''Return Value'''
 
'''Return Value'''
 
* Reference index for the decal model.
 
* Reference index for the decal model.
|}
 

Revision as of 06:14, 17 July 2006

To provide a smooth loading of assets for a typical game session, it is important that the engine do as much work as possible to load those assets before the session begins. By doing this, the player’s experience is not disturbed by interruptions as the engine tries to cache assets in the middle of a session, often causing “hitching�? or delays in rendering. To do this, entities must declare and precache the assets they intend to use before the session starts. There are multiple utility functions in place to achieve this.

Precache()

The Precache() function is available to all descendants of the CBaseEntity class and must be called in the Spawn() function of each entity. All assets used by the entity (models, sounds, VCD, decals) must be declared in this function to be used. Assets may not be precached outside of this function.

Assets are precached using the following functions:

PrecacheModel()

Used to precache a model.

int CBaseEntity::PrecacheModel(
     const char *name
)

Parameters

  • name
    • Filename of the model to load

Return Value

  • Reference index for the model.


PrecacheScriptSound()

Used to precache a sound script as declared in a sound script file.

void CBaseEntity::PrecacheScriptSound(
     const char *soundname
)

Parameters

  • soundname
    • Name of sound script to precache.

Remarks

  • Sound script must be declared in the sound script manifest document.

Return Value

  • None.


PrecacheInstancedScene()

Used to precache a VCD file.

void PrecacheInstancedScene(
     char const *pszScene
)

Parameters

  • pszScene
    • Filename of the VCD to precache.

Return Value

  • None.


UTIL_PrecacheOther()

Utility function that will call the Precache() function for an entity that will be referenced or created during the lifetime of the calling entity. This is often used for weapons that will create a child entity (like a grenade) dynamically during the course of a session.

void UTIL_PrecacheOther(
     const char *szClassname,
     const char *modelName
)

Parameters

  • szClassname
    • Entity classname to precache assets for.
  • modelName
    • Model name to use for this entity.

Return Value

  • None.


UTIL_PrecacheDecal()

Used to precache a decal.

int UTIL_PrecacheDecal(
     const char *name,
     bool preload
)

Parameters

  • name
    • Name of the decal to precache.
  • preload
    • Whether or not to preload this decal.

Return Value

  • Reference index for the decal model.