Entity streaming example
Create the entity and set metadata on the authoritative server wrapper:
ts
const vehicle = vimp.vehicles.create(
vimp.hash('adder'),
94.5,
-1940.2,
20.7,
307,
{ dimension: 0 },
)
if (vehicle) {
vehicle.setStreamSyncedMeta('fuel', 65)
vehicle.setStreamSyncedMeta('owner', null)
}Read the initial snapshot on stream-in and observe later updates:
ts
vimp.on('streamSyncedMetaChange', (change) => {
if (
change.entityType === vimp.entities.ENTITY_TYPE.Vehicle &&
change.key === 'fuel'
) {
console.log(`Vehicle #${change.entityId} fuel:`, change.newValue)
}
})
vimp.on('vehicleStreamIn', (vehicle) => {
if (!vehicle) return
if (vehicle.remoteId === null) return
const fuel = vimp.entities.getStreamSyncedMeta<number>(
vimp.entities.ENTITY_TYPE.Vehicle,
vehicle.remoteId,
'fuel',
)
console.log(`Vehicle #${vehicle.remoteId} streamed in with fuel ${fuel}`)
})The client's remoteId is the identifier used by generic replicated entity APIs. Do not cache the GTA handle across stream-out and stream-in transitions.