Commit 6012f182 authored by Dirk's avatar Dirk

introduce get_hunger API function + documentation

parent 4f6b59b5
......@@ -61,7 +61,6 @@ This removes 5 hunger points from Foobar’s hunger value with the reason “opt
The function can be used in a globalstep that loops over all players, too.
local timer = 0
......@@ -81,6 +80,17 @@ This globalstep iterates over all players and gives them 1 hunger point every se
Be careful with globalsteps because they can cause server lag.
### … and getting hunger
Modders want to get a player’s current hunger value. Instead of manually determining the hunger value’s attribute and manually retrieving the custom attribute and converting it to a number the function `hunger_ng.get_hunger` exists.
local hunger_value = hunger_ng.get_hunger('Foobar')
print('Foobar’s hunger is at '..hunger_value)
This prints something along “Foobar’s hunger value is at 18.371”. The return value can be used for any type of action. Be aware that the value should not be stored for too long because the actual value will change over time and actions basing on the hunger value might not be exact anymore.
# Configuration
The mod allows a lot of configuration. All of the following attributes are editable either via the advanced configuration within the client or by manually adding the options to `minetest.conf`.
......@@ -93,9 +93,11 @@ end
-- Replace the global table used for easy variable access within the mod with
-- an API-like global table for other mods to utilize.
local add_hunger_data = hunger_ng.functions.add_hunger_data
local alter_hunger = hunger_ng.functions.add_hunger_data
local alter_hunger = hunger_ng.functions.alter_hunger
local get_hunger = hunger_ng.functions.get_hunger
hunger_ng = nil
hunger_ng = {
add_hunger_data = add_hunger_data,
alter_hunger = alter_hunger
alter_hunger = alter_hunger,
get_hunger = get_hunger
......@@ -44,6 +44,21 @@ local debug_log = function (playername, what, old, new, change, reason)
-- Get the current hunger value
-- Gets (Returns) the current hunger value without having to know or quess the
-- player attribute of it. This function is used for the API only since all
-- other functions within the mod already have access to the needed parameters
-- for getting the hunger value without knowing the name of the attribute.
-- @param playername The name of the player whose hunger value is to be get
-- @return number The hunger value
hunger_ng.functions.get_hunger = function (playername)
local player = minetest.get_player_by_name(playername)
return tonumber(player:get_attribute(hunger_attribute))
-- Alter health by given value
-- @param playername The name of a player whose health value should be altered
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment