About writting mods


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 :slight_smile:

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.

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 ?

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

Some useful links on LUA in Cataclysm:

1 Like

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.

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

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

I just not get it where game ask for mods.

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

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

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

You can look for function calls:

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

first parameter is mod folder, second is mod identifier

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

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

Now i get it, thanks ! :slight_smile: