How would I make it so my screen fades to black & white upon health loss?
5 replies, posted
I'm a developer for a DarkRP server (yeah man i can code all sorts of jobs just give me ur tc admin and ur server will be custom in no time) and I was wondering if it were possible to have a script that makes it so your screen will slowly loose color depending on health lost?
Eg, if I drop under 20% then it will apply a black and white effect, I imagine it will work similar to drugs in DarkRP.
If anybody can help me out with this I'd be grateful, I'm broke at the minute so I can't really give you anything except from internet points.
(Would it also be possible to have it so when the player spawns, the screen will start with a black and white effect and slowly fade into normal?)
Thanks in advance.
[QUOTE=Cheese Person;42732401][b]I'm a developer for a DarkRP server[/b] (yeah man i can code all sorts of jobs just give me ur tc admin and ur server will be custom in no time) and [b]I was wondering if it were possible to have a script that makes it so your screen will slowly loose color depending on health lost?[/b]
Eg, if I drop under 20% then it will apply a black and white effect, I imagine it will work similar to drugs in DarkRP.
If anybody can help me out with this I'd be grateful, I'm broke at the minute so I can't really give you anything except from internet points.
(Would it also be possible to have it so when the player spawns, the screen will start with a black and white effect and slowly fade into normal?)
Thanks in advance.[/QUOTE]
What kind of 'developer' can't make a simple, fully clientsided script?
[QUOTE=Netheous;42732796]What kind of 'developer' can't make a simple, fully clientsided script?[/QUOTE]
I'm new to lua. All I want it an example so I can learn from it and reconstruct it.
[QUOTE=Cheese Person;42732808]I'm new to lua. All I want it an example so I can learn from it and reconstruct it.[/QUOTE]
Alright then.
Make a local variable for alpha, create a function hooked to HUDPaint, use any of rectangle drawing function from either surface or draw library, create a simple formula for alpha increasement, my suggestion.
[lua]if LocalPlayer():Health() < 20 then
Alpha = Alpha - (12.75 * LocalPlayer():Health())
end[/lua]
sorry for not providing an example thought.
Or DrawColorModify()
[lua] local tab = {}
tab[ "$pp_colour_colour" ] = 0.20
tab[ "$pp_colour_contrast" ] = 1
DrawColorModify( tab )
[/lua]
And modify the color over time.
[QUOTE=Netheous;42732796]What kind of 'developer' can't make a simple, fully clientsided script?[/QUOTE]
Sorry but if you're unable to understand the difference between "fade to black" and "fade to black & white" then you're absolutely not in a position to judge people on what they can or can't do.
[editline]2nd November 2013[/editline]
[QUOTE=Internet1001;42732900]Or DrawColorModify()
[lua] local tab = {}
tab[ "$pp_colour_colour" ] = 0.20
tab[ "$pp_colour_contrast" ] = 1
DrawColorModify( tab )
[/lua]
And modify the color over time.[/QUOTE]
Here's a more detailed example:
[lua]local tab = {
["$pp_colour_addr"] = 0,
["$pp_colour_addg"] = 0,
["$pp_colour_addb"] = 0,
["$pp_colour_brightness"] = 0,
["$pp_colour_contrast"] = 1,
["$pp_colour_colour"] = 1,
["$pp_colour_mulr"] = 1,
["$pp_colour_mulg"] = 1,
["$pp_colour_mulb"] = 1
}
hook.Add("RenderScreenspaceEffects", "PlayerHealthColorMod", function()
local pl = LocalPlayer()
local maxhealth = pl:GetMaxHealth()
local health = pl:Health()
local mul = math.Clamp(math.TimeFraction(maxhealth * 0.2, maxhealth * 0.8, health), 0, 1)
tab["$pp_colour_colour"] = mul
DrawColorModify(tab)
end)
[/lua]
I've used a lot of local variables to make it easier to understand for you, but they're obviously not required. In this example (if it does work, I haven't actually tested it), you will perceive colors as normal as long as you are above 80% health. Then your screen with gradually fade to black and white until you reach 20% or lower.
If you don't know, math.Clamp makes sure a value stays between an interval (between 0 and 1 here), and math.TimeFraction is a very handy function which gives you the "progress" of a value between two bounds. For instance, math.TimeFraction(a, b, c) will return 0 if c is equal to a, 1 if c is equal to b, and a value between 0 and 1 if it's somewhere in between.
RenderScreenspaceEffects is the hook in which all post process effects (such as DrawColorModify) should be rendered. You can see the documentation of DrawColorModify here: [url]http://wiki.garrysmod.com/page/Global/DrawColorModify[/url]
If you want a smooth transition between colors and black & white, you might want to use [url=http://wiki.garrysmod.com/page/math/Approach]math.Approach[/url] and [url=http://wiki.garrysmod.com/page/Global/FrameTime]FrameTime[/url]. The idea is to store the "current color multiplier" in a variable that is local to your script (declare a local variable at the beginning of your script, outside of any function), as well as the "desired color multiplier", update the desired color multiplier according to the local player's health, and then approach the "current color multiplier" towards the "desired color multiplier" using math.Approach with a multiple of FrameTime() as the step. This should ensure a smooth transition even if you take a lot of damage at once.
Sorry, you need to Log In to post a reply to this thread.