Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. Red Dead Redemption 2

      1. Gameplay
      2. Missions
      3. Help & Support
    2. Red Dead Online

      1. Gameplay
      2. Find Lobbies & Outlaws
      3. Help & Support
    1. Crews & Posses

      1. Recruitment
    2. Events

    1. GTA Online

      1. DLC
      2. Find Lobbies & Players
      3. Guides & Strategies
      4. Vehicles
      5. Content Creator
      6. Help & Support
    2. Grand Theft Auto Series

    3. GTA 6

    4. GTA V

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

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

    7. GTA Vice City Stories

    8. GTA Liberty City Stories

    9. GTA San Andreas

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

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

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

      1. GTA Advance
      2. GTA 2
      3. GTA
    13. 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. 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. News

    2. Forum Support

    3. 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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • 2 Users Currently Viewing
    0 members, 0 Anonymous, 2 Guests

×
×
  • Create New...

Important Information

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