I need help with the HUD API. Help a fellow noob.

Post Reply
ChristienChapman
Member
Posts: 117
Joined: Sat May 28, 2022 00:04

I need help with the HUD API. Help a fellow noob.

by ChristienChapman » Post

I am a noob with the HUD api. I need help with removing the hud when called to. I need to be able to clear the screen when the player moves to another menu. Anyone who helps with this will be credited in my project. Thanks!


Code: Select all

-- All glory to God, Jesus Christ, and the Holy Spirit.


dofile(minetest.get_modpath("minespy") .. "/services/points.lua")

dofile(minetest.get_modpath("minespy") .. "/services/records.lua")

dofile(minetest.get_modpath("minespy") .. "/extra_content/contentTheSwarm.lua")

dofile(minetest.get_modpath("minespy") .. "/extra_content/contentColdOrigins.lua")

dofile(minetest.get_modpath("minespy") .. "/dashboard/ms.lua")

minespyMemory = ""




function hud_addElementFull(player, element)

dir = element

local removal_id = tostring(player) .. element .. ".deny"
minespyMemory = tostring(player) .. element .. ".active"



local id = player:hud_add({
			hud_elem_type = "image",
			position = {x = 1, y = 1},
			scale = {x = 2.6, y = 2.6},
		alignment = { x = -0.5, y = -0.7},
			text = dir .. ".png",
			offset = {x=-262, y = -103},
			number = 0xFFFFFF

		})



while minespyMemory == removal_id do
player:hud_remove(id)
end



	end










		








minetest.register_chatcommand("dashboard", {
privs = {
interact = true
},
func = function(playername)

local player = minetest.get_player_by_name(playername)

hud_addElementFull(player, "dashboard")



return true
end,
})



minetest.register_chatcommand("dashboarde", {
privs = {
interact = true
},
func = function(playername)

local player = minetest.get_player_by_name(playername)
minespyMemory = tostring(player) .. "dashboard.deny"




return true
end,
})

















God bless you.

List of releases:
Minetest Zombies Minigame - viewtopic.php?f=9&t=28442&p=412633#p412633

- > cdb_1d60e1a03f83

User avatar
Blockhead
Member
Posts: 1623
Joined: Wed Jul 17, 2019 10:14
GitHub: Montandalar
IRC: Blockhead256
In-game: Blockhead Blockhead256
Location: Land Down Under
Contact:

Re: I need help with the HUD API. Help a fellow noob.

by Blockhead » Post

First and foremost -- indent your code properly, and don't include so many contiguous blank lines! This got about 5x easier to read once I fixed the indentation.

The important lesson to learn here is (once again) you can't hold information about multiple players in just a single array, you need a table, and that table needs setup and teardown. Also, you need to actually have an entirely separate function for deletions, not sure why you put what looked like the start of that function in the same block as the creation.

I've refactored it with the changes noted at the bottom of the file. Not tested. This should track HUDs per element per player. It also lets you register new elements other than the one you had included. Of course, this will only display static HUDs, you would need more complicated logic to show dynamic ones.

Please *do* ask more questions, so that this is a learning exercise and not just a "fix this plz" thread. Thanks.

Code: Select all

-- All glory to God, Jesus Christ, and the Holy Spirit.


dofile(minetest.get_modpath("minespy") .. "/services/points.lua")
dofile(minetest.get_modpath("minespy") .. "/services/records.lua")
dofile(minetest.get_modpath("minespy") .. "/extra_content/contentTheSwarm.lua")
dofile(minetest.get_modpath("minespy") .. "/extra_content/contentColdOrigins.lua")
dofile(minetest.get_modpath("minespy") .. "/dashboard/ms.lua")

local hudIds = {}
local hudElements

local function hud_registerElement(name, def)
    hudElements[element] = def
end

hud_registerElement("dashboard", {
        hud_elem_type = "image",
        position = {x = 1, y = 1},
        scale = {x = 2.6, y = 2.6},
        z_index = 1000,
        alignment = { x = -0.5, y = -0.7},
        text = element .. ".png",
        offset = {x=-262, y = -103},
        number = 0xFFFFFF
})

local function hud_addElementFull(playername, element)
    local element = table.copy(hudElements[element])
    assert(element, "No such element registered: " .. tostring(element))
    element.text = element .. ".png"

    local player = minetest.get_player_by_name(playername)
    local id = player:hud_add(element)

    if not id then return false

    hudIds[player][element] = id
    return true
end

local function hud_removeElementFull(playername, element)
    local player = minetest.get_player_by_name(playername)
    player:hud_remove(hudIds[playername][element])
    return true
end

minetest.register_chatcommand("dashboard", {
    privs = { interact = true },
    func = function(playername)
        if hud_addElementFull(playername, "dashboard") then
            return true
        else
            return false, "Couldn't set up your dashboard!"
        end
    end,
})

