From 9ada994d5b3bd874e537b0bc9641b925058bdf51 Mon Sep 17 00:00:00 2001 From: "Y. Wang" Date: Fri, 13 Sep 2024 11:17:36 +0000 Subject: Avoid unnecessarily updating the driver HUD This patch avoids sending the driver HUD if it contains the same text that was previously sent. --- advtrains/trainhud.lua | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/advtrains/trainhud.lua b/advtrains/trainhud.lua index b2744ad..f9f4876 100644 --- a/advtrains/trainhud.lua +++ b/advtrains/trainhud.lua @@ -103,18 +103,20 @@ function advtrains.set_trainhud(name, text, driver) if not player then return end + local drivertext = driver or "" local driverhud = { hud_elem_type = "image", name = "ADVTRAINS_DRIVER", position = {x=0.5, y=1}, offset = {x=0,y=-170}, - text = driver or "", + text = drivertext, alignment = {x=0,y=-1}, - scale = {x=1,y=1},} + scale = {x=1,y=1}, + } if not hud then - hud = {["driver"]={}} + hud = {} advtrains.hud[name] = hud - hud.id = player:hud_add({ + hud.id = player:hud_add { hud_elem_type = "text", name = "ADVTRAINS", number = 0xFFFFFF, @@ -123,18 +125,23 @@ function advtrains.set_trainhud(name, text, driver) text = text, scale = {x=200, y=60}, alignment = {x=0, y=-1}, - }) - hud.oldText=text + } hud.driver = player:hud_add(driverhud) + hud.oldText = text + hud.oldDriver = drivertext else if hud.oldText ~= text then player:hud_change(hud.id, "text", text) hud.oldText=text end if hud.driver then - player:hud_change(hud.driver, "text", driver or "") + if hud.oldDriver ~= drivertext then + player:hud_change(hud.driver, "text", drivertext) + hud.oldDriver = drivertext + end elseif driver then hud.driver = player:hud_add(driverhud) + hud.oldDriver = drivertext end end end -- cgit v1.2.3