Quantcast

Jump to content

» «
Photo

[C#] How to spawn car with a ped?

5 replies to this topic
Aktarus
  • Aktarus

    Player Hater

  • Members
  • Joined: 21 Aug 2017
  • United-Kingdom

#1

Posted 3 weeks ago

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 above the ground, above sea or building and so on…
But the code doesn't work! I don’t know why. If someone can help.
Here is the code:

       public async void SpawnPed()
        {
            await Delay(0);
            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(-100, 100);
            float newPosY = playerPos.Y + rndSpawn.Next(-100, 100);

            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);

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

Thanks

 

 

 


sollaholla
  • sollaholla

    C# Dev.

  • Members
  • Joined: 19 May 2016
  • United-States

#2

Posted 3 weeks ago

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 above the ground, above sea or building and so on…
But the code doesn't work! I don’t know why. If someone can help.

Thanks

Have you tried GET_GROUND_Z_FOR_3D_COORD and setting the Ped's Z coordinate to the return value?


Aktarus
  • Aktarus

    Player Hater

  • Members
  • Joined: 21 Aug 2017
  • United-Kingdom

#3

Posted 3 weeks ago

 

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 above the ground, above sea or building and so on…
But the code doesn't work! I don’t know why. If someone can help.

Thanks

Have you tried GET_GROUND_Z_FOR_3D_COORD and setting the Ped's Z coordinate to the return value?

 

 

Oh. I didn't know this function. So it gives you the Z value of the ground from the given coords. I am gonna try that right now.


Aktarus
  • Aktarus

    Player Hater

  • Members
  • Joined: 21 Aug 2017
  • United-Kingdom

#4

Posted 3 weeks ago Edited by Aktarus, 3 weeks ago.

So after some test. I have decided to restart from the begining this function. Then I realised that the simple and basic CREATE_PED doesn't work. Actually it works... but after 1 or 2 seconds the game crash. The error message is "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(10);

    // Check if the model is valid
    if (model.IsInCdImage && model.IsValid)
    {
        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);
}

Does anyone can explain me what this error means please?


kagikn
  • kagikn

    Ghillie

  • Members
  • Joined: 05 Jun 2014
  • Japan

#5

Posted 3 weeks ago

So after some test. I have decided to restart from the begining this function. Then I realised that the simple and basic CREATE_PED doesn't work. Actually it works... but after 1 or 2 seconds the game crash. The error message is "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(10);

    // Check if the model is valid
    if (model.IsInCdImage && model.IsValid)
    {
        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);
}
Does anyone can explain me what this error means please?

This is probably responsible for the error (GetUninitializedObject doesn't call constructors). https://github.com/c...Native.cpp#L188
So you have to avoid using the classes implementing INativeValue for return values for Native.Call, for now.
And I believe "await Delay(1);" doesn't work to pause execution, use Wait(0) or Yield() instead.

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

public async void AtStart()
{
    var model = new Model("S_M_Y_AIRWORKER");
    model.Request();

    // Check if the model is valid
    if (model.IsInCdImage && model.IsValid)
    {
        while (!model.IsLoaded)
        {
            Yield();
        }
    }
    Function.Call(Hash.SET_MODEL_AS_NO_LONGER_NEEDED, model.Hash);

    Vector3 playerPos = Function.Call<Vector3>(Hash.GET_ENTITY_COORDS, Game.Player.Character);
    Ped superPed = new Ped(Function.Call<int>(Hash.CREATE_PED, 4, model, playerPos.X + 5f, playerPos.Y, playerPos.Z, 0.0f, false, false));
}
  • Aktarus likes this

Aktarus
  • Aktarus

    Player Hater

  • Members
  • Joined: 21 Aug 2017
  • United-Kingdom

#6

Posted 3 weeks ago

@kagikn

It works! Thanks mate.

  • sollaholla likes this




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users