Skip to content

Commit

Permalink
refactor(vehicle): more vehicleProperties debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Jul 14, 2024
1 parent 3a46104 commit 2edb79e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
36 changes: 23 additions & 13 deletions client/vehicle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,43 @@ AddStateBagChangeHandler('initVehicle', '', async (bagName: string, key: string,
AddStateBagChangeHandler('vehicleProperties', '', async (bagName: string, key: string, value: any) => {
if (!value) return DEBUG && console.info(`removed ${key} state from ${bagName}`);

const entity = await waitFor(async () => {
const entity = GetEntityFromStateBagName(bagName);
DEV: console.info(key, entity);
const entity = await waitFor(
async () => {
const entity = GetEntityFromStateBagName(bagName);
DEV: console.info(key, entity);

if (entity) return entity;
}, 'failed to get entity from statebag name');
if (entity) return entity;
},
'failed to get entity from statebag name',
10000
);

if (!entity) return;

// properties sometimes fail to set for whatever reason, so here's a dumb workaround.
const resolved = await new Promise((resolve) => {
const status = await new Promise((resolve) => {
let i = 0;
let interval: CitizenTimer;

interval = setInterval(() => {
i++;
const doesEntityExist = DoesEntityExist(entity);

if (i % 5) console.info(`Attempting to set ${bagName} on entity ${entity} (${i})`);

if (i > 5 || !DoesEntityExist(entity)) {
resolve(i > 5 ? 1 : 0);
if (i > 100 || !doesEntityExist || !Entity(entity).state[key]) {
resolve(doesEntityExist ? 0 : i > 100 ? 1 : 2);
return clearInterval(interval);
}

setVehicleProperties(entity, value);
}, 100);
}, 200);
});

if (!resolved) return;

Entity(entity).state.set(key, null, true);
console.info(
status
? status === 1
? `Failed to set ${bagName} on entity ${entity} (timed out)`
: `Set ${bagName} on entity ${entity}`
: `Failed to set ${bagName} on entity ${entity} (entity does not exist)`
);
});
12 changes: 12 additions & 0 deletions server/vehicle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ export async function CreateVehicle(
state.set('initVehicle', true, true);
state.set('vehicleProperties', metadata.properties, true);

let interval: CitizenTimer;

interval = setInterval(() => {
if (GetVehicleNumberPlateText(entity) !== '') {
console.log(`Set vehicleProperties for entity ${entity}`);
state.set('vehicleProperties', null, true);
return clearInterval(interval);
}

if (!DoesEntityExist(entity)) clearInterval(interval);
}, 200);

return vehicle;
}

Expand Down

0 comments on commit 2edb79e

Please sign in to comment.