Last active
October 15, 2021 03:50
-
-
Save TheDutchDev/89df8f3c279889df2f3383c47a1fb4c6 to your computer and use it in GitHub Desktop.
alt:V MP Async model loading with cancellation and clearing of previously loaded models that are no longer being used.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as alt from 'alt-client'; | |
import * as natives from 'natives'; | |
class AsyncModel { | |
constructor() { | |
this.loadingModels = new Set(); | |
this.loadedModels = new Map(); | |
} | |
cancel(model) { | |
this.loadingModels.delete(model); | |
this.removeLoadedModel(model); | |
} | |
async load(model) { | |
return new Promise(resolve => { | |
const hash = alt.hash(model); | |
if (natives.hasModelLoaded(hash) || this.loadingModels.has(model)) { | |
return resolve(true); | |
} | |
this.loadingModels.add(model); | |
natives.requestModel(hash); | |
const interval = alt.setInterval(() => { | |
if (!this.loadingModels.has(model)) { | |
return done(natives.hasModelLoaded(hash)); | |
} | |
if (natives.hasModelLoaded(hash)) { | |
return done(true); | |
} | |
}, 25); | |
const timeout = alt.setTimeout(() => { | |
return done(natives.hasModelLoaded(hash)); | |
}, 3000); | |
const done = result => { | |
alt.clearInterval(interval); | |
alt.clearTimeout(timeout); | |
if (this.loadingModels.has(model)) | |
this.addLoadedModel(model); | |
this.loadingModels.delete(model); | |
return resolve(result); | |
}; | |
}); | |
} | |
addLoadedModel(model) { | |
let counter = 0; | |
if (this.loadedModels.has(model)) | |
counter = this.loadedModels.get(model); | |
this.loadedModels.set(model, ++counter); | |
} | |
removeLoadedModel(model) { | |
const hash = alt.hash(model); | |
if (!this.loadedModels.has(model)) | |
natives.setModelAsNoLongerNeeded(hash); | |
let count = this.loadedModels.get(model); | |
count--; | |
if (count > 0) { | |
this.loadedModels.set(model, count); | |
return; | |
} | |
natives.setModelAsNoLongerNeeded(hash); | |
this.loadedModels.delete(model); | |
} | |
unloadAllModels() { | |
for (let model in this.loadedModels) { | |
natives.setModelAsNoLongerNeeded(alt.hash(model)); | |
} | |
this.loadedModels.clear( ); // Thanks Kar! | |
} | |
} | |
export const asyncModel = new AsyncModel(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { asyncModel } from "./async-models"; | |
// Other code... | |
await asyncModel.load( "v_ilev_carmod3door" ); // EXPECTS MODEL STRING, NOT HASH! | |
// Spawn here with eg createObjectNoOffset | |
asyncModel.cancel( "v_ilev_carmod3door" ); // cancel loading of a model. | |
asyncModel.removeLoadedModel( "v_ilev_carmod3door" ); // unload model when eg object gets deleted. | |
asyncModel.unloadAllModels( ); // Unload all models (eg when stopping resource). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment