From Minetest Developer Wiki
Jump to: navigation, search

In order to avoid custom file management to store mod information, configuration and other data, version 0.4.16 introduced mod storage in the Lua API. It is also known as the mod metadata API and is essentially a key/ value store for mod specific data. A mod can get a StorageRef at initialization time and use that reference to read and write data from and to the mod storage. This storage is local to the mod and should for all intents and purposes be used by that mod only. Great care must be taken making the mods storage a global, as other mods could then potentially write into your mod's storage and break things.

The key value store works similar to NodeMetaRef, with the exception that there is of course no inventory associated to the mod storage since mod metadata does not refer to a specific position in the world. The StorageRef can be gotten at e.g. the top of your init.lua via local mod_storage = minetest.get_mod_storage().

A couple of warnings:

  • Do not call minetest.get_mod_storage() after initialization of the mod, e.g. in functions and callbacks. Such calls will result in returning nil.
  • The storage is truly local to the mod so do not set some piece of data in one mod and then try retrieve it in another mod using that other mod's local storage



local mod_storage = minetest.get_mod_storage()
mod_storage:set_string("motd", "Hello World!")
mod_storage:set_int("retries", 3)
        motd = "Now another message",
        retries = 10,
        mod_feature_x_enabled = "false"

Implementation Notes

The mod storage is stored in the world folder under the mod_storage directory. The file name is the name of the mod. I.e. a mod named some_mod will have a file called some_mod in worlds/yourworld/mod_storage containing the data, e.g.:

{"motd":"Hello World!"}

As an administrator this makes for easy management of mod storage without having to resort to Lua code, but make sure you know what you are doing/ know the mod well whose storage you edit (or you are the mod's author of course). Also be careful editing these files when your server is online as you could potentially break things when both you and the server is trying to write the same file.

Personal tools