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