Jump to content

[Solved!] How to spawn ped from C++ Script Hook?


WesternGamer
 Share

Recommended Posts

I am making a mod and I need help spawning a ped. I tried finding the function that spawns peds in Visual Studio 2019 by using Find but was not successful. Can someone tell me what function spawns peds and show me how it is used?

Edited by WesternGamer
Marked post as solved
Link to comment
Share on other sites

Ped spawnPed(Hash pedModel, float coordX, float coordY, float coordZ)
{
	STREAMING::REQUEST_MODEL(pedModel, true);
	while (!STREAMING::HAS_MODEL_LOADED(pedModel)) WAIT(0);
	Ped pedSpawn = PED::CREATE_PED(pedModel, coordX, coordY, coordZ, 0, 1, 1, 1, 1);
	PED::_SET_RANDOM_OUTFIT_VARIATION(pedSpawn, 1);
	ENTITY::PLACE_ENTITY_ON_GROUND_PROPERLY(pedSpawn, 1);
	ENTITY::FREEZE_ENTITY_POSITION(pedSpawn, 0);
	STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(pedModel);
	return pedSpawn;
}

I do it like this.

So you can do something like this for example, this should spawn Charles next to you:

Vector3 plCoords = ENTITY::GET_ENTITY_COORDS(PLAYER::PLAYER_PED_ID(), true, false);
Ped myPed = spawnPed(MISC::GET_HASH_KEY("CS_charlessmith"), plCoords.x + 2, plCoords.y, plCoords.z);

 

Link to comment
Share on other sites

1 hour ago, crossed99 said:

Ped spawnPed(Hash pedModel, float coordX, float coordY, float coordZ)
{
	STREAMING::REQUEST_MODEL(pedModel, true);
	while (!STREAMING::HAS_MODEL_LOADED(pedModel)) WAIT(0);
	Ped pedSpawn = PED::CREATE_PED(pedModel, coordX, coordY, coordZ, 0, 1, 1, 1, 1);
	PED::_SET_RANDOM_OUTFIT_VARIATION(pedSpawn, 1);
	ENTITY::PLACE_ENTITY_ON_GROUND_PROPERLY(pedSpawn, 1);
	ENTITY::FREEZE_ENTITY_POSITION(pedSpawn, 0);
	STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(pedModel);
	return pedSpawn;
}

I do it like this.

So you can do something like this for example, this should spawn Charles next to you:


Vector3 plCoords = ENTITY::GET_ENTITY_COORDS(PLAYER::PLAYER_PED_ID(), true, false);
Ped myPed = spawnPed(MISC::GET_HASH_KEY("CS_charlessmith"), plCoords.x + 2, plCoords.y, plCoords.z);

 

@crossed99 Thanks for the info, this will definitely help!

Link to comment
Share on other sites

  • 3 months later...
On 9/2/2021 at 12:30 AM, crossed99 said:
Ped spawnPed(Hash pedModel, float coordX, float coordY, float coordZ)
{
	STREAMING::REQUEST_MODEL(pedModel, true);
	while (!STREAMING::HAS_MODEL_LOADED(pedModel)) WAIT(0);
	Ped pedSpawn = PED::CREATE_PED(pedModel, coordX, coordY, coordZ, 0, 1, 1, 1, 1);
	PED::_SET_RANDOM_OUTFIT_VARIATION(pedSpawn, 1);
	ENTITY::PLACE_ENTITY_ON_GROUND_PROPERLY(pedSpawn, 1);
	ENTITY::FREEZE_ENTITY_POSITION(pedSpawn, 0);
	STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(pedModel);
	return pedSpawn;
}

I do it like this.

So you can do something like this for example, this should spawn Charles next to you:

Vector3 plCoords = ENTITY::GET_ENTITY_COORDS(PLAYER::PLAYER_PED_ID(), true, false);
Ped myPed = spawnPed(MISC::GET_HASH_KEY("CS_charlessmith"), plCoords.x + 2, plCoords.y, plCoords.z);

 

 

 

Not working for me, need further instructions

 

