The three virtues of a programmer: Laziness, Impatience, and Hubris. – Larry Wall

Difference between revisions of "UE2:ObjectPool (UT2004)"

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search
(Auto-generated page)
 
(added descriptions and usage instructions)
 
Line 3: Line 3:
 
| parent1 = Object
 
| parent1 = Object
 
}}
 
}}
{{autogenerated}}
+
Provides a pool for unused non-actor objects that can be reused later.
null
+
 
 +
==Usage==
 +
The object pool is always available to any actors via <code>Level.ObjectPool</code>. Instead of creating new non-actor objects with the [[new]] operator, you call <code>Level.ObjectPool.AllocateObject()</code> with the desired object class. You may have to [[typecast]] the result to the requested class. It is a good idea to explicitly initialize all relevant properties of the allocated object as it may have been used before and properties might not have their default values anymore. Provided all other object pool users play by the rules, the returned object will not be referenced or used by anything else, so apart from explicit initialization you can use it like any other object.
 +
 
 +
When you no longer need an object, whether you allocated it from the pool or not, you can release it into the pool by calling <code>Level.ObjectPool.FreeObject()</code>. Make sure that the object is no longer referenced by any of your code afterwards. If the object contains sensible information or references other objects, it's a good idea to clear the relevant variables before releasing the object.
  
 
==Properties==
 
==Properties==
Line 10: Line 14:
 
'''Type:''' [[array]]<{{cl|Object}}>
 
'''Type:''' [[array]]<{{cl|Object}}>
  
<!-- enter variable description -->
+
The actual object pool.
  
 
==Instance functions==
 
==Instance functions==
Line 16: Line 20:
 
{{code|simulated function {{cl|Object}}&nbsp;'''AllocateObject''' ([[Types#Class|Class]]&nbsp;'''ObjectClass''')}}
 
{{code|simulated function {{cl|Object}}&nbsp;'''AllocateObject''' ([[Types#Class|Class]]&nbsp;'''ObjectClass''')}}
  
<!-- enter function description -->
+
Returns an object of the specified class from the pool. If the pool did not contain any object of that class, a new instance of the specified class is created and returned.
  
 
====FreeObject====
 
====FreeObject====
 
{{code|simulated function '''FreeObject''' ({{cl|Object}}&nbsp;'''Obj''')}}
 
{{code|simulated function '''FreeObject''' ({{cl|Object}}&nbsp;'''Obj''')}}
  
<!-- enter function description -->
+
Puts the specified object into the pool. Make sure the object is no longer referenced by anything else when releasing it into the pool. Keep in mind that the object is not explicitly reset when it is added to or allocated from the pool. Any data the object contained when it was added will still be there when it is allocated again.
  
 
====Shrink====
 
====Shrink====
 
{{code|simulated function '''Shrink''' ()}}
 
{{code|simulated function '''Shrink''' ()}}
  
<!-- enter function description -->
+
Empties the object pool. Provided the objects are not referenced anywhere else, they are subject to garbage collection now.

Latest revision as of 07:45, 25 October 2009

UT2004 Object >> ObjectPool
Package: 
Engine
This class in other games:
UE2Runtime, UT2003, U2, U2XMP

Provides a pool for unused non-actor objects that can be reused later.

Usage[edit]

The object pool is always available to any actors via Level.ObjectPool. Instead of creating new non-actor objects with the new operator, you call Level.ObjectPool.AllocateObject() with the desired object class. You may have to typecast the result to the requested class. It is a good idea to explicitly initialize all relevant properties of the allocated object as it may have been used before and properties might not have their default values anymore. Provided all other object pool users play by the rules, the returned object will not be referenced or used by anything else, so apart from explicit initialization you can use it like any other object.

When you no longer need an object, whether you allocated it from the pool or not, you can release it into the pool by calling Level.ObjectPool.FreeObject(). Make sure that the object is no longer referenced by any of your code afterwards. If the object contains sensible information or references other objects, it's a good idea to clear the relevant variables before releasing the object.

Properties[edit]

Objects[edit]

Type: array<Object>

The actual object pool.

Instance functions[edit]

AllocateObject[edit]

simulated function Object AllocateObject (Class ObjectClass)

Returns an object of the specified class from the pool. If the pool did not contain any object of that class, a new instance of the specified class is created and returned.

FreeObject[edit]

simulated function FreeObject (Object Obj)

Puts the specified object into the pool. Make sure the object is no longer referenced by anything else when releasing it into the pool. Keep in mind that the object is not explicitly reset when it is added to or allocated from the pool. Any data the object contained when it was added will still be there when it is allocated again.

Shrink[edit]

simulated function Shrink ()

Empties the object pool. Provided the objects are not referenced anywhere else, they are subject to garbage collection now.