Player attributes: A quick start guide

A Tutorial for Minetest

Want to make a nice currency mod? Or an awesome magic casting mod? You'll need a way to store your data per player. Look no further! Here is the

Player attributes: A quick start guide

Be 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 attributes

They 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.
One could, of course, argue that he'd rather have complete control over where his mod writes data; or maybe would like to access the attributes while the player is offline. I will not cover those cases in this short tutorial.

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?
  1. Grab the player object. Somehow.
  2. Use set_attribute(attribute_name, value) member function of it to set your attribute. Should be self explaining.
  3. Use get_attribute(attribute_name) to get the attribute. You should probably place the return value in a variable.
Alright then. We got the basic guideline. Now, you want examples. Examples are nice. Who'd read an article without examples?!
--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.
minetest.register_chatcommand("setAttribute", {
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!"
end
player:set_attribute(attribute, value) --Aaand just set the attribute
return true, "Done."
end,
})
There is no getAttribute chatcommand. Try writing one.

Need more help?

Got stuck on this very very very simple tutorial? Send me a message. Or comment below. I might answer. Or someone else might. It doesn't really matter.

Posts

None found

Embed

Share banner
Image URL:
HTML embed code:
BB embed code:
Markdown embed code:

Credits

Key Authors
lisacvuk
lisacvuk Joined 1y ago
Offline
103 points Ranked 30149th
Author

Submitter

lisacvuk avatar
lisacvuk Joined 1y ago
Offline
103 points Ranked 30149th

lisacvuk
Creator
Sign up to access this!
Sign up to access this!
Sign up to access this!
  • Paypal Donate

Game

Sign up to access this!

Category

Details

Difficulty Level
Intermediate Difficulty

Attributes

Share

  • Share on Reddit
  • Share on Twitter
  • Share on Facebook
  • Share on Google+

Stats

  • 552 Views
  • 0 Posts
  • 1y Submitted
  • 1y Modified

Scores

  • Not yet rated
Sign up to access this!
bcp.crwdcntrl.net tracking pixel