[Base Class](../groups/Extension.Base Class.md) / ObjPool
ObjPool<T> Class
Universal object pool, which can be used for reuse of various types of objects
Example usage: Create a script called ObjPoolExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, press the F key to generate a block based on the existing blocks in the object pool at the player's current position, and recycle the coordinates back to the origin after 5 seconds. Frequent F presses will prompt the client log that there are no objects in the object pool, and press the G key to destroy all blocks in the recycling state
@Component
export default class ObjPoolExample extends Script {
private objPool: ObjPool<Cube>;
protected onStart(): void {
if (!SystemUtil.isClient()) return;
const cubeAssetId = "197386";
AssetUtil.asyncDownloadAsset(cubeAssetId).then(() => {
//Initialize an object pool of 5 objects
this.objPool = new ObjPool<Cube>(
this.onCubeCreate,
this.onCubeReset,
this.onCubeDestroy,
this.onCubeDespawn,
5
);
InputUtil.onKeyDown(Keys.F, () => {
let size = this.objPool.size;
if (size <= 0) {
Console.log ("There are no objects in the object pool");
return;
}
let cube = this.objPool.spawn();
setTimeout(() => {
//Recycle the block in 5 seconds
this.objPool.despawn(cube);
cube.obj.worldTransform.position = new Vector(0, 0, 0);
}, 5000);
});
InputUtil.onKeyDown(Keys.G, () => {
//Destroy all reclaimed objects in the object pool
this.objPool.clear();
})
});
}
//Create callback for new object
private onCubeCreate(): Cube {
let cube = new Cube();
cube.obj.setCollision(CollisionStatus.Off);
cube.obj.worldTransform.position = new Vector(0, 0, 0);
return cube;
}
//Reset object callback
private onCubeReset(cube: Cube): void {
let playerPos = mw.getCurrentPlayer().character.worldTransform.position;
cube.obj.worldTransform.position = playerPos;
}
//Callback for destroying objects
private onCubeDestroy(cube: Cube): void {
cube.obj.destroy();
cube.obj = null;
}
//Callback for returning objects
private onCubeDespawn(cube: Cube): void {
}
}
class Cube {
public obj: mw.GameObject = null;
constructor() {
this.obj = mw.GameObject.spawn("197386");
}
}
@Component
export default class ObjPoolExample extends Script {
private objPool: ObjPool<Cube>;
protected onStart(): void {
if (!SystemUtil.isClient()) return;
const cubeAssetId = "197386";
AssetUtil.asyncDownloadAsset(cubeAssetId).then(() => {
//Initialize an object pool of 5 objects
this.objPool = new ObjPool<Cube>(
this.onCubeCreate,
this.onCubeReset,
this.onCubeDestroy,
this.onCubeDespawn,
5
);
InputUtil.onKeyDown(Keys.F, () => {
let size = this.objPool.size;
if (size <= 0) {
Console.log ("There are no objects in the object pool");
return;
}
let cube = this.objPool.spawn();
setTimeout(() => {
//Recycle the block in 5 seconds
this.objPool.despawn(cube);
cube.obj.worldTransform.position = new Vector(0, 0, 0);
}, 5000);
});
InputUtil.onKeyDown(Keys.G, () => {
//Destroy all reclaimed objects in the object pool
this.objPool.clear();
})
});
}
//Create callback for new object
private onCubeCreate(): Cube {
let cube = new Cube();
cube.obj.setCollision(CollisionStatus.Off);
cube.obj.worldTransform.position = new Vector(0, 0, 0);
return cube;
}
//Reset object callback
private onCubeReset(cube: Cube): void {
let playerPos = mw.getCurrentPlayer().character.worldTransform.position;
cube.obj.worldTransform.position = playerPos;
}
//Callback for destroying objects
private onCubeDestroy(cube: Cube): void {
cube.obj.destroy();
cube.obj = null;
}
//Callback for returning objects
private onCubeDespawn(cube: Cube): void {
}
}
class Cube {
public obj: mw.GameObject = null;
constructor() {
this.obj = mw.GameObject.spawn("197386");
}
}
Type parameters
Name |
---|
T |
Table of contents
Accessors
size(): number other |
---|
Get the number of free objects in the object pool |
Methods
clear(): void other |
---|
Clear objects from the pool |
despawn(obj : T ): void other |
Return an object |
spawn(): T other |
Generate an object |
Construct an object pool
Type parameters
Name |
---|
T |
Parameters
onCreateObj () => T | Callback for creating a new object |
---|---|
onReset? (obj : T ) => void | Reset object callback default: null |
onDestroy? (obj : T ) => void | Cancel object callback default: null |
onDespawn? (obj : T ) => void | Return object callback default: null |
initNum? number | The number of default cache objects default: 2 range: unlimited type: integer |
Accessors
size
• |
---|
Get the number of free objects in the object pool Returns |
number |
---|
Methods
clear
• clear(): void
other
Clear objects from the pool
despawn
• despawn(obj
): void
other
Return an object
Parameters
obj T | object |
---|
spawn
• spawn(): T
other
Generate an object
Returns
T | object |
---|