HUD get connected players länge

German
Post Reply
User avatar
niwla23
Member
Posts: 118
Joined: Sat Nov 17, 2018 17:40
In-game: Niwla

HUD get connected players länge

by niwla23 » Post

Hallo,
ich bin gerade dabei einen Kasten zu schreiben (in Lua), der Infos über den Server anzeigt. Ich möchte gerne anzeigen wie viele Spieler online sind. dazu habe ich local b = minetest.get_connected_players oben eingefügt und weiter unten in der HUD definition text = #b, geschrieben um die Länge der Liste und damit die Anzahl der Spieler zu bekommen.
Hier der Code:

Code: Select all

local players_hud_id = {}
local b = minetest.get_connected_players


minetest.register_on_joinplayer(function(player)

   local player_name = player:get_player_name()

   players_hud_id[player_name] = player:hud_add({
     hud_elem_type = "image",
     position  = {x = 0.97, y = 0.5},
     offset    = {x = -220, y = 0},
     text      = "anzeige.png",
     scale     = { x = 1, y = 1},
     alignment = { x = 1, y = 0 },
   })

   players_hud_id[player_name] = player:hud_add({
     hud_elem_type = "text",
     position  = {x = 0.97, y = 0.35},
     offset    = {x = -220, y = 0},
     text      = "MT Citybuild",
     scale     = { x = 100, y = 100},
     alignment = { x = 3, y = 3 },
     number    = 0xFFFFFF,
   })

   players_hud_id[player_name] = player:hud_add({
     hud_elem_type = "text",
     position  = {x = 0.97, y = 0.4},
     offset    = {x = -220, y = 0},
     text      = #b,
     scale     = { x = 100, y = 100},
     alignment = { x = 1, y = 1 },
     number    = 0xFFFFFF,
   })
end)
Aber das will nicht funktionieren:
minetestausgabe.PNG
(39.53 KiB) Not downloaded yet
Ich verstehe das nicht, ist es doch keine Liste? Ich hoffe jemand weiß, was das Problem ist.

User avatar
Krock
Developer
Posts: 4562
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: HUD get connected players länge

by Krock » Post

Du verweist mit "b" zur Funktion "minetest.get_connected_players". Um die Rückgabewerte der Funktion zu erhalten (Spielerliste), musst du folglich die Funktion aufrufen.

Code: Select all

-- Entweder so:
     text      = #b(),
-- Oder mit dem ursprünglichen Funktionsnamen direkt:
     text      = #minetest.get_connected_players(),
PS: Versuch' nicht, "minetest.get_connected_players()" im Hauptcode aufzurufen -> Verweis zu Erklärung viewtopic.php?p=336701#p336701
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
niwla23
Member
Posts: 118
Joined: Sat Nov 17, 2018 17:40
In-game: Niwla

Re: HUD get connected players länge

by niwla23 » Post

Danke!
Ich kriege das mit dem automatischem Update aber nicht hin. Im Minetest Modding book steht da dieser Befehl. Aber ich weiß nicht was bei places,text und places_text ersetzt ewerden muss.(Was ist die ID?)

Code: Select all

local players_hud_id = {}
local b = minetest.get_connected_players


minetest.register_on_joinplayer(function(player)

   local player_name = player:get_player_name()

   players_hud_id[player_name] = player:hud_add({
     hud_elem_type = "image",
     position  = {x = 0.97, y = 0.5},
     offset    = {x = -220, y = 0},
     text      = "anzeige.png",
     scale     = { x = 1, y = 1},
     alignment = { x = 1, y = 0 },
   })

   players_hud_id[player_name] = player:hud_add({
     hud_elem_type = "text",
     position  = {x = 0.97, y = 0.35},
     offset    = {x = -220, y = 0},
     text      = "MT Citybuild",
     scale     = { x = 100, y = 100},
     alignment = { x = 3, y = 3 },
     number    = 0xFFFFFF,
   })

   players_hud_id[player_name] = player:hud_add({
     hud_elem_type = "text",
     position  = {x = 0.97, y = 0.4},
     offset    = {x = -220, y = 0},
     text      = #b(),
     scale     = { x = 100, y = 100},
     alignment = { x = 1, y = 1 },
     number    = 0xFFFFFF,
   })


   minetest.register_on_joinplayer(
   player:hud_change(ids["places"], "text", places_text)
 )

 
end)

User avatar
niwla23
Member
Posts: 118
Joined: Sat Nov 17, 2018 17:40
In-game: Niwla

Re: HUD get connected players länge

by niwla23 » Post

Also hier jetzt nochmal eine neue Version die immer noch nicht funktioniert:

Code: Select all

local players_hud_id = {}
local b = minetest.get_connected_players


