# About writting mods

**URL:** <https://discourse.cataclysmdda.org/t/about-writting-mods/14388>\
**Category:** The Drawing Board\
**Created:** [October 23, 2017, 11:51pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388 "2017-10-23T23:51:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![however](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@however](https://discourse.cataclysmdda.org/u/however)\
**Post date:** [October 23, 2017, 11:51pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/1 "2017-10-23T23:51:16Z")

</div>

Hello,

I just want to ask about mod writting, from coding im mostly know C but my question is pure curiosity. I saw that mods are mostly written in json files, but i saw lua also and i heard that C also could work. And my question is, how program read those lua files, and how could write c, and overall how it reading json exacly. I just want to know that, if someone can explain me ill be much gratefull 🙂

---

<div class="post-metadata">

**Author:** ![kevin.granade](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/kevin.granade/32/1316_2.png) [@kevin.granade](https://discourse.cataclysmdda.org/u/kevin.granade)\
**Post date:** [October 24, 2017, 12:29am UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/2 "2017-10-24T00:29:48Z")

</div>

Adding or editing JSON.  
The vast majority of the data dda handles (item definitions, crafting recipes, monster definitions, map generation templates, martial arts styles, mutations) is actually defined as json files. How this works is that there is some code written in c++ that loads the json entities and turns them into data and logic that can be used by the game engine. There are only certain things you can adjust about a JSON entity, because the code that reads it has to be written in such a way that it can apply that data to an entity of that type.  
Implementation-wise, there are two components to this. First we have a JSON library that was written for dda that can parse and emit JSON. Secondly there is code for each kind of entity that reads JSON entries and turns them into game logic.

Adding or editing LUA.  
LUA is a programming language that is designed to be embedded in other programs. There are various parts of dda that can accept LUA as an input, usually as part of some JSON entity. In theory, any behavior can be specified by LUA, but in practice, each piece of functionality that a piece of LUA can exercise has to have supporting C++ code. As a result, there are only a few places where LUA can be injected into the code, making it difficult to work with.

---

<div class="post-metadata">

**Author:** ![however](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@however](https://discourse.cataclysmdda.org/u/however)\
**Post date:** [October 24, 2017, 1:01am UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/3 "2017-10-24T01:01:48Z")

</div>

> [@Kevin Granade](#):
>
> How this works is that there is some code written in c++ that loads the json entities and turns them into data and logic that can be used by the game engine.

Two questions:  
1.In what place of c++ code exacly json is loaded into game ?  
2.Is in that same manner like json, lua is loaded ? Becouse i saw mods where in one catalog was and lua and json files, there cant be just one big json file ? And why that lua is not good for implement like json exacly ?

---

<div class="post-metadata">

**Author:** ![Zhilkin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/zhilkin/32/3749_2.png) [@Zhilkin](https://discourse.cataclysmdda.org/u/Zhilkin)\
**Post date:** [October 24, 2017, 5:02am UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/4 "2017-10-24T05:02:31Z")

</div>

Lua can be called from several places:

- in-game lua console (called from debug menu, useful for debug and testing purposes);
- mapgen (`lua` mapgen method);
- mapgen finishing (`lua` finishing script, called after mapgen is done);
- on game loaded (called from `preload.lua` in mod folder);
- in various callbacks (called from `main.lua` in mod folder).

You can use `dofile('./folder/subfolder/filename.lua')` function for convenience in any of the above.

There are currently four callbacks functions:

- on\_new\_player\_created;
- on\_skill\_increased;
- on\_minute\_passed;
- on\_day\_passed.

I want to add more callback functions - [https://github.com/CleverRaven/Cataclysm-DDA/pull/21744/](https://github.com/CleverRaven/Cataclysm-DDA/pull/21744/).

Some useful links on LUA in Cataclysm:

- on lua support - [https://github.com/CleverRaven/Cataclysm-DDA/blob/master/doc/LUA\_SUPPORT.md](https://github.com/CleverRaven/Cataclysm-DDA/blob/master/doc/LUA_SUPPORT.md)
- on lua mapgen - [https://github.com/CleverRaven/Cataclysm-DDA/blob/master/doc/MAPGEN.md](https://github.com/CleverRaven/Cataclysm-DDA/blob/master/doc/MAPGEN.md)
- functions exported from cpp to lua - [https://github.com/CleverRaven/Cataclysm-DDA/blob/master/lua/class\_definitions.lua](https://github.com/CleverRaven/Cataclysm-DDA/blob/master/lua/class_definitions.lua)
- sample mods bundled with the game- [https://github.com/CleverRaven/Cataclysm-DDA/tree/master/doc/sample\_mods](https://github.com/CleverRaven/Cataclysm-DDA/tree/master/doc/sample_mods)
- StatsThroughSkills mod bundled with game - [https://github.com/CleverRaven/Cataclysm-DDA/blob/master/data/mods/StatsThroughSkills/main.lua](https://github.com/CleverRaven/Cataclysm-DDA/blob/master/data/mods/StatsThroughSkills/main.lua)
- Cataclysm-DDA-LUA mod collection for CDDA - [https://github.com/ZhilkinSerg/Cataclysm-DDA-LUA](https://github.com/ZhilkinSerg/Cataclysm-DDA-LUA) or [http://smf.cataclysmdda.com/index.php?topic=14861.0](http://smf.cataclysmdda.com/index.php?topic=14861.0)
- DegradeBuildings mod for CDDA - [http://smf.cataclysmdda.com/index.php?topic=14825.0](http://smf.cataclysmdda.com/index.php?topic=14825.0)
- ForestGrowth mod for CDDA - [http://smf.cataclysmdda.com/index.php?topic=15051.msg307590#msg307590](http://smf.cataclysmdda.com/index.php?topic=15051.msg307590#msg307590)
- links to some lua code/mods for CDDA - [https://www.reddit.com/r/cataclysmdda/comments/6rb4wk/lua\_mods/](https://www.reddit.com/r/cataclysmdda/comments/6rb4wk/lua_mods/)

---

<div class="post-metadata">

**Author:** ![Coolthulhu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/coolthulhu/32/2446_2.png) [@Coolthulhu](https://discourse.cataclysmdda.org/u/Coolthulhu)\
**Post date:** [October 24, 2017, 12:30pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/5 "2017-10-24T12:30:45Z")

</div>

> [@however](#):
>
> In what place of c++ code exacly json is loaded into game ?

Each type has its own loader function. They are defined in relevant files (say, item\_factory.cpp covers item loading), then assigned to json types in init.cpp. That is, which function is called depends on “type” field of the json.  
Many jsons use a structure called generic\_factory (in generic\_factory.h) to store the loaded values.

---

<div class="post-metadata">

**Author:** ![however](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@however](https://discourse.cataclysmdda.org/u/however)\
**Post date:** [October 24, 2017, 3:23pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/6 "2017-10-24T15:23:42Z")

</div>

Which exacly lines of .cpp code load json and lua mods in those files ?

---

<div class="post-metadata">

**Author:** ![Coolthulhu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/coolthulhu/32/2446_2.png) [@Coolthulhu](https://discourse.cataclysmdda.org/u/Coolthulhu)\
**Post date:** [October 24, 2017, 4:08pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/7 "2017-10-24T16:08:06Z")

</div>

There are hundreds if not thousands of lines that do that. You’ll need to be much more specific.

---

<div class="post-metadata">

**Author:** ![however](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@however](https://discourse.cataclysmdda.org/u/however)\
**Post date:** [October 24, 2017, 4:25pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/8 "2017-10-24T16:25:54Z")

</div>

I just not get it where game ask for mods.

---

<div class="post-metadata">

**Author:** ![Zhilkin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/zhilkin/32/3749_2.png) [@Zhilkin](https://discourse.cataclysmdda.org/u/Zhilkin)\
**Post date:** [October 24, 2017, 6:21pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/9 "2017-10-24T18:21:22Z")

</div>

Following function loads mod data (see [https://github.com/CleverRaven/Cataclysm-DDA/blob/master/src/game.cpp#L384](https://github.com/CleverRaven/Cataclysm-DDA/blob/master/src/game.cpp#L384)):

```auto
void game::load_data_from_dir( const std::string &path, const std::string &src, loading_ui &ui )
```

---

<div class="post-metadata">

**Author:** ![however](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@however](https://discourse.cataclysmdda.org/u/however)\
**Post date:** [October 24, 2017, 6:44pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/10 "2017-10-24T18:44:46Z")

</div>

what is “const std::string & path, const std::string &src” ?

---

<div class="post-metadata">

**Author:** ![Zhilkin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/zhilkin/32/3749_2.png) [@Zhilkin](https://discourse.cataclysmdda.org/u/Zhilkin)\
**Post date:** [October 24, 2017, 6:48pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/11 "2017-10-24T18:48:24Z")

</div>

> [@however](#):
>
> what is “const std::string & path, const std::string &src” ?

You can look for function calls:

```auto
load_data_from_dir( mod.path, mod.ident, ui );
```

first parameter is mod folder, second is mod identifier

---

<div class="post-metadata">

**Author:** ![however](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@however](https://discourse.cataclysmdda.org/u/however)\
**Post date:** [October 24, 2017, 7:13pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/12 "2017-10-24T19:13:12Z")

</div>

Mod options are reading from that mod folder all time or those things go to ram untill game is end ?

---

<div class="post-metadata">

**Author:** ![Zhilkin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/zhilkin/32/3749_2.png) [@Zhilkin](https://discourse.cataclysmdda.org/u/Zhilkin)\
**Post date:** [October 24, 2017, 8:35pm UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/13 "2017-10-24T20:35:51Z")

</div>

Data is loaded to memory on new game start/savegame load.

---

<div class="post-metadata">

**Author:** ![however](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@however](https://discourse.cataclysmdda.org/u/however)\
**Post date:** [October 25, 2017, 1:14am UTC](https://discourse.cataclysmdda.org/t/about-writting-mods/14388/14 "2017-10-25T01:14:08Z")

</div>

Now i get it, thanks ! 🙂
