Storing a World.CreateVehicle in a Vehicle Object causes a game crash [C#]


#1

I dont know if this is unique to me or if is a common issue (I searched around and couldn’t find much around google nor the fourms)

But basically attempting to store the returned object from World.CreateVehicle method in the Citizen.FX.Core causes the game to crash.

I found that just calling the method World.CreateVehicle does not cause the game to crash, then reverting back to attempting to store the Vehicle in the Vehicle object also does not cause a crash.

But if you quit the game and load again and attempt to spawn the vehicle again it does crash.

I feel like im missing something here in regards to spawning and storing a vehicle but any other tutorial or forum post i found has it either outdated or missing the World.CreateVehicle().result at the end

Below is the method/code im attempting to run, and also. due to the game crashing i’m unable to look at some logs to see if there is a Exception im missing, Any way i may look at the logs after a crash?

        private async void VehicalMenuOnItemSelect(UIMenu sender, UIMenuItem item, int index)
        {
            switch (index)
            {
                case 0: await SpawnVehicle(); break;
            }
        }

        private async Task SpawnVehicle()
        {
            if (Vehicle == null)
            {
                Vehicle = World.CreateVehicle(VehicleHash.Taxi, VehicleSpawnPoint, 0f).Result;
                VehicleBlip = Vehicle.AttachBlip();
                VehicleBlip.Sprite = BlipSprite.PersonalVehicleCar;
                VehicleBlip.Color = BlipColor.White;
                VehicleBlip.Name = "Job Vehicle";
                VehicleBlip.IsShortRange = false;
            }
            else
            {
                Screen.ShowNotification("You already have a vehicle out");
                VehicleBlip.IsFlashing = true;
                VehicleBlip.ShowRoute = true;
            }
        }

Cheers in advance


#2

I always do this and i deeply apologise for the wasted question but i manged to find my problem just after i posted this by trial and error and on accident

Turns out you dont retrive the result of a task with .result but by putting the await state next the to the method to be called, i should have know this and it seems like a simple solution and i even reference my mistake in the original post

Vehicle = await World.CreateVehicle(VehicleHash.Taxi, VehicleSpawnPoint, 0f); // this is the correct term to use

#3

Vehicle is the Object Type. you need to create a Object name too. i.e.

private Vehicle vehicle;

if(vehicle == null){
    vehicle = await World.CreateVehicle();
}

#4

A appreciate the response thank you, i forgot i omitted part of my code to include that i had publicly declared Vehicle as a global variable outside the scope of the method.