Hi there!
First of all English is not my native language so, sorry for the mess.
I’m pretty new to the JS world, so for learning more about it, I’m trying to convert some of the basics ESX mecanics into JS.
My first try is to convert the Spawn Vehicle functions, which in Lua look like this :
ESX.Game.SpawnVehicle = function(modelName, coords, heading, cb)
local model = (type(modelName) == 'number' and modelName or GetHashKey(modelName))
Citizen.CreateThread(function()
RequestModel(model)
while not HasModelLoaded(model) do
Citizen.Wait(0)
end
local vehicle = CreateVehicle(model, coords.x, coords.y, coords.z, heading, true, false)
local id = NetworkGetNetworkIdFromEntity(vehicle)
SetNetworkIdCanMigrate(id, true)
SetEntityAsMissionEntity(vehicle, true, false)
SetVehicleHasBeenOwnedByPlayer(vehicle, true)
SetModelAsNoLongerNeeded(model)
RequestCollisionAtCoord(coords.x, coords.y, coords.z)
while not HasCollisionLoadedAroundEntity(vehicle) do
RequestCollisionAtCoord(coords.x, coords.x, coords.x)
Citizen.Wait(0)
end
if cb ~= nil then
cb(vehicle)
end
end)
end
And my JS version of it look like this (missing the RequestCollision part, and cb)
window.on('rebirth:testcar', (modelName, coords, heading) => {
const model = GetHashKey(modelName);
window.setTick(() => {
RequestModel(model);
if(!HasModelLoaded(model)) return;
const vehicle = CreateVehicle(model, coords[0], coords[1], coords[2], heading, true, false );
const id = NetworkGetNetworkIdFromEntity(vehicle);
SetNetworkIdCanMigrate(id, true);
SetEntityAsMissionEntity(vehicle,true, false);
SetVehicleHasBeenOwnedByPlayer(vehicle, true);
SetModelAsNoLongerNeeded(model)
});
});
It actually works, it does spawn car… actually a lot of car
until the script loose his mind and can’t found the vehicle entity anymore.
What I’m trying to understand here is how to make it know that the car is spawned and stop the setTick to happen.
I’m sorry if my question look dumb, I’m trying my best but my brain is kinda slow.
Thanks for your time and have a nice day!
Nimo.