minetest.get_mod_storage

From Minetest Developer Wiki
Jump to: navigation, search

Syntax

minetest.get_mod_storage()

Description

Returns the StorageRef of the current mod. This StorageRef can then be used by the mod to store and retrieve information in a similar fashion to NodeMetaRef. The information is persisted, so after a server restart the information is still available.

Example

note.png
Note: This method MUST be called during mod initialization! I.e. put it somewhere so that it runs when the mod is loaded such as at the top of init.lua. Calling get_mod_storage inside functions will return nil.


local mod_storage = minetest.get_mod_storage()
 
minetest.register_chatcommand("set_motd", {
	params = "Message",
	description = "Show message of the day.",
	privs = {server = true},
	func = function(name, param)
		mod_storage:set_string("motd", param)
	end,
})
 
minetest.register_on_joinplayer(function(player)
	local motd = mod_storage:get_string("motd")
	if motd then
		minetest.chat_send_player(player:get_player_name(), motd)
	end
end)


note.png
Note: The StorageRef is local to the mod. When accessing data in the storage, do not assume you can retrieve information that another mod had stored in its own mod storage. To achieve this you will need to make your mod's storage variable a global so that you can access if from another mod, or better still, provide accessor functions.


E.g. the below will not work:

mod_a/init.lua

local mod_storage = minetest.get_mod_storage()
mod_storage:set_string("foo", "bar")


mod_b/init.lua

local mod_storage = minetest.get_mod_storage()
mod_storage:get_string("foo") -- Will return nil
Personal tools
Namespaces

Variants
Actions
Navigation
API
Toolbox