Player attributes: A quick start guideBe warned, for this mod will not show you the basics of modding the game. It is expected that you already know how to write functions, and how to call them.
So, here we go.
About player attributesThey are a data storage system added in Minetest version 0.4.16. If you've got an older version, please open a new tab and download latest Minetest. Or don't. Whatever.
What are the advantages compared to just saving in a file, you might ask?
I shall answer your inquiry:
- No need to manually save them every X seconds; The engine does it for you.
- No need to bother with tons of Lua file edit functions.
- It's probably faster then that new awesome storage function you wrote.
Now that I spent half my life reading the above, can I get the code I can copy-paste?You'll have to bear with me for a while longer. You don't want your brand new code not to work, do you?
So, how does one access the player attributes?
- Grab the player object. Somehow.
- Use set_attribute(attribute_name, value) member function of it to set your attribute. Should be self explaining.
- Use get_attribute(attribute_name) to get the attribute. You should probably place the return value in a variable.
--We'll just register a chat command. It's probably the simplest way.
--I don't really care if it is not the simplest way.
params = "<attribute> <value>",
description = "Set your attribute to a value.",
func = function(name, attribute, value) --Yeah, that is just the player name. Not the object.
local player = minetest.get_player_by_name(name) --Make that name an object!
if not player then --Maybe the chatcommand is not being called by a player?
return false, "Player does not exist!"
player:set_attribute(attribute, value) --Aaand just set the attribute
return true, "Done."