# Custom death event using LUA?

**URL:** <https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185>\
**Category:** The Lab\
**Tags:** mod\
**Created:** [August 12, 2018, 6:12am UTC](https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185 "2018-08-12T06:12:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axema](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/axema/32/10822_2.png) [@Axema](https://discourse.cataclysmdda.org/u/Axema)\
**Post date:** [August 12, 2018, 6:12am UTC](https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185/1 "2018-08-12T06:12:51Z")

</div>

As said, I want to get some help from creating a custom death function with .LUA file. Can someone tell me how its done?

Thanks 🙂

---

<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:** [August 12, 2018, 12:51pm UTC](https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185/2 "2018-08-12T12:51:13Z")

</div>

Do you want to use LUA to make something happen when a creature dies, or do you want to add a new function to LUA? The second is kind of documented in doc/LUA\_SUPPORT.md.

---

<div class="post-metadata">

**Author:** ![Axema](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/axema/32/10822_2.png) [@Axema](https://discourse.cataclysmdda.org/u/Axema)\
**Post date:** [August 12, 2018, 9:39pm UTC](https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185/3 "2018-08-12T21:39:42Z")

</div>

I’ve been reading it everytime, but nothing really happpens ☹  
Its hard for me to understand, tbh.

---

<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:** [August 12, 2018, 9:45pm UTC](https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185/4 "2018-08-12T21:45:06Z")

</div>

Again, what you even trying to do? Depending on your goal, LUA may not even be the right solution.

---

<div class="post-metadata">

**Author:** ![Axema](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/axema/32/10822_2.png) [@Axema](https://discourse.cataclysmdda.org/u/Axema)\
**Post date:** [August 13, 2018, 5:38am UTC](https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185/5 "2018-08-13T05:38:59Z")

</div>

I want to make a modified death function that spawns a secondary creature after death of the primary. It is similar to “THING” death function in pattern of:

tentacle dog \> thing  
primary \> secondary

I have no idea how can I make the code into a .LUA file

---

<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:** [August 13, 2018, 12:39pm UTC](https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185/6 "2018-08-13T12:39:07Z")

</div>

Ah, okay. I thought you might be able to do this in JSON, but death spawn is hard coded to WORMS and BLOB and there’s no generic C++ function.

I don’t know LUA very well, but afaict there isn’t an “on\_death” trigger.

Personally, since I’m an okay C++ programmer, I’d submit a patch to add some new optional JSON to a MONSTER, something like this:

```auto
"death_spawn": { // optional death spawn
    "spawn_id": "death_dog", // mandatory spawn
    "number": [2, 2], // mandatory range of spawns, in this case 2 to 2
    "death_msg": "<number> <spawnname> crawl out of <name>'s corpse", // mandatory death message
    "only_death_msg": "The <name> splits in <number>", // optional death message if this the only death event
    "terrain": ["DIGGABLE"] // optional TERRAIN flags to limit spawn points
},

```

and then convert WORMS and mondeath::worm() to the use the new death\_spawn object. And then add your tentacle dog.

Unfortunately, despite ZhilkinSerg’s efforts, the use of LUA in CDDA is poorly documented. You might try asking him on the discord channel for advice.

---

<div class="post-metadata">

**Author:** ![Axema](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/axema/32/10822_2.png) [@Axema](https://discourse.cataclysmdda.org/u/Axema)\
**Post date:** [August 13, 2018, 1:01pm UTC](https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185/7 "2018-08-13T13:01:16Z")

</div>

I’ll try that. and thanks for that advice 😃

One last question… And I apologize for being an info-hungry guy.  
So, “reproduction” even exist in-game? I’ve been trying it with this guide from the source [code](https://github.com/CleverRaven/Cataclysm-DDA/blob/master/src/monstergenerator.cpp):

```
 //Reproduction
    if( jo.has_member( "reproduction" ) ) {
        JsonObject repro = jo.get_object( "reproduction" );
        optional( repro, was_loaded, "baby_count", baby_count, -1 );
        optional( repro, was_loaded, "baby_timer", baby_timer, -1 );
        optional( repro, was_loaded, "baby_monster", baby_monster, auto_flags_reader<mtype_id> {},
                  mtype_id::NULL_ID() );
        optional( repro, was_loaded, "baby_egg", baby_egg, auto_flags_reader<itype_id> {},
                  "null" );
        if( jo.has_member( "baby_flags" ) ) {
            baby_flags.clear();
            JsonArray baby_tags = jo.get_array( "baby_flags" );
            while( baby_tags.has_more() ) {
                baby_flags.push_back( baby_tags.next_string() );
            }
        }
        reproduces = true;
    }

```

**I did this one:**

```
{
 {
"reproduction": {
"baby_monster": "mon_insect_saddler_baby",
"baby_count": 1,
"baby_timer": 32 }
 }
}

```

Nothing really occurs, or something is not right with the .JSON code 😕

---

<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:** [August 13, 2018, 1:40pm UTC](https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185/8 "2018-08-13T13:40:00Z")

</div>

Reproduction is a thing, but I think the baby timer is in days so you’ll have to wait a while for your baby insect. Also, you need to add the reproduction object to a specific monster. See data/json/monsters/mammal.json for the bear (mon\_bear) and bear cub (mon\_bear\_cub).

But if you play the game and live for a couple of weeks, you’ll see baby chickens and deer fawns and puppies all over the place.

---

<div class="post-metadata">

**Author:** ![Axema](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/axema/32/10822_2.png) [@Axema](https://discourse.cataclysmdda.org/u/Axema)\
**Post date:** [August 13, 2018, 10:13pm UTC](https://discourse.cataclysmdda.org/t/custom-death-event-using-lua/16185/9 "2018-08-13T22:13:13Z")

</div>

Thanks, I thought the timer is calculated as seconds 😄
