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 :joy: 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.

You could set a variable outside of the tick, then use an if statement to check if the variable is equal to true or false, once the vehicle is loaded set it to true.

window.on('rebirth:testcar', (modelName, coords, heading) => {
    const model = GetHashKey(modelName);
    let loaded = false;
    window.setTick(() => {
       if(!loaded){
        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)
        loaded = true;
     }
    });
});

I’ve not tested it but this should work.

1 Like

It worked like a charm!
Thanks you a lot :slight_smile:

EDIT : Well, it worked nice, but now I want to know if there’s a way to fully stop the setTick to happen to don’t have useless thread running to enhance performance. Something like a clearTick (like clearTimeout or clearInterval)
Any clue ?

EDIT 2 : Looking at this : v8/timer.js made us think maybe using setTimer could work. Will try this and later edit again this.