# How to spawn NPCs?

**URL:** <https://discourse.cataclysmdda.org/t/how-to-spawn-npcs/18750>\
**Category:** The Toolbox\
**Created:** [February 10, 2019, 11:37am UTC](https://discourse.cataclysmdda.org/t/how-to-spawn-npcs/18750 "2019-02-10T11:37:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fris0uman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/fris0uman/32/8607_2.png) [@Fris0uman](https://discourse.cataclysmdda.org/u/Fris0uman)\
**Post date:** [February 10, 2019, 11:37am UTC](https://discourse.cataclysmdda.org/t/how-to-spawn-npcs/18750/1 "2019-02-10T11:37:11Z")

</div>

/!\ Possible spoiler ahead /!\  
I need help with [#28157 (contains spoiler)](https://github.com/CleverRaven/Cataclysm-DDA/pull/28157)

To make it work I need to spawn NPC near the player, but what i’ve got so far crashes the game :

```auto
        std::shared_ptr<npc> tmp = std::make_shared<npc>();
        tmp->normalize();
        tmp->randomize( NC_CLASSE );
        tmp->spawn_at_precise({ get_levx(), get_levy() }, p);
        return true; 

```

( [SPOILER] : NC\_CLASS is in fact NC\_HALLU which is a new class )

So what’s the proper way to spawn an NPC ?

---

<div class="post-metadata">

**Author:** ![Fris0uman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/fris0uman/32/8607_2.png) [@Fris0uman](https://discourse.cataclysmdda.org/u/Fris0uman)\
**Post date:** [February 13, 2019, 8:29pm UTC](https://discourse.cataclysmdda.org/t/how-to-spawn-npcs/18750/2 "2019-02-13T20:29:50Z")

</div>

I changed code to :

```auto
    if( one_in( 100 ) ) {
        std::shared_ptr<npc> tmp = std::make_shared<npc>();
        tmp->normalize();
        tmp->randomize( NC_CLASS );
        tmp->spawn_at_precise( { get_levx(), get_levy() }, p );
        if( !critter_at( p, true ) ) {
            overmap_buffer.insert_npc( tmp );
            return true;
        } else {
            return false;
        }
    }

```

And now instead of a hard crash I get :

```auto
  DEBUG : Invalid body part token -858993460
 
 FUNCTION : convert_bp
 FILE : ..\src\bodypart.cpp
 LINE : 136

```

Anybody got any idea on what I’m doing wrong ?

---

<div class="post-metadata">

**Author:** ![Valiant](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/valiant/32/4788_2.png) [@Valiant](https://discourse.cataclysmdda.org/u/Valiant)\
**Post date:** [February 14, 2019, 3:23pm UTC](https://discourse.cataclysmdda.org/t/how-to-spawn-npcs/18750/3 "2019-02-14T15:23:07Z")

</div>

Have you tried to add `load_npcs()` in the end, like in code to spawn NPC in debug menu?

---

<div class="post-metadata">

**Author:** ![Fris0uman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/fris0uman/32/8607_2.png) [@Fris0uman](https://discourse.cataclysmdda.org/u/Fris0uman)\
**Post date:** [February 14, 2019, 10:00pm UTC](https://discourse.cataclysmdda.org/t/how-to-spawn-npcs/18750/4 "2019-02-14T22:00:07Z")

</div>

Just did, it still gives the same error.

---

<div class="post-metadata">

**Author:** ![mlangsdorf](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/mlangsdorf/32/2462_2.png) [@mlangsdorf](https://discourse.cataclysmdda.org/u/mlangsdorf)\
**Post date:** [February 15, 2019, 8:03pm UTC](https://discourse.cataclysmdda.org/t/how-to-spawn-npcs/18750/5 "2019-02-15T20:03:43Z")

</div>

Why aren’t you using:

```auto
    const string_id<npc_template> npc_template_name( "npc_template_name" );
    const int tmp_id = g->m.place_npc( get_levx(), get_levy(), npc_template_name );
    g->load_npcs();

    return g->find_npc( tmp_id );

```

I know that works for me.

---

<div class="post-metadata">

**Author:** ![Fris0uman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/fris0uman/32/8607_2.png) [@Fris0uman](https://discourse.cataclysmdda.org/u/Fris0uman)\
**Post date:** [February 19, 2019, 8:24am UTC](https://discourse.cataclysmdda.org/t/how-to-spawn-npcs/18750/6 "2019-02-19T08:24:03Z")

</div>

I’ve made the npc template, and implemented your suggestion but I still get the Invalid body part token error.

What does this errorr mean ? I don’t see where we would/should have touch anything about body part.

---

<div class="post-metadata">

**Author:** ![Fris0uman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/fris0uman/32/8607_2.png) [@Fris0uman](https://discourse.cataclysmdda.org/u/Fris0uman)\
**Post date:** [March 8, 2019, 11:30am UTC](https://discourse.cataclysmdda.org/t/how-to-spawn-npcs/18750/7 "2019-03-08T11:30:49Z")

</div>

Does anybody know what’s the link between bodypart.cpp and spawning an NPC ?

Does the error could come from a mistake in the json ? Even though bodyparts are never adressed in any of them.

EDIT : I haven’t figure where the bodypart error came from but this works as far as spawning NPC is concerned

```auto
        std::shared_ptr<npc> tmp = std::make_shared<npc>();
        tmp->normalize();
        tmp->randomize( NC_CLASS );
        tmp->spawn_at_precise( { get_levx(), get_levy() }, tripoint );
        overmap_buffer.insert_npc( tmp );
        load_npcs();
```