Ped spawnPed(Hash pedModel, float coordX, float coordY, float coordZ)
{
    STREAMING::REQUEST_MODEL(pedModel, true);
    while (!STREAMING::HAS_MODEL_LOADED(pedModel)) WAIT(0);
    Ped pedSpawn = PED::CREATE_PED(pedModel, coordX, coordY, coordZ, 0, 1, 1, 1, 1);
//    PED::_SET_RANDOM_OUTFIT_VARIATION(pedSpawn, 1);
//    ENTITY::PLACE_ENTITY_ON_GROUND_PROPERLY(pedSpawn, 1);
//    ENTITY::FREEZE_ENTITY_POSITION(pedSpawn, 0);
    STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(pedModel);
    return pedSpawn;
}

 

 

// lines due to compiler errors

 

 

    while (true)

    {

        if (IsKeyJustUp(VK_F3)) // F3 to spawn a female
        {
            
            Vector3 plCoords = ENTITY::GET_ENTITY_COORDS(PLAYER::PLAYER_PED_ID(), true, false);
            Ped myPed = spawnPed((GAMEPLAY::GET_HASH_KEY("sac_female")), plCoords.x + 2, plCoords.y, plCoords.z);


            DrawText(0, 0, "Spawning NPC");
        }

 

 

Pressing F3, nothing happens (no spawn), besides "spawning NPC" appearing briefly on the screen, signaling F3 keypress was registered 

 

 

 

My Fallout 4 and Skyrim mods: SAC - LoversLab

 

Link to comment
Share on other sites

Without the _SET_RANDOM_OUTFIT_VARIATION line they're invisible when they spawn in, so it might be working you just can't see them. You can also try  instead: ENTITY::SET_ENTITY_VISIBLE(pedSpawn, true);

Or maybe they fall under the ground without the PLACE_ENTITY_ON_GROUND line.

 

But they shouldn't give you compiler errors. What errors are you getting?

Edited by crossed99
Link to comment
Share on other sites

9 hours ago, crossed99 said:

Without the _SET_RANDOM_OUTFIT_VARIATION line they're invisible when they spawn in, so it might be working you just can't see them. You can also try  instead: ENTITY::SET_ENTITY_VISIBLE(pedSpawn, true);

Or maybe they fall under the ground without the PLACE_ENTITY_ON_GROUND line.

 

But they shouldn't give you compiler errors. What errors are you getting?

 

Turns out my natives.h was outdated. SgtJoe helped me update it, now your code works and spawns the peds as intended

 

Thank you

 

 

 

My Fallout 4 and Skyrim mods: SAC - LoversLab

 

Link to comment
Share on other sites

17 hours ago, crossed99 said:

Without the _SET_RANDOM_OUTFIT_VARIATION line they're invisible when they spawn in, so it might be working you just can't see them. You can also try  instead: ENTITY::SET_ENTITY_VISIBLE(pedSpawn, true);

Or maybe they fall under the ground without the PLACE_ENTITY_ON_GROUND line.

 

But they shouldn't give you compiler errors. What errors are you getting?

 

 

I'd like to refine this a little further:

 

- I'd like the ped to spawn in front of the player, but no matter how I offset x and y, the spawn point seems random

 

- I'd like the ped to turn to face the player, have tried 

 

TASK::TASK_TURN_PED_TO_FACE_ENTITY(myPed, player, 2000, 0, 0, 0);

 

but it doesn't seem to do anything

 

 

My Fallout 4 and Skyrim mods: SAC - LoversLab

 

Link to comment
Share on other sites

Modifying X and Y moves the spawn point east / west and north / south from you, regardless of your facing.

You can get the coordinates in front of a ped with this:

Vector3 front = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(playerPed, 0.0f, 2.0f, 0.0f);

 

I can't test it right now, but your native to turn them should work, pretty sure I used that before. Make sure "player" is PLAYER::PLAYER_PED_ID() and not PLAYER::PLAYER_ID() or something, other than that not sure...

  • Thanks 1
Link to comment
Share on other sites

 

Follow-up / tune-up:

 

I have a custom ped model added to metapeds, containing outfits from multiple groups of peds (a mashup, so to speak)

 

PED::_SET_RANDOM_OUTFIT_VARIATION(pedSpawn, 1);

 

^^ this is very "sticky", returns outfits from the same group of peds, resulting in uniform populations

 

PED::_SET_PED_OUTFIT_PRESET(pedSpawn, (std::rand() % (PED::GET_NUM_META_PED_OUTFITS(pedSpawn) - 1)), false);

 

^^ this offers true (or, anyway, better) randomization

 

 

 

My Fallout 4 and Skyrim mods: SAC - LoversLab

 

Link to comment
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.

 Share

×
×
  • Create New...