Skip to content

Instantly share code, notes, and snippets.

@root-cause
Created September 22, 2024 16:48
Show Gist options
  • Save root-cause/e99af44eedb3d3d68d63c8d421ccb0e4 to your computer and use it in GitHub Desktop.
Save root-cause/e99af44eedb3d3d68d63c8d421ccb0e4 to your computer and use it in GitHub Desktop.
function searchSpawnPointsAsync(options) {
const { position, radius, maxHeightDifference = 5.0, flags = 0, minDistanceBetweenPoints = 0.0, timeout = 0 } = options;
return new Promise((resolve, reject) => {
// cancel the active search if there is one
if (mp.game.ped.spawnpointsIsSearchActive()) {
mp.game.ped.spawnpointsCancelSearch();
}
// start new search
mp.game.ped.spawnpointsStartSearch(position.x, position.y, position.z, radius, maxHeightDifference, flags, minDistanceBetweenPoints, timeout);
const timer = setInterval(() => {
if (!mp.game.ped.spawnpointsIsSearchActive()) {
clearInterval(timer);
reject("cancelled");
} else if (mp.game.ped.spawnpointsIsSearchFailed()) {
clearInterval(timer);
mp.game.ped.spawnpointsCancelSearch();
reject("timeout");
} else if (mp.game.ped.spawnpointsIsSearchComplete()) {
clearInterval(timer);
const numResults = mp.game.ped.spawnpointsGetNumSearchResults();
const results = new Array(numResults);
for (let i = 0; i < numResults; i++) {
const result = mp.game.ped.spawnpointsGetSearchResult(i);
results[i] = {
...result,
flags: mp.game.ped.spawnpointsGetSearchResultFlags(i)
};
}
mp.game.ped.spawnpointsCancelSearch();
resolve(results);
}
}, 100);
});
}
mp.keys.bind(0x75 /* F6 */, false, async () => {
try {
const results = await searchSpawnPointsAsync({
position: mp.players.local.position,
radius: 20.0,
minDistanceBetweenPoints: 3.0,
flags: 2 | 16
});
for (const result of results) {
mp.markers.new(1, new mp.Vector3(result.x, result.y, result.z), 1.0);
}
mp.gui.chat.push(`Found ${results.length} points`);
} catch (err) {
mp.gui.chat.push(`Search failed: ${err}`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment