Legacy:ObjectPool - Unreal Wiki

# Legacy:ObjectPool

### From Unreal Wiki, The Unreal Engine Documentation Site

UT2003 :: Object >> ObjectPool (Package: Engine)

The ObjectPool stores non-Actor objects that are no longer used to prevent needlessly instanciating objects and to reduce the amount of garbage.

## Usage

You can "allocate" an object (i.e. retrieve one from the ObjectPool if there is one of the same class in it already, or instantiate a new one) with the following code:

    MyTexRotator = Level.ObjectPool.AllocateObject(Class'TexRotator');  // allocates a TexRotator object

When you're done using the allocated object, put it back into the ObjectPool for others to use later:

    Level.ObjectPool.FreeObject(MyTexRotator);  // later calls to AllocateObject may return this TexRotator object

Note: Keep in mind that the object that's returned by AllocateObject may have its object properties set to something else than the default; maybe your code is not the first place this object instance is used in. Be sure to set all relevant properties of the allocated object the way you need them.

Warning on Material objects: It's indeed possible to allocate and use Material objects through ObjectPool, but many of them work only with one set of parameters within the same frame – i.e. you can't use the same TexRotator object to rotate and render a texture on a HUD, then rotate it a bit more and draw it at a different place on the same HUD within the same tick. You must use separate TexRotator objects to do that.

## Properties

array<Object> Objects
  Stores all unallocated objects.

## Methods

Object AllocateObject( class ObjectClass ) (simulated)
  Returns an object of the specified class. The object is taken from the Objects array or if no object of the specified class is found a new object is created. See Creating Actors and Objects.
FreeObject( Object Obj ) (simulated)
  Adds an object to the Objects array.
  Important: You have to make sure there are no more references to the object before using FreeObject.
Shrink() (simulated)
  Clears the Objects array. All objects without references are marked for garbage-collection. See Destroying Objects.

## Related Topics

- Creating Actors and Objects
- Destroying Objects
- Garbage Collection
- LevelInfo creates a global ObjectPool

Categories: Legacy Class (UT2003) | Legacy Class (UT2004)

##### Views

- Legacy
- Discussion
- Current revision

##### Search