This mod does some fascinating and advanced things!
I'm trying to understand how updating the hud works, would you mind answering some stupid newbie questions? The only documentation I can find on this at all is in
https://github.com/minetest/minetest/blob/master/doc/lua_api.txtYour phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
minetest.register_globalstep(function ( dtime )
timer = timer + dtime;
if (timer >= 1.0) then
timer = 0;
for _,p in ipairs(minetest.get_connected_players()) do
local name = p:get_player_name();
local h = p:hud_add({
hud_elem_type = "text";
position = {x=0.5, y=0.01};
text = get_time();
number = 0xFFFF00;
scale = 20;
});
if (player_hud[name]) then
p:hud_remove(player_hud[name]);
end
player_hud[name] = h;
end
end
end);
So, you establish one globalstep that will run every cycle. In a multiplayer game this globalstep will run for every single player, right? That's why you specifically loop through all the players to update everyone's huds? And I presume this would be true of any globalstep added by a mod. If an item adds a globalstep, then in multiplayer, that globalstep will run for ALL players unless the globalstep takes specific care to only affect the item that added it.
local h = p:hud_add
hud_add adds a new element to player p's hud. You are adding a text element that will appear relatively half way across the players screen in the x coord, and 0.1 way down the screen in the y coord. scale sets the text size and number assigns the color I would assume?
The h that is returned is the id of new hud element right?
Where I get really confused is the section right after the hud_add.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
if (player_hud[name]) then
p:hud_remove(player_hud[name]);
end
player_hud[name] = h;
player_hud was defined at the top of this lua as blank. So player_hud[name] will be nil on the first check, and will be assigned to h which came out of our hud_add. But then the NEXT time through the loop, player_hud[name] will be defined, and so, for this player, we remove element h.
So, if I'm following, we add the clock text to the hud every cycle. If the clock text was there last cycle as well, we remove the OLD clock text from the hud. I'm guessing that if we didn't do this the new element and old element would overlap each other and after just a few seconds that section of the screen would just be a mess. Is that right?
Is adding a new element and removing the old element more efficient then using hud_change to just change the text element of the previously added item?
This is fascinating code, but if you (or anyone) could help explain to me how it works, I would be very grateful.
Thanks!