• Is it bad to use surface.CreateFont inside HUDPaint?
    7 replies, posted
Surely some of you will want to punch me in the face after reading the title, but hear me out and tell me what I'm doing wrong. Take a look at how the default ammo and health boxes look in gmod. I took the liberty of finding a random video to show this: [url]http://youtu.be/U49KwBKPgP0?t=2m42s[/url] I'm trying to imitate that. Now, I'm fairly new to all this, but I'm pretty sure there is no function that creates a box filled with text exactly like this, which updates with the same animation style. I figured the only way to imitate that is to draw the text twice: once for the blur, and once for the readable part. Maybe more than one blur, actually. The problem is that the blur isn't static - it changes practically every frame! Am I expected to make dozens of fonts, using the right one each time in HUDPaint? Either way, my code currently looks like this: [lua]hook.Add( "HUDPaint" , "Draw Turn Bank Score" , function() local x = ScrW() - 110 local y = ScrH() - 70 local width = 100 local height = 60 local xmid = x + width/2 local timeSinceLastUpdate = UnPredictedCurTime() - lastUpdate local boldness = math.max(0, 127 - (100 * timeSinceLastUpdate) ) surface.CreateFont( "WarboxScoreBlur" , { size = height, blursize = math.Clamp( 10 * (3 - timeSinceLastUpdate) , 0 , 10), scanlines = 2 } ) draw.RoundedBox( 6 , x , y , width , height , Color( 240 , 200 , 100 , boldness ) ) draw.DrawText( bankValue , "WarboxScoreBlur" , xmid , y , Color(240,200,100) , TEXT_ALIGN_CENTER ) draw.DrawText( bankValue , "WarboxScoreFont" , xmid , y , Color(240,200,100) , TEXT_ALIGN_CENTER ) end )[/lua] And it DOES work (although I haven't tweaked the font settings to match perfectly yet), except occasionally the player client crashes. Yay. I figured it's most likely because I'm overusing CreateFont. Am I right? What's the CORRECT way to do this? Edit: I should note that I'm hiding the actual ammo box and re-purposing the area. Edit Edit: this is where I define the other font: [lua]function GM:Initialize() surface.CreateFont( "WarboxScoreFont" , { size = 60 } ) end[/lua]
You never want to call surface.CreateFont inside HUDPaint. You still want in in the Clientside to create it, just not in HUDPaint.
[QUOTE=Kozmic;46998689]You never want to call surface.CreateFont inside HUDPaint. You still want in in the Clientside to create it, just not in HUDPaint.[/QUOTE] Then where? Every frame? Interval?
[QUOTE=Neat-Nit;46998698]Then where? Every frame? Interval?[/QUOTE] call it once in the script. if you call it multiple times, it takes up memory. usually when you refresh lua alot, it will take up some memory, but it's so small it would take [i]lots[/i] of lua refreshes to start hogging memory, depending on your RAM. but when you call it [i]very[/i] frequently, like in HUDPaint, it will slowly kill your garry's mod and slow down your pc for a bit. learned this the hard way. example: [lua] surface.CreateFont( ... ) hook.Add( "HUDPaint", ..., function() ... end ) [/lua]
[QUOTE=mitterdoo;46998782]call it once in the script.[/QUOTE] The problem is that I want it to change during the script.
[QUOTE=Neat-Nit;46998796]The problem is that I want it to change during the script.[/QUOTE] if you want to use multiple fonts in the code, you're going to have to create them all separately. if you're making some sort of 'enter your font here' thing, make sure the font is being made once.
Okay, I solved it twice. If anyone comes across this and wants to know how, here: 1. To have different blur sizes, define them inside a for loop and append the blur sizes in the name. ("fontWithBlur"..blursize) When you use the fonts, just append the desired blur size in the font name in the same manner. 2. The ammo blur thing is actually of a single size and just fades out... So I only need two fonts in the end: one solid and one blurred. Serves me right for coding at 5 AM.
Here's a basic guide I'm writing on a few of the biggest mistakes made and how not to and why it isn't good to make them... [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/optimization/_generalized_optimization_guide.lua.html[/url]
Sorry, you need to Log In to post a reply to this thread.