Please help with clearing huds

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

Please help with clearing huds

by ChristienChapman » Post

I have modified the Rubenwardy hudkit for a new dashboard system in Minetest. So far it is going really well. The dashboard is able to access a player's profile, tell the viewer their name, how many points they have, their best round in-game, and soon what DLC they own. Only problem is that when you run /clean to clear all hud elements, it will crash. Two things are causing this. an index for the player is being cleared of an element but not accounting for duplicate elements or elements which have not been called yet. Anyone who can help fix this will be credited. Thanks and God bless you!

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

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("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




	









dashboard/ms.lua

Code: Select all

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








function getPoints(player)

local value = tonumber(points.get(minetest.get_player_by_name(player)))


return value

end















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
		})

        minespy.hud:add(playername, "dashboard_profile_points", {
		hud_elem_type = "text",
		position = {x = 1, y = 1},
			scale = {x = 2.6, y = 2.6},
		alignment = { x = -0.5, y = -0.7},
			text = "Points: " .. tostring(getPoints(playername)),
			offset = {x=-1063, y = -425},
number = 0xFFFFFF
		})





minespy.hud:add(playername, "dashboard_profile_best_round", {
		hud_elem_type = "text",
		position = {x = 1, y = 1},
			scale = {x = 2.6, y = 2.6},
		alignment = { x = -0.5, y = -0.7},
			text = "Best round: " .. tostring(getPoints(playername)) .. " " .. "getMap",
			offset = {x=-1042, y = -404},
number = 0xFFFFFF
		})





minespy.hud:add(playername, "dashboard_profile_name", {
		hud_elem_type = "text",
		position = {x = 1, y = 1},
			scale = {x = 2.6, y = 2.6},
		alignment = { x = -0.5, y = -0.7},
			text = playername,
			offset = {x=-847, y = -489},
number = 0xFFFFFF
		})














minespy.hud:add(playername, "dashboard_profile_owns", {
		hud_elem_type = "text",
		position = {x = 1, y = 1},
			scale = {x = 2.6, y = 2.6},
		alignment = { x = -0.5, y = -0.7},
			text = "Owns:",
			offset = {x=-1077, y = -353},
number = 0xFFFFFF
		})










dir = "ex1.png"
        minespy.hud:add(playername, "dashboard_profile_ex1", {
		hud_elem_type = "image",
		position = {x = 1, y = 1},
			scale = {x = 0.5, y = 0.5},
		alignment = { x = -0.5, y = -0.7},
			text = dir,
			offset = {x=-946, y = -234},
number = 0xFFFFFF

})




dir = "ex1.png"
        minespy.hud:add(playername, "dashboard_profile_ex2", {
		hud_elem_type = "image",
		position = {x = 1, y = 1},
			scale = {x = 0.5, y = 0.5},
		alignment = { x = -0.5, y = -0.7},
			text = dir,
			offset = {x=-546, y = -234},
number = 0xFFFFFF

})






dir = "ex1.png"
        minespy.hud:add(playername, "dashboard_profile_ex3", {
		hud_elem_type = "image",
		position = {x = 1, y = 1},
			scale = {x = 0.5, y = 0.5},
		alignment = { x = -0.5, y = -0.7},
			text = dir,
			offset = {x=-146, y = -234},
number = 0xFFFFFF

})







end,

})




		



















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


dir = "dashboard_about_menu.png"
        minespy.hud:add(playername, "dashboard_about_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("store", {
    privs = { interact = true },
    func = function(playername)


dir = "dashboard_store_menu.png"
        minespy.hud:add(playername, "dashboard_store_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,
})








function getProfile(player)




end





God bless you.

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

- > cdb_1d60e1a03f83

User avatar
Andrey01
Member
Posts: 2577
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: Please help with clearing huds

by Andrey01 » Post

The first things which I find strange in your code are you name varyables wrong. For example, in add, exists and remove methods you should write local player = minetest.get_player_by_name(name) because you get the playerref, not string name.Then you index self.players in one spots by names, in other by player userdatas. That`s certainly error-prone. You should index it by playernames.

Also, remove method should return nil in case if self.players[name] for a particular player doesn't exist since it means there are no any huds for him, i.e. the code should look like the following way:

Code: Select all

remove = function(self, name, id, def)
    local elements = self.players[name]
    
    if not elements then
        return
    end

    if not elements[id] then
        return
    end

    local player = minetest.get_player_by_name(name)
    player:hud_remove(elements[id])
end

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

Re: Please help with clearing huds

by ChristienChapman » Post

Thanks for the help. I removed anything related to hudkits. I had some UI work going and working but I don't have the time to work on the hud glitches. Thanks for the help again...
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: No registered users and 11 guests