• Dynamic alpha not working properly?
    6 replies, posted
[lua] function StaminaAlertAlpha() local NextRunTime = 0 if ( CurTime() >= NextRunTime ) then NextRunTime = CurTime() + 0.0333 local Stamina = LocalPlayer():GetNWInt( "stamina" ); SAlertAlpha = 0; if SAlertAlpha == 0 then local DoAdd = true; local DoSub = false; elseif SAlertAlpha == 255 then local DoAdd = false; local DoSub = true; end if Stamina <= 40 && Stamina >= 15 then if DoAdd then math.Clamp( SAlertAlpha + 9, 0, 255 ); elseif DoSub then math.Clamp( SAlertAlpha - 9, 0, 255 ); end elseif Stamina < 15 then if DoAdd then math.Clamp( SAlertAlpha + 18, 0, 255 ); elseif DoSub then math.Clamp( SAlertAlpha - 18, 0, 255 ); end else SAlertAlpha = 0; end end end hook.Add( "Think", "StaminaAlertAlpha", StaminaAlertAlpha ) [/lua] Meanwhile in HUDPaint()... [lua] draw.RoundedBox( 6, 190, ScrH()-64, 74, 300, Color(200,10,10,SAlertAlpha) ); [/lua] This thing is supposed to making the small drawn box fade in/out, or pulsate, or whatever you want to call it. However, it will not work. A quick print debug informed me that the function is running properly, so apparently the alpha is not getting set properly. This is all in cl_init.lua and the external values are working fine (i.e. stamina). I'm not sure what I'm doing wrong, but it's probably just a really stupid mistake like a syntax error or something.
Oh yeah, that makes sense. Testing. [editline]09:27AM[/editline] Still isn't working. I guess that fixed one thing but there must be something else wrong.
Just use a sin wave, it's a lot easier to use. [lua]hook.Add("HUDPaint", "AlphaTest", function() local speed = 4 -- how fast to make it tick local tick = ((math.sin(CurTime() * speed)+1)/2) * 255 -- Creates a sin wave that goes from 0 to 255 local col = Color( 13, 200, 12, tick ) draw.RoundedBox(8, 20, 20, 80, 80, col) end)[/lua] Here is a basic example.
Oh, that's clever. Tweaked, saved and loading. [editline]10:40AM[/editline] Not working. I'll look for what I did wrong. Hook 'Alphatrans' Failed: includes/modules/draw.lua:184: attempt to index local 'color' (a nil value) [lua] hook.Add( "HUDPaint", "Alphatrans", function() local Stamina = LocalPlayer():GetNWInt( "stamina" ); local alpha = Color( 200, 10, 10, alpha ); local speed = 1; if Stamina <= 40 && Stamina >= 15 then local dosin = true; local speed = 1; elseif Stamina < 15 then local dosin = true; local speed = 3; else local dosin = false; end if dosin then local alpha = ((math.sin(CurTime() * speed)+1)/2) * 255; -- Creates sin wave from 0 to 255 else end draw.RoundedBox( 6, 190, ScrH()-64, 74, 300, alpha ); end ) [/lua] The reason I'm checking for dosin is because whenever I set speed to 0 so that it does not do anything, it returns an error about a nil value and fails. This is the only way I could think to do it. [editline]10:43AM[/editline] Whoops forgot to replace one of the "color"s with "alpha". Loading to test if it's fixed. [editline]10:45AM[/editline] The colored box is drawn but it is drawn regardless of the value of the stamina and does not have a sin 'pulse'. [editline]10:48AM[/editline] I'll try moving the dosin check to drawing, not the sin function. That's what I should have done. [editline]10:51AM[/editline] Didn't work, but i realized that I have two 'local alpha's being set. Fixed and loading to test. [editline]10:53AM[/editline] Gawd dommit still not working. Debug shows that the local variables are being set appropriately. [editline]10:55AM[/editline] Lol I'm stupid, crisscrossing the local variable names again. ANOTHER LOAD
Many things are wrong with that. In the color variable you have an "alpha" argument which you never set so it is nil. You're creating a local "alpha" variable in "if dosin then" so it only exists in that statement. You're not giving draw.RoundedBox a color for it's color argument, you're giving it a nil value. Here is an example I did using health: [lua]hook.Add("HUDPaint", "AlphaTest", function() local HP = LocalPlayer():Health() local speed = 1 local alpha = 255 if HP <= 40 and HP > 15 then speed = 3 alpha = ((math.sin(CurTime() * speed)+1)/2) * 255 elseif HP <= 15 then speed = 6 alpha = ((math.sin(CurTime() * speed)+1)/2) * 255 end local col = Color( 13, 200, 12, alpha ) draw.RoundedBox(8, 20, 20, 80, 80, col) end)[/lua]
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffuuuuuuuuuuuu [lua] hook.Add( "HUDPaint", "Alphatrans", function() local Stamina = LocalPlayer():GetNWInt( "stamina" ); local alpha = Color( 200, 10, 10, sinalpha ); local speed = 1; local sinalpha = ((math.sin(CurTime() * speed)+1)/2) * 255; -- Creates sin wave from 0 to 255 if Stamina <= 40 && Stamina >= 15 then local dosin = true; local speed = 1; elseif Stamina < 15 then local dosin = true; local speed = 3; else local dosin = false; local speed = 1; end if dosin then draw.RoundedBox( 6, 190, ScrH()-64, 74, 300, alpha ); else end end ) [/lua] [editline]11:05AM[/editline] Disregard the above, I made it without seeing CowThing's latest post. Thanks for the tips, that single post taught me a handful of things. This is what I have and I'm about to test it. [lua] hook.Add( "HUDPaint", "Alphatrans", function() local Stamina = LocalPlayer():GetNWInt( "stamina" ); local speed = 1; local alpha = 255; if Stamina <= 40 && Stamina >= 15 then speed = 1; alpha = ((math.sin(CurTime() * speed)+1)/2) * 255; elseif Stamina < 15 then speed = 3; alpha = ((math.sin(CurTime() * speed)+1)/2) * 255; end local col = Color( 200, 10, 10, sinalpha ); draw.RoundedBox( 6, 190, ScrH()-64, 74, 300, col ); end ) [/lua] [editline]11:14AM[/editline] Woot I think I got it to work. [lua] hook.Add( "HUDPaint", "Alphatrans", function() local Stamina = LocalPlayer():GetNWInt( "stamina" ); local speed = 1; local alpha = 0; if Stamina <= 40 && Stamina >= 15 then speed = 3; alpha = ((math.sin(CurTime() * speed)+1)/2) * 255; elseif Stamina < 15 then speed = 6; alpha = ((math.sin(CurTime() * speed)+1)/2) * 255; end local col = Color( 100, 10, 10, alpha ); draw.RoundedBox( 6, 190, ScrH()-64, 74, 300, col ); end ) [/lua] [editline]11:18AM[/editline] Awesome, it works now I just need to tweak the color/speed. Thanks CowThing. [img]http://www.facepunch.com/image.php?u=80617&dateline=1263322560[/img]:hf:[img]http://www.facepunch.com/image.php?u=179239&dateline=1263709216[/img] [editline]11:22AM[/editline] [IMG]http://imgur.com/Yr5L6.jpg[/IMG] Final code: [lua] hook.Add( "HUDPaint", "Alphatrans", function() local Stamina = LocalPlayer():GetNWInt( "stamina" ); local speed = 1; local alpha = 0; if Stamina <= 40 && Stamina >= 15 then speed = 5; alpha = ((math.sin(CurTime() * speed)+1)/2) * 255; elseif Stamina < 15 then speed = 10; alpha = ((math.sin(CurTime() * speed)+1)/2) * 255; end local col = Color( 255, 0, 0, alpha ); draw.RoundedBox( 6, 190, ScrH()-64, 74, 300, col ); end ) [/lua]
Sorry, you need to Log In to post a reply to this thread.