Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!   (84,805 visits to this link)

    2. News

    1. GTA Online

      1. Find Lobbies & Players
      2. Guides & Strategies
      3. Vehicles
      4. Content Creator
      5. Help & Support
    2. Crews

      1. Events
      2. Recruitment
    1. Grand Theft Auto Series

    2. GTA Next

    3. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    4. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA Mods
    5. GTA Chinatown Wars

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    9. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    10. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    11. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    12. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    3. Featured Mods

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption 2

    2. Red Dead Redemption

    3. Rockstar Games

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Programming
      5. Movies & TV
      6. Music
      7. Sports
      8. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Forum Support

    2. Site Suggestions

Sign in to follow this  
Aktarus

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

Recommended Posts

Aktarus

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

 

 

 

Share this post


Link to post
Share on other sites
sollaholla

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?

Share this post


Link to post
Share on other sites
Aktarus

 

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.

Share this post


Link to post
Share on other sites
Aktarus

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?

Edited by Aktarus

Share this post


Link to post
Share on other sites
kagikn

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/crosire/scripthookvdotnet/blob/dev_v3/source/core/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));}

Share this post


Link to post
Share on other sites
Aktarus

@kagikn

It works! Thanks mate.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.