minetest.register_on_joinplayer(function(player)

   local player_name = player:get_player_name()

   players_hud_id[player_name] = player:hud_add({
     hud_elem_type = "image",
     position  = {x = 0.97, y = 0.5},
     offset    = {x = -220, y = 0},
     text      = "anzeige.png",
     scale     = { x = 1, y = 1},
     alignment = { x = 1, y = 0 },
   })

   players_hud_id[player_name] = player:hud_add({
     hud_elem_type = "text",
     position  = {x = 0.97, y = 0.35},
     offset    = {x = -220, y = 0},
     text      = "MT Citybuild",
     scale     = { x = 100, y = 100},
     alignment = { x = 3, y = 3 },
     number    = 0xFFFFFF,
   })

   local online = player:hud_add({
     hud_elem_type = "text",
     position  = {x = 0.97, y = 0.4},
     offset    = {x = -220, y = 0},
     text      = #b(),
     scale     = { x = 100, y = 100},
     alignment = { x = 1, y = 1 },
     number    = 0xFFFFFF,
   })


   minetest.register_on_joinplayer(
   player:hud_change(online, "text", #b())

)
end)
Hier die Fehlermeldung:
minetestfehlerhudboolean.PNG
(27.47 KiB) Not downloaded yet
PS: Die fehlermeldung taucht erst auf wenn ein Client joint.
Ich hoffe jemand versteht den fehler.

User avatar
Krock
Developer
Posts: 4562
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: HUD get connected players länge

by Krock » Post

Protip: In deiner `debug.txt` Datei (ganz unten) werden die Fehler detailliert aufgezeichnet. Es wird dir in der Zukunft sehr helfen, das eigentliche Problem zu finden.

Fehler in Zeile 39-42.
1) Du versuchst ein Callback in einem Callback zu registrieren. NOT LIKE THIS. Einfach "minetest.register_on_joinplayer(" und die dazugehörige Klammer unterhalb entfernen
2) Zeile 39: "minetest.register_on_joinplayer" erwartet eine Funktion als erstes Argument. Vergleich' es mal mit Zeile 5 bzw. 43.

Ich glaube, du wolltest stattdessen "minetest.after" verwenden, um das HUD nach einigen Sekunden aktualisieren zu lassen.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
niwla23
Member
Posts: 118
Joined: Sat Nov 17, 2018 17:40
In-game: Niwla

Re: HUD get connected players länge

by niwla23 » Post

Danke, auf minetest.after war ich nicht gekommen, aber ist irgendwie logisch sonst wird es ja nur bei einem Spieler angezeigt.
Ich habe den Code nun nochmal geändert:

Code: Select all


local players_hud_id = {}
local b = minetest.get_connected_players


minetest.register_on_joinplayer(function(player)

  player_name = player:get_player_name()

   players_hud_id[player_name] = player:hud_add({
     hud_elem_type = "image",
     position  = {x = 0.97, y = 0.5},
     offset    = {x = -220, y = 0},
     text      = "anzeige.png",
     scale     = { x = 1, y = 1},
     alignment = { x = 1, y = 0 },
   })

   players_hud_id[player_name] = player:hud_add({
     hud_elem_type = "text",
     position  = {x = 0.97, y = 0.35},
     offset    = {x = -220, y = 0},
     text      = "MT Citybuild",
     scale     = { x = 100, y = 100},
     alignment = { x = 3, y = 3 },
     number    = 0xFFFFFF,
   })

   local online = player:hud_add({
     hud_elem_type = "text",
     position  = {x = 0.97, y = 0.4},
     offset    = {x = -220, y = 0},
     text      = #b(),
     scale     = { x = 100, y = 100},
     alignment = { x = 1, y = 1 },
     number    = 0xFFFFFF,
   })
end)


local player = player_name
minetest.after(10, function(player)

    player:hud_change(online, "text", #b())
end)

So kommt aber diese Meldung:
.__ __ __
_____ |__| ____ _____/ |_ ____ _______/ |_
/ \| |/ \_/ __ \ __\/ __ \ / ___/\ __\
| Y Y \ | | \ ___/| | \ ___/ \___ \ | |
|__|_| /__|___| /\___ >__| \___ >____ > |__|
\/ \/ \/ \/ \/
2018-12-11 16:34:24: ACTION[Main]: World at [C:\Program Files\Minetest\minetest-0.4.17.1-win64\bin\..\worlds\virusbanwelt]
2018-12-11 16:34:24: ACTION[Main]: Server for gameid="minetest" listening on 0.0.0.0:30000.
2018-12-11 16:34:27: ACTION[Server]: Niwla [127.0.0.1] joins game.
2018-12-11 16:34:27: ACTION[Server]: Niwla joins game. List of players: Niwla
2018-12-11 16:34:27: WARNING[Server]: Assignment to undeclared global "player_name" inside a function at ...inetest\minetest-0.4.17.1-win64\bin\..\mods\hud\init.lua:8.
2018-12-11 16:34:33: ACTION[Server]: Niwla places node default:cobble at (41,9,-103)
2018-12-11 16:34:33: ACTION[Server]: Niwla places node default:cobble at (50,9,-114)
2018-12-11 16:34:34: ERROR[Main]: ServerError: AsyncErr: environment_Step: Runtime error from mod 'hud' in callback environment_Step(): ...inetest\minetest-0.4.17.1-win64\bin\..\mods\hud\init.lua:44: attempt to index local 'player' (a nil value)
2018-12-11 16:34:34: ERROR[Main]: stack traceback:
2018-12-11 16:34:34: ERROR[Main]: ...inetest\minetest-0.4.17.1-win64\bin\..\mods\hud\init.lua:44: in function 'func'
2018-12-11 16:34:34: ERROR[Main]: ...\minetest-0.4.17.1-win64\bin\..\builtin\common\after.lua:18: in function <...\minetest-0.4.17.1-win64\bin\..\builtin\common\after.lua:4>
2018-12-11 16:34:34: ERROR[Main]: ...minetest-0.4.17.1-win64\bin\..\builtin\game\register.lua:420: in function <...minetest-0.4.17.1-win64\bin\..\builtin\game\register.lua:400>
2018-12-11 16:34:34: ERROR[Main]: stack traceback:
2018-12-11 16:34:34: ACTION[Server]: Niwla leaves game. List of players:

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests