I was wondering if anyone knows of a way to fade a user's screen to black once they use an entity for a specific amount of time and then once that time has allotted, then it would fade out to the clear screen they had originally.
[i]Note: I've already tried Player:ScreenFade() and it doesn't appear to work. If you have gotten it to work then I'd be glad if you were to share how you've gotten it to work.[/i]
Just create a black box at (0, 0) with sizes (ScrW(), ScrH()).
Set a local for alpha.
local alpha = 255
and use math.Approach() to go down to 0
I added this to cl_init.lua:
[lua]usermessage.Hook("FadeToBlack", function()
print("Screen faded to black!");
local alpha = 0;
timer.Create("EndFade", 0.1, 0, function()
if (alpha < 255) then
alpha = alpha + 5;
draw.RoundedBox( 2, 0, 0, ScrW(), ScrH(), Color(0, 0, 0, alpha) );
print("Alpha set to "..alpha);
else
timer.Destroy("EndFade");
print("Timer destroyed.");
end
end)
end)[/lua]
And used this when the ENT:Use() function is called in init.lua:
[lua]function ENT:Use(player)
umsg.Start("FadeToBlack", player);
umsg.End();
end[/lua]
The console prints out that everything is working but my screen doesn't fade to black. Any idea?
draw.RoundedBox needs to be placed in a drawing hook such as HUDPaint.
Here's what I've done with the functions:
[b]cl_init.lua[/b]
[lua]function ENT:Think()
if (self:GetNetworkedBool(self:EntIndex().."sFadeIn") == true) then
self:FadeIn();
elseif (self:GetNetworkedBool(self:EntIndex().."sFadeOut") == true) then
self:FadeOut();
end;
end;
function ENT:FadeIn()
self:SetNetworkedBool(self:EntIndex().."sFadeIn", false);
print("Screen is fading to black!");
local alpha = 0;
timer.Create("FadeInTimer - "..self:EntIndex(), 0.01, 0, function()
if (alpha < 255) then
alpha = alpha + 15;
draw.RoundedBox( 2, 0, 0, ScrW(), ScrH(), Color(0, 0, 0, alpha) );
print("Alpha set to "..alpha);
else
timer.Destroy("FadeInTimer - "..self:EntIndex());
print("Timer destroyed.");
end;
end);
end;
hook.Add("HUDPaint", "FadeIn", FadeIn);
function ENT:FadeOut()
self:SetNetworkedBool(self:EntIndex().."sFadeOut", false);
print("Screen is fading to clear!");
local alpha = 255;
timer.Create("FadeOutTimer - "..self:EntIndex(), 0.01, 0, function()
if (alpha > 0) then
alpha = alpha - 15;
draw.RoundedBox( 2, 0, 0, ScrW(), ScrH(), Color(0, 0, 0, alpha) );
print("Alpha set to "..alpha);
else
timer.Destroy("FadeOutTimer - "..self:EntIndex());
print("Timer destroyed.");
end;
end);
end;
hook.Add("HUDPaint", "FadeOut", FadeOut);[/lua]
[b]init.lua[/b]
[lua]function ENT:Use()
self:SetNetworkedBool(self:EntIndex().."sFadeIn", true);
timer.Simple(3.12, function()
self:SetNetworkedBool(self:EntIndex().."sFadeOut", true);
end);
end[/lua]
I've encountered two problems:
1. Doesn't fade to black/clear.
2. After the first use, it doesn't print that it's fading in/out if used again.