alt.loadModel
Used to load a model based on hash. Best when used in tandem with alt.hash
.
Declaration
alt.loadModel(modelHash: number): void
Usage
alt.loadModel(someModelHash);
Real World Example
const someModelHash = alt.hash('cheetah');
alt.loadModel(hash);
You should definitely use async for loading models and spawning objects, you want to ensure that the model is loaded before creating the object
Alternative Implementation
Personally, I have had issues with use alt.loadModel so I've opted for using natives for loading models. Here's a raw implementation using natives.
export async function loadModel(hash: number): Promise<boolean> {
return await new Promise((resolve: Function) => {
native.requestModel(hash);
let count = 0;
if (native.hasModelLoaded(hash)) {
resolve(true);
return;
}
const interval = alt.setInterval(() => {
if (count >= 100) {
resolve(false);
alt.clearInterval(interval);
return;
}
if (!native.hasModelLoaded(hash)) {
count += 1;
return;
}
alt.clearInterval(interval);
resolve(true);
}, 100);
});
}
These examples assume you have imported alt
from alt-client
.