minetest.register_chatcommand("dashboarde", {
    privs = { interact = true },
    func = function(playername)
        return hud_removeElementFull(playername, "dashboard")
    end,
})

minetest.register_on_joinplayer(function(player)
    hudIds[player:get_player_name()] = {}
end)

minetest.register_on_leaveplayer(function(player)
    hudIds[player:get_player_name()] = nil
end)

-- API export code - for using the functions outside this file
minespy = minespy or {}
minespy.hud_addElementFull = hud_addElementFull
minespy.hud_removeElementFull = hud_removeElementFull
minespy.hud_registerElement = hud_registerElement

-- Changes by Blockhead
-- Indented the code properly
-- Replaced 'minespyMemory' with a local table indexed by player names
-- Added an on_leaveplayer and on_joinplayer to setup and clear HUDs from the
-- table respectively
-- Replaced tostring(player) with player:get_player_name() where appropriate
-- Set the z-index of the HUD to 1000 so it would render on top of things by
-- convention.
-- Added API export code block
-- Made an function to register HUDs with a given element name as kind of
-- templates.
/˳˳_˳˳]_[˳˳_˳˳]_[˳˳_˳˳\ Advtrains enthusiast | My map: Noah's Railyard | My Content on ContentDB ✝️♂

ChristienChapman
Member
Posts: 117
Joined: Sat May 28, 2022 00:04

Re: I need help with the HUD API. Help a fellow noob.

by ChristienChapman » Post

Troll noob strikes again...

So I fixed up a few errors but I probably messed up something along the way! I keep getting table is nil. Sorry to bother you but maybe show me how to understand the table thing without providing a direct solution as pertains to this hud api and I might be able to fix this myself. (Learning experience...)

Code: Select all

-- All glory to God, Jesus Christ, and the Holy Spirit.


dofile(minetest.get_modpath("minespy") .. "/services/points.lua")
dofile(minetest.get_modpath("minespy") .. "/services/records.lua")
dofile(minetest.get_modpath("minespy") .. "/extra_content/contentTheSwarm.lua")
dofile(minetest.get_modpath("minespy") .. "/extra_content/contentColdOrigins.lua")
dofile(minetest.get_modpath("minespy") .. "/dashboard/ms.lua")

local hudIds = {}
local hudElements = {}

for i=1, 1000 do
      hudElements[i] = "null"
    end

local function hud_registerElement(name, def)
    hudElements[def] = def


end

hud_registerElement("dashboard", {
        hud_elem_type = "image",
        position = {x = 1, y = 1},
        scale = {x = 2.6, y = 2.6},
        z_index = 1000,
        alignment = { x = -0.5, y = -0.7},
        text = "dashboard" .. ".png",
        offset = {x=-262, y = -103},
        number = 0xFFFFFF
})











local function hud_addElementFull(playername, element)
    local element = table.copy(hudElements[element])
    assert(element, "No such element registered: " .. tostring(element))
    element.text = element .. ".png"

    local player = minetest.get_player_by_name(playername)
    local id = player:hud_add(element)

    if not id then return false

else

    hudIds[player][element] = id
    return true
end
end

local function hud_removeElementFull(playername, element)
    local player = minetest.get_player_by_name(playername)
    player:hud_remove(hudIds[playername][element])
    return true
end

minetest.register_chatcommand("dashboard", {
    privs = { interact = true },
    func = function(playername)

        if hud_addElementFull(playername, "dashboard") then
            return true
        else
            return false, "Couldn't set up your dashboard!"
        end
    end,
})

minetest.register_chatcommand("dashboarde", {
    privs = { interact = true },
    func = function(playername)
        return hud_removeElementFull(playername, "dashboard")
    end,
})

minetest.register_on_joinplayer(function(player)
    hudIds[player:get_player_name()] = {}
end)

minetest.register_on_leaveplayer(function(player)
    hudIds[player:get_player_name()] = nil
end)

-- API export code - for using the functions outside this file
minespy = minespy or {}
minespy.hud_addElementFull = hud_addElementFull
minespy.hud_removeElementFull = hud_removeElementFull
minespy.hud_registerElement = hud_registerElement

-- Changes by Blockhead
-- Indented the code properly
-- Replaced 'minespyMemory' with a local table indexed by player names
-- Added an on_leaveplayer and on_joinplayer to setup and clear HUDs from the
-- table respectively
-- Replaced tostring(player) with player:get_player_name() where appropriate
-- Set the z-index of the HUD to 1000 so it would render on top of things by
-- convention.
-- Added API export code block
-- Made an function to register HUDs with a given element name as kind of
-- templates.
God bless you.

List of releases:
Minetest Zombies Minigame - viewtopic.php?f=9&t=28442&p=412633#p412633

- > cdb_1d60e1a03f83

Peril
Member
Posts: 40
Joined: Sun Feb 10, 2019 14:11

Re: I need help with the HUD API. Help a fellow noob.

by Peril » Post

Have you... included the correct dependencies?

That was my experience with Thirsty and Ethereal and getting the table is nil. If you're calling on stuff that is not in your mod, you need to have it as an optional dependency.
Forgot my old username/email, it's been a number of years please bear with.
Stats: OS: Manjaro Linux; Using minetest-git via AUR

ChristienChapman
Member
Posts: 117
Joined: Sat May 28, 2022 00:04

Re: I need help with the HUD API. Help a fellow noob.

by ChristienChapman » Post

I tinkered with Rebemwardy vote hudkit and there is now a fully functioning hud add and remove for each player. There is only one problem, if you send the same hud request twice, then clear, it crashes. This is a major gamebreaking bug that I need help with. Will send the code soon. Whoever helps will be credited, almost done with the bulk of this project.
God bless you.

List of releases:
Minetest Zombies Minigame - viewtopic.php?f=9&t=28442&p=412633#p412633

- > cdb_1d60e1a03f83

ChristienChapman
Member
Posts: 117
Joined: Sat May 28, 2022 00:04

Re: I need help with the HUD API. Help a fellow noob.

by ChristienChapman » Post

Okay here is the code. It works perfectly fine, but, if you were to type /dashboard twice and then /clean, one HUD element will linger on the screen while the other is cleared. If you type /clean again Minetest will crash. By the way, if you were to open the other menus, everything would work just fin. The bug only occurs when you open the same menu twice in a row and then run /clean. Any help on this specific problem would be great and you will receive an instant credit in my project. Sorry I have to ask help all of the time. However, once this one issue is solved, there will not be any more questions on this topic. Thanks and God bless!



init.lua:

Code: Select all

-- All glory to God, Jesus Christ, and the Holy Spirit.

minespy = {}

dofile(minetest.get_modpath("minespy") .. "/services/points.lua")
dofile(minetest.get_modpath("minespy") .. "/services/records.lua")
dofile(minetest.get_modpath("minespy") .. "/extra_content/contentTheSwarm.lua")
dofile(minetest.get_modpath("minespy") .. "/extra_content/contentColdOrigins.lua")
dofile(minetest.get_modpath("minespy") .. "/dashboard/ms.lua")


hudkit_ms = dofile(minetest.get_modpath("minespy") .. "/hudkit.lua")
minespy.hud = hudkit_ms()

minetest.register_chatcommand("dashboard", {
    privs = { interact = true },
    func = function(playername)


dir = "dashboard.png"
        minespy.hud:add(playername, "dashboard", {
		hud_elem_type = "image",
		position = {x = 1, y = 1},
			scale = {x = 2.6, y = 2.6},
		alignment = { x = -0.5, y = -0.7},
			text = dir,
			offset = {x=-262, y = -103},
number = 0xFFFFFF
		})






    end,
})






minetest.register_chatcommand("profile", {
    privs = { interact = true },
    func = function(playername)


dir = "dashboard_profile_menu.png"
        minespy.hud:add(playername, "dashboard_profile_menu", {
		hud_elem_type = "image",
		position = {x = 1, y = 1},
			scale = {x = 2.6, y = 2.6},
		alignment = { x = -0.5, y = -0.7},
			text = dir,
			offset = {x=-262, y = -103},
number = 0xFFFFFF
		})






    end,
})


















minetest.register_chatcommand("clean", {
    privs = { interact = true },
    func = function(playername)


clean(playername)






    end,
})





function clean(playername, type)

        


if(type == "dashboard")
then
minespy.hud:remove(playername, "dashboard")
end
minespy.hud:remove(playername, "dashboard")
minespy.hud:remove(playername, "dashboard_profile_menu")




    end




	





hudkit.lua:


Code: Select all

-- HudKit, by rubenwardy
-- License: Either WTFPL or CC0, you can choose.

local function hudkit_ms()
	return {
		players = {},

		add = function(self, player, id, def)
			name = minetest.get_player_by_name(player)
			elements = self.players[name]

			if not elements then
				self.players[name] = {}
				elements = self.players[name]
			end

			elements[id] = name:hud_add(def)
		end,

		exists = function(self, player, id)
		name = minetest.get_player_by_name(player)
			local elements = self.players[name: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, def)
			name = minetest.get_player_by_name(player)
			elements = self.players[name]

			if not elements then
				self.players[name] = {}
				elements = self.players[name]
			end

			elements[id] = name:hud_remove(elements[id])
		end,
	}
end

return hudkit_ms
God bless you.

List of releases:
Minetest Zombies Minigame - viewtopic.php?f=9&t=28442&p=412633#p412633

- > cdb_1d60e1a03f83

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests