Skip to content

Instantly share code, notes, and snippets.

@rubenwardy

rubenwardy/hudkit.lua

Last active Aug 29, 2015
Embed
What would you like to do?
HudKit for Minetest
-- HudKit, by rubenwardy
-- License: Either WTFPL or CC0, you can choose.
local function hudkit()
return {
players = {},
add = function(self, player, id, def)
local name = player:get_player_name()
local elements = self.players[name]
if not elements then
self.players[name] = {}
elements = self.players[name]
end
elements[id] = player:hud_add(def)
end,
exists = function(self, player, id)
local elements = self.players[player:get_player_name()]
return elements and elements[id]
end,
change = function(self, player, id, stat, value)
local elements = self.players[player:get_player_name()]
if not elements or not elements[id] then
return false
end
player:hud_change(elements[id], stat, value)
return true
end,
remove = function(self, player, id)
local elements = self.players[player:get_player_name()]
if not elements or not elements[id] then
return false
end
player:hud_remove(elements[id])
elements[id] = nil
return true
end
}
end
local myhudkit = hudkit()
function example(player)
-- Please note, passing a nil player value will cause HudKit
-- to crash (it does not check for that).
local some_data = "abcd"
if not myhudkit:exists(player, "modname:hud_1") then
myhudkit:add(player, "modname:hud_1", {
hud_elem_type = "text",
position = {x = 1, y = 0},
scale = {x = 100, y = 100},
text = some_data,
offset = {x=-100, y = 20}
})
else
myhudkit:change(player, "modname:hud_1", "text", some_data)
end
end
minetest.register_on_leaveplayer(function(player)
myhudkit.players[player:get_player_name()] = nil
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.