XeroXipher Posted February 14 Share Posted February 14 Hey guys... I spent the whole day removing the built in GTA SA Hud and rebuilding my own Hud with Health, Armor, Money, Weapon and Time. I used dxDrawRect for the bars and dxDrawText for the Text. I love what it looks like now, but tonight I noticed it will only run for about 15 minutes then freeze and eventually crash. I used: addEventHandler("onClientRender", root, DrawHud) which works well... But as I was reading the Wiki I saw that onClientRender updates every frame so if your 60 or 90FPS it will redraw the Hud 60 to 90 times a second. I think that's why it is crashing. Is there a better way to use dxDrawRect and dxDrawText then onClientRender? Link to comment
Moderators IIYAMA Posted February 14 Moderators Share Posted February 14 15 hours ago, XeroXipher said: I think that's why it is crashing. Is there a better way to use dxDrawRect and dxDrawText then onClientRender? The following functions shouldn't cause crashes: https://wiki.multitheftauto.com/wiki/DxDrawRectangle https://wiki.multitheftauto.com/wiki/DxDrawText But you most likely did crash because of a memory leak. To find the reason I will need to look at your code for that. Just a thing that can lead up to a crash: By putting dxCreateFont/dxCreateTexture/dxCreateScreenSource(everything with Create in it's name) inside of an onClientRender triggered function Link to comment
XeroXipher Posted February 15 Author Share Posted February 15 Hey @IIYAMA, this is my coding around the area: HUD.lua local sx,sy = guiGetScreenSize() local px,py = 1600,900 local x,y = (sx/px), (sy/py) function HUDDrawing() local playerHealth = getElementHealth ( getLocalPlayer() ) local playerArmor = getPedArmor ( getLocalPlayer() ) local playerMoney = getPlayerMoney( getLocalPlayer() ) local time = getRealTime() local hours = time.hour local minutes = time.minute local seconds = time.second local monthday = time.monthday local month = time.month local year = time.year local formattedTime = string.format("%04d-%02d-%02d %02d:%02d", year + 1900, month + 1, monthday, hours, minutes) local Inventory = dxCreateTexture("Inventory.png") dxDrawRectangle(x*1085,y*0,x*1490,y*160, tocolor( 46, 46, 42, 150 )) -- Create our black transparent MOTD background Rectangle. dxDrawRectangle(x*1265,y*0,x*1368,y*36, tocolor( 120, 120, 120, 180), true) dxDrawRectangle(x*1265,y*0,x*1368/400*playerHealth ,y*36, tocolor( 169, 48, 48, 200), true) dxDrawText(" Health: "..math.floor(tonumber(playerHealth)).."%", x*1265, y*0, x*1365, y*36, tocolor(255, 255, 255, 255), 1.10, "default-bold", "center", "center", false, false, true, false, false) dxDrawRectangle(x*1265, y*36, x*1368, y*36, tocolor(120, 120, 120, 180), true) dxDrawRectangle(x*1265, y*36, x*1368/400*playerArmor, y*36, tocolor(24, 60, 176, 200), true) dxDrawText(" Armor: "..math.floor(tonumber(playerArmor)).."%", x*1265, y*36, x*1365, y*74, tocolor(255, 255, 255, 255), 1.10, "default-bold", "center", "center", false, false, true, false, false) dxDrawText(" $"..math.floor(tonumber(playerMoney)).."", x*1300, y*72, x*1268, y*114, tocolor(255, 255, 255, 255), 1.10, "pricedown", "center", "center", false, false, true, false, false) dxDrawRectangle(x*1265,y*72,x*1368, y*36, tocolor( 115, 210, 90, 200), true) dxDrawText("Local Time: "..formattedTime.."", x*1480, y*112, x*1368, y*135, tocolor(255,255,255,255), 0.50, "bankgothic", "center", "center", false, false, true, false, false) dxDrawText("Virox Roleplay", x*9, y*880, x*246, y*890, tocolor(255, 255, 255, 255), 1.00, "pricedown", "center", "center", false, false, true, false, false) -- Your Text Bottom Left end -- attach the event handler to the root element of the resouce -- this means it will only trigger when its own resource is started function moveHUDComponent() setPlayerHudComponentVisible( "weapon", true ) setPlayerHudComponentVisible( "ammo", true ) setPlayerHudComponentProperty("weapon", "position", 950, 0) setPlayerHudComponentProperty("ammo", "position", 1000, 100) end addEventHandler("onClientResourceStart", resourceRoot, moveHUDComponent) addEventHandler("onClientRender", root, HUDDrawing) meta.xml <meta> <script src = "HUD.lua" type = "client" /> </meta> Link to comment
XeroXipher Posted February 15 Author Share Posted February 15 Is it proper practice to QUOTE previous replies? I pinged the Moderator that responded to my post and I got a Hidden RedBox from my post which needs to be approved by Moderators... I didn't abuse anything so I don't know if it was my (@) comment or if it was just a safety precaution. Link to comment
XeroXipher Posted February 15 Author Share Posted February 15 10 hours ago, IIYAMA said: The following functions shouldn't cause crashes: https://wiki.multitheftauto.com/wiki/DxDrawRectangle https://wiki.multitheftauto.com/wiki/DxDrawText But you most likely did crash because of a memory leak. To find the reason I will need to look at your code for that. Just a thing that can lead up to a crash: By putting dxCreateFont/dxCreateTexture/dxCreateScreenSource(everything with Create in it's name) inside of an onClientRender triggered function Hey, sorry, I didn't know if you would get my last post because it was flagged... I don't know how to fix a Memory Sink but I do believe that it what it is... This is: HUD.lua local sx,sy = guiGetScreenSize() local px,py = 1600,900 local x,y = (sx/px), (sy/py) function HUDDrawing() local playerHealth = getElementHealth ( getLocalPlayer() ) local playerArmor = getPedArmor ( getLocalPlayer() ) local playerMoney = getPlayerMoney( getLocalPlayer() ) local time = getRealTime() local hours = time.hour local minutes = time.minute local seconds = time.second local monthday = time.monthday local month = time.month local year = time.year local formattedTime = string.format("%04d-%02d-%02d %02d:%02d", year + 1900, month + 1, monthday, hours, minutes) local Inventory = dxCreateTexture("Inventory.png") dxDrawRectangle(x*1085,y*0,x*1490,y*160, tocolor( 46, 46, 42, 150 )) -- Create our black transparent MOTD background Rectangle. dxDrawRectangle(x*1265,y*0,x*1368,y*36, tocolor( 120, 120, 120, 180), true) dxDrawRectangle(x*1265,y*0,x*1368/400*playerHealth ,y*36, tocolor( 169, 48, 48, 200), true) dxDrawText(" Health: "..math.floor(tonumber(playerHealth)).."%", x*1265, y*0, x*1365, y*36, tocolor(255, 255, 255, 255), 1.10, "default-bold", "center", "center", false, false, true, false, false) dxDrawRectangle(x*1265, y*36, x*1368, y*36, tocolor(120, 120, 120, 180), true) dxDrawRectangle(x*1265, y*36, x*1368/400*playerArmor, y*36, tocolor(24, 60, 176, 200), true) dxDrawText(" Armor: "..math.floor(tonumber(playerArmor)).."%", x*1265, y*36, x*1365, y*74, tocolor(255, 255, 255, 255), 1.10, "default-bold", "center", "center", false, false, true, false, false) dxDrawText(" $"..math.floor(tonumber(playerMoney)).."", x*1300, y*72, x*1268, y*114, tocolor(255, 255, 255, 255), 1.10, "pricedown", "center", "center", false, false, true, false, false) dxDrawRectangle(x*1265,y*72,x*1368, y*36, tocolor( 115, 210, 90, 200), true) dxDrawText("Local Time: "..formattedTime.."", x*1480, y*112, x*1368, y*135, tocolor(255,255,255,255), 0.50, "bankgothic", "center", "center", false, false, true, false, false) dxDrawText("Virox Roleplay", x*9, y*880, x*246, y*890, tocolor(255, 255, 255, 255), 1.00, "pricedown", "center", "center", false, false, true, false, false) -- Your Text Bottom Left end -- attach the event handler to the root element of the resouce -- this means it will only trigger when its own resource is started function moveHUDComponent() setPlayerHudComponentVisible( "weapon", true ) setPlayerHudComponentVisible( "ammo", true ) setPlayerHudComponentProperty("weapon", "position", 950, 0) setPlayerHudComponentProperty("ammo", "position", 1000, 100) end addEventHandler("onClientResourceStart", resourceRoot, moveHUDComponent) addEventHandler("onClientRender", root, HUDDrawing) meta.xml <meta> <script src="HUD.lua" tpye="client" /> </meta> (I am going to delete my previous post, it got flagged, I think for Directly responding to you rather than quoting you.) Link to comment
XeroXipher Posted February 15 Author Share Posted February 15 RESOLVED In the past I have used different languages that ignored whitespace in code... I guess MTA SA runs through the whitespace and I think that's where the memory leak was... I deleted all whitespace (Sometimes I find it easier to read code if there are linebreaks in the code) but I deleted these linebreaks and the client doesn't Freeze/Crasha anymore. Link to comment
justn Posted February 15 Share Posted February 15 8 hours ago, XeroXipher said: RESOLVED In the past I have used different languages that ignored whitespace in code... I guess MTA SA runs through the whitespace and I think that's where the memory leak was... I deleted all whitespace (Sometimes I find it easier to read code if there are linebreaks in the code) but I deleted these linebreaks and the client doesn't Freeze/Crasha anymore. That's not why, in your code you're creating a texture element each time a new frame is rendered, which is why you were experiencing crashes. Create the element outside of the render event. Link to comment
XeroXipher Posted February 15 Author Share Posted February 15 Okay, 2 things, first of all the problem had come back but I uninstalled and reinstalled MTA SA and I have been running the game for several hours without a crash... But how do I render the HUD without onClientRender()? I am willing to adept to a better way. Link to comment
justn Posted February 15 Share Posted February 15 Like i said, just create the texture element outside of the render event (so remove dxCreateTexture line from the HUDDrawing function) and your problem is solved Link to comment
Moderators IIYAMA Posted February 15 Moderators Share Posted February 15 3 hours ago, XeroXipher said: But how do I render the HUD without onClientRender()? As Justn and me mentioned: On 14/02/2025 at 18:34, IIYAMA said: Just a thing that can lead up to a crash: By putting dxCreateFont/dxCreateTexture/dxCreateScreenSource(everything with Create in it's name) inside of an onClientRender triggered function Move this line to the top of your code: 16 hours ago, XeroXipher said: local Inventory = dxCreateTexture("Inventory.png") Like this: local sx,sy = guiGetScreenSize() local px,py = 1600,900 local x,y = (sx/px), (sy/py) local Inventory = dxCreateTexture("Inventory.png") -- just put it here and not inside of HUDDrawing. function HUDDrawing() --[[ ... ]] Link to comment
XeroXipher Posted February 15 Author Share Posted February 15 Okay, thanks for your support : ) 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now