The game CRASH at start on car SPAWN with ped

Hello guys,
I am trying to spawn ped in a “safe way”, I mean trying to make them spawning in a safe place and on the ground directly. For that I am using a trick: create a car with a ped and then delete the car.
This avoid the ped to spawn far in the sky or above sea or building and so on…
But the code make the game crash! I don’t know why. If someone can help.
Here is the code:

      public async void SpawnPed()
        {
            await Delay(0);
            Random rndSpawn = new Random();
            Vehicle carSpawned;
            Function.Call(Hash.REQUEST_MODEL, "cavalcade");
            while (!new Model("cavalcade").IsLoaded)
            {
                await Delay(10);
            }

            Vector3 playerPos = Function.Call<Vector3>(Hash.GET_ENTITY_COORDS, Game.Player.Character);
            float newPosX = playerPos.X + rndSpawn.Next(-10, 10);
            float newPosY = playerPos.Y + rndSpawn.Next(-10, 10);

            carSpawned = Function.Call<Vehicle>(Hash.CREATE_VEHICLE, "cavalcade", newPosX, newPosY, playerPos.Z, 1f, true, false);

            Function.Call(Hash.SET_VEHICLE_ON_GROUND_PROPERLY, carSpawned);

            while (!Function.Call<bool>(Hash.HAS_MODEL_LOADED, "S_M_Y_Construct_02"))
            {
                await Delay(10);
                Function.Call(Hash.REQUEST_MODEL, "S_M_Y_Construct_02");
            }

            Function.Call(Hash.SET_MODEL_AS_NO_LONGER_NEEDED, "S_M_Y_Construct_02");

            Ped pedToSpawn = Function.Call<Ped>(Hash.CREATE_PED_INSIDE_VEHICLE, carSpawned , 4, "S_M_Y_Construct_02", -1, true, false);      

             Function.Call(Hash.SET_ENTITY_AS_MISSION_ENTITY, carSpawned, true, true);
             Function.Call(Hash.DELETE_VEHICLE, carSpawned);

             if (Entity.Exists(pedToSpawn))
              {
                    carSpawned.MarkAsNoLongerNeeded();
                    carSpawned.Delete();
               }
        }

Thanks

The actual CREATE_VEHICLE/PED natives take a hash, not a string. The Lua API adapts these to support a string, to make sure you’re passing the right arguments to natives in C#, try something like this:

using static CitizenFX.Core.Native.API;

// ...

RequestModel(...);

Thank you for your answer. I have followed your advice. Unfortunately it still crash the gamewith a message error “Type vehicule cannot be marshalled as an unmanaged structure”.
Sometimes it is " “Type Ped cannot be marshalled as an unmanaged structure”.

Ok. I am trying to understand what is wrong with that code. Then I have restarted the function from the beginning and by the simpliest instruction: start by only spawn a ped. And it doesn’t works. I mean it start and I can see the ped spawning. And that’s it. 1 or 2 seconds later it crash. Same error message: "Type Ped cannot be marshalled as an unmanaged structure”!
Here is the code:

public Main()
{
    EventHandlers["playerSpawned"] += new Action(AtStart);
    Tick += OnTick;
}

public async void AtStart()
{
    var model = new Model("S_M_Y_AIRWORKER");
    await model.Request(50);
    // Check the model is valid
    if (model.IsInCdImage && model.IsValid)
    {
        TriggerEvent("chatMessage", "Cool !!! Ton modele est valide!!!");
         // Ensure the model is loaded before we try to create it in the world
          while (!model.IsLoaded)
          {
               await Delay(1);
          }
     }
     
     Function.Call(Hash.SET_MODEL_AS_NO_LONGER_NEEDED, new InputArgument[] { model });

     Vector3 playerPos = Function.Call<Vector3>(Hash.GET_ENTITY_COORDS, Game.Player.Character);

     Ped superPed = Function.Call<Ped>(Hash.CREATE_PED, 4, model, playerPos.X + 5f, playerPos.Y, playerPos.Z, 0.0f, false, false);
}

Help someone! :disappointed_relieved:

you can’t use a wrapper class as return type, you’ve got to use <int> and then do new Ped(value)

Thanks to you guys! It works now

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.