Skip to content
GameObjPool

[Base Class](../groups/Extension.Base Class.md) / GameObjPool

GameObjPool Class ​

The object pool used to cache GameObjects is suitable for the reuse caching of asset, scene objects, and prefabricated objects in the asset library

Example usage: Create a script called GameObjPoolExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, a block will be generated at the origin and disappear after 5 seconds

ts
@Component
export default class GameObjPoolExample extends mw.Script {

    protected onStart(): void {
        this.createCube();
    }

    //Create a block dynamically through the object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        GameObjPool.asyncSpawn(cubeAssetId, GameObjPoolSourceType.Asset).then(obj => {
            obj.worldTransform.position = new Vector(0, 0, 0);
            setTimeout(() => {
                //Recycle the block in 5 seconds
                GameObjPool.despawn(obj);
            }, 5000);
        });
    }
}
@Component
export default class GameObjPoolExample extends mw.Script {

    protected onStart(): void {
        this.createCube();
    }

    //Create a block dynamically through the object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        GameObjPool.asyncSpawn(cubeAssetId, GameObjPoolSourceType.Asset).then(obj => {
            obj.worldTransform.position = new Vector(0, 0, 0);
            setTimeout(() => {
                //Recycle the block in 5 seconds
                GameObjPool.despawn(obj);
            }, 5000);
        });
    }
}

Table of contents ​

Methods ​

destroy(): void other
Destroy the global instance of the object pool
asyncSpawn<T: extends GameObject<T>>(guid: string, type?: GameObjPoolSourceType): Promise<T: extends GameObject<T>> other
Asynchronously generate an object
clear(guid: string, type?: GameObjPoolSourceType): void other
Clear all objects corresponding to the UID in the object pool
clearAll(): void other
Clear all objects from the object pool
despawn(obj: GameObject): void other
Return an object
spawn<T: extends GameObject<T>>(guid: string, type?: GameObjPoolSourceType): T: extends GameObject<T> other
Generate an object

Methods ​

destroy ​

• destroy(): void other

Destroy the global instance of the object pool


asyncSpawn ​

• Static asyncSpawn<T>(guid, type?): Promise<T> other

Asynchronously generate an object

Parameters ​

guid stringAsset GUID range: depends on the length of asset ID
type? GameObjPoolSourceTypeAsset type default: asset in asset library

Returns ​

Promise<T>Generated object

Precautions

Note that the original asset need to be preloaded

Example usage: Create a script called GameObjPoolExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, a block will be generated at the origin and disappear after 5 seconds

ts
@Component
export default class GameObjPoolExample extends Script {

    protected onStart(): void {
        this.createCube();
    }

    //Create a block dynamically through the object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        GameObjPool.asyncSpawn(cubeAssetId, GameObjPoolSourceType.Asset).then(obj => {
            obj.worldTransform.position = new Vector(0, 0, 0);
            setTimeout(() => {
                //Recycle the block in 5 seconds
                GameObjPool.despawn(obj);
            }, 5000);
        });
    }
}
@Component
export default class GameObjPoolExample extends Script {

    protected onStart(): void {
        this.createCube();
    }

    //Create a block dynamically through the object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        GameObjPool.asyncSpawn(cubeAssetId, GameObjPoolSourceType.Asset).then(obj => {
            obj.worldTransform.position = new Vector(0, 0, 0);
            setTimeout(() => {
                //Recycle the block in 5 seconds
                GameObjPool.despawn(obj);
            }, 5000);
        });
    }
}

Type parameters ​

Textends GameObject<T>

clear ​

• Static clear(guid, type?): void other

Clear all objects corresponding to the UID in the object pool

Parameters ​

guid stringAsset GUID range: depends on the length of asset ID
type? GameObjPoolSourceTypeAsset type default: asset in asset library

Usage example: create a script named GameObjPoolExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and generate 10 blocks, each with a different position, and all of them will be destroyed after 5 seconds

ts
@Component
export default class GameObjPoolExample extends Script {

    protected onStart(): void {
        if (SystemUtil.isClient()) {
            this.createCube();
        }
    }

    //Dynamically create multiple blocks through an object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        //Create 10 blocks with non overlapping positions
        for (let i = 0;
i < 10;
i++) {
            GameObjPool.asyncSpawn(cubeAssetId, GameObjPoolSourceType.Asset).then(obj => {
                obj.worldLocation = new Vector(i * 300, 0, 0);
                //Retrieve the object but do not hide it
                GameObjPool.despawn(obj);
                obj.worldLocation = new Vector(i * 300, 0, 0);
                obj.setVisibility(PropertyStatus.On);
            });
        }
        setTimeout(() => {
            //Destroy the recycled objects created through cubeAssetId in the object pool
            GameObjPool.clear(cubeAssetId);
        }, 5000);
    }
}
@Component
export default class GameObjPoolExample extends Script {

    protected onStart(): void {
        if (SystemUtil.isClient()) {
            this.createCube();
        }
    }

    //Dynamically create multiple blocks through an object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        //Create 10 blocks with non overlapping positions
        for (let i = 0;
i < 10;
i++) {
            GameObjPool.asyncSpawn(cubeAssetId, GameObjPoolSourceType.Asset).then(obj => {
                obj.worldLocation = new Vector(i * 300, 0, 0);
                //Retrieve the object but do not hide it
                GameObjPool.despawn(obj);
                obj.worldLocation = new Vector(i * 300, 0, 0);
                obj.setVisibility(PropertyStatus.On);
            });
        }
        setTimeout(() => {
            //Destroy the recycled objects created through cubeAssetId in the object pool
            GameObjPool.clear(cubeAssetId);
        }, 5000);
    }
}

clearAll ​

• Static clearAll(): void other

Clear all objects from the object pool

Usage example: create a script named GameObjPoolExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, and generate 10 squares and 10 spheres. Each square and sphere has a different position, and half of them will be destroyed after 5 seconds

ts
@Component
export default class GameObjPoolExample extends Script {

    protected onStart(): void {
        if (SystemUtil.isClient()) {
            this.createCube();
        }
    }

    //Create and destroy multiple different objects dynamically through object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        const cubeAssetId2 = "7675";
        //Create 10 blocks with non overlapping positions
        for (let i = 0;
i < 10;
i++) {
            GameObjPool.asyncSpawn(cubeAssetId, GameObjPoolSourceType.Asset).then(obj => {
                obj.worldLocation = new Vector(i * 300, 0, 0);
                //Only recycle the first 5 blocks
                if (i <= 5) return;
                //Retrieve the object but do not hide it
                GameObjPool.despawn(obj);
                obj.worldLocation = new Vector(i * 300, 0, 0);
                obj.setVisibility(PropertyStatus.On);
            });
        }
        //Create 10 spheres without overlapping position
        for (let i = 0;
i < 10;
i++) {
            GameObjPool.asyncSpawn(cubeAssetId2, GameObjPoolSourceType.Asset).then(obj => {
                obj.worldLocation = new Vector(i * 300, 300, 0);
                //Only recycle the first 5 spheres
                if (i <= 5) return;
                //Retrieve the object but do not hide it
                GameObjPool.despawn(obj);
                obj.worldLocation = new Vector(i * 300, 300, 0);
                obj.setVisibility(PropertyStatus.On);
            });
        }
        setTimeout(() => {
            //Destroy all reclaimed objects in the object pool
            GameObjPool.clearAll();
        }, 5000);
    }
}
@Component
export default class GameObjPoolExample extends Script {

    protected onStart(): void {
        if (SystemUtil.isClient()) {
            this.createCube();
        }
    }

    //Create and destroy multiple different objects dynamically through object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        const cubeAssetId2 = "7675";
        //Create 10 blocks with non overlapping positions
        for (let i = 0;
i < 10;
i++) {
            GameObjPool.asyncSpawn(cubeAssetId, GameObjPoolSourceType.Asset).then(obj => {
                obj.worldLocation = new Vector(i * 300, 0, 0);
                //Only recycle the first 5 blocks
                if (i <= 5) return;
                //Retrieve the object but do not hide it
                GameObjPool.despawn(obj);
                obj.worldLocation = new Vector(i * 300, 0, 0);
                obj.setVisibility(PropertyStatus.On);
            });
        }
        //Create 10 spheres without overlapping position
        for (let i = 0;
i < 10;
i++) {
            GameObjPool.asyncSpawn(cubeAssetId2, GameObjPoolSourceType.Asset).then(obj => {
                obj.worldLocation = new Vector(i * 300, 300, 0);
                //Only recycle the first 5 spheres
                if (i <= 5) return;
                //Retrieve the object but do not hide it
                GameObjPool.despawn(obj);
                obj.worldLocation = new Vector(i * 300, 300, 0);
                obj.setVisibility(PropertyStatus.On);
            });
        }
        setTimeout(() => {
            //Destroy all reclaimed objects in the object pool
            GameObjPool.clearAll();
        }, 5000);
    }
}

despawn ​

• Static despawn(obj): void other

Return an object

Parameters ​

obj GameObjectThe object to be returned

Example usage: Create a script called GameObjPoolExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, a block will be generated at the origin and disappear after 5 seconds

ts
@Component
export default class GameObjPoolExample extends Script {

    protected onStart(): void {
        this.createCube();
    }

    //Create a block dynamically through the object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        GameObjPool.asyncSpawn(cubeAssetId, GameObjPoolSourceType.Asset).then(obj => {
            obj.worldTransform.position = new Vector(0, 0, 0);
            setTimeout(() => {
                //Recycle the block in 5 seconds
                GameObjPool.despawn(obj);
            }, 5000);
        });
    }
}
@Component
export default class GameObjPoolExample extends Script {

    protected onStart(): void {
        this.createCube();
    }

    //Create a block dynamically through the object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        GameObjPool.asyncSpawn(cubeAssetId, GameObjPoolSourceType.Asset).then(obj => {
            obj.worldTransform.position = new Vector(0, 0, 0);
            setTimeout(() => {
                //Recycle the block in 5 seconds
                GameObjPool.despawn(obj);
            }, 5000);
        });
    }
}

spawn ​

• Static spawn<T>(guid, type?): T other

Generate an object

Parameters ​

guid stringAsset GUID range: depends on the length of asset ID
type? GameObjPoolSourceTypeAsset type default: asset in asset library

Returns ​

TGenerated object

Usage example: create a script named GameObjPoolExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and a box will be generated at the origin

ts
@Component
export default class GameObjPoolExample extends Script {

    protected onStart(): void {
        this.createCube();
    }

    //Create a block dynamically through the object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        AssetUtil.asyncDownloadAsset(cubeAssetId).then(() => {
            let obj = GameObjPool.spawn(cubeAssetId);
            obj.worldTransform.position = new Vector(0, 0, 0);
        });
    }
}
@Component
export default class GameObjPoolExample extends Script {

    protected onStart(): void {
        this.createCube();
    }

    //Create a block dynamically through the object pool
    public createCube(): void {
        const cubeAssetId = "197386";
        AssetUtil.asyncDownloadAsset(cubeAssetId).then(() => {
            let obj = GameObjPool.spawn(cubeAssetId);
            obj.worldTransform.position = new Vector(0, 0, 0);
        });
    }
}

Type parameters ​

Textends GameObject<T>