• Lua local number incremental increase and decrease.
    15 replies, posted
Thank you for helping if you can, I just have need of a simple bit of code. I need it to change the local setnumber variable incrementally to increase when the console key is turned on, and decrease when it is turned off. I don't know 'lua-math' that well, so any help is appreciated. [B]Main code has been omitted from this, for the sake of just answering my question.[/B] [code] local togglescript = true local startnumber = 0 local endnumber = 3 if togglescript then local setnumber = startnumber <insert math here> endnumber //Decrease to 0 when off. else local setnumber = startnumber <insert math here> endnumber //Increase to 3 when on. [/code]
Please be clearer about what you're trying to do, it will change the code you need a lot.
[QUOTE=Crazy Quebec;22526900]Please be clearer about what you're trying to do, it will change the code you need a lot.[/QUOTE] I just need to define 2 local variables, 0 and 3. I [B]ALREADY[/B] Have the code to toggle the console command myself, that I did not show in the preview, for the sake of keeping the clutter out, all I need to know is the lua mathmatical arithmetic that I need to put into the script to [b]make the value slowly increase to 3, and decrease from 3 to 0 when the console variable is off. [/b] If you insist on seeing the code, here is a pastebin of it. [URL]http://pastebin.com/W3Pe9rtf[/URL]
Decrease and increase when? Every second? Every think? Anytime the console command is on/off? Anyway here's the lazy version to do it : [b][url=http://wiki.garrysmod.com/?title=Math.Approach]Math.Approach [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] Seeing your code you might want to include a time delay in that think. Also instead of adding and removing it continually you can just check for a condition at the top.
The code doesn't make sense? [lua]local togglescript = true ... hook.Add("Think", "toggle_outline_script", togglescript)[/lua] How about you just tell what you need this for, because I really don't see what you want to do.
[QUOTE=Crazy Quebec;22527018]Decrease and increase when? Every second? Every think? Anytime the console command is on/off? Anyway here's the lazy version to do it : [B][URL="http://wiki.garrysmod.com/?title=Math.Approach"]Math.Approach [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B] Seeing your code you might want to include a time delay in that think. Also instead of adding and removing it continually you can just check for a condition at the top.[/QUOTE] I seem to be encountering problems.. [B]lua\includes\extensions\math.lua:203: attempt to compare nil with number[/B] Here is a copy of the code. [URL]http://pastebin.com/bGzuTvwC[/URL] [editline]06:59AM[/editline] [QUOTE=_Kilburn;22527100]The code doesn't make sense? local togglescript = true ... hook.Add("Think", "toggle_outline_script", togglescript) How about you just tell what you need this for, because I really don't see what you want to do.[/QUOTE] Its purpose is to visually make the bordered outline stencil effect that I have, incrementally get larger and larger, like its fading into existance, then when turned off via console command, which works already, it would slowly fade out with a slowly decreasing size. Also, read the pastebin to see the entire header code, the rest of it is private code, but I want this header to basically control the incremental value. [url]http://pastebin.com/bGzuTvwC[/url]
[QUOTE=Colton Rappe;22527135]I seem to be encountering problems.. [b]lua\includes\extensions\math.lua:203: attempt to compare nil with number[/b] Here is a copy of the code. [url]http://pastebin.com/bGzuTvwC[/url][/QUOTE] That's not how programming works... There is no magic function which magically increases or decreases a variable over time, because everything inside of a block of code is executed at the same instant. If you make a block of code extremely long to execute, it would just freeze your game while the code is running. Here is something that looks like what you might need. [lua]local togglescript = true local startnumber = 0 local endnumber = 3 local setnumber = startnumber local last_time = CurTime() local increase_rate = 1 local decrease_rate = 1 hook.Add("Think", "toggle_script_progress", function() if togglescript then -- decrease setnumber = setnumber - decrease_rate * (CurTime() - last_time) if setnumber < startnumber then setnumber = startnumber end else -- increase setnumber = setnumber + increase_rate * (CurTime() - last_time) if setnumber > endnumber then setnumber = endnumber end end last_time = CurTime() end) concommand.Add("toggle_outline_script", function() togglescript = not togglescript end)[/lua] The only thing the concommand does is toggle the togglescript variable. The "Think" hook does the rest. "Think" hooks are automatically executed every time the game "thinks", so basically, all the time, with a very short delay between each execution. That's quite convenient for running continuous stuff. I'm using the variable last_time to calculate how much time elapsed between each execution. As a result, you only need to modify increase_rate and decrease_rate to change the speed at which your variable is incrementing or decrementing. With a value of 1, it takes 1 second to go from 0 to 1. [editline]09:05AM[/editline] [QUOTE=Colton Rappe;22527135]Its purpose is to visually make the bordered outline stencil effect that I have, incrementally get larger and larger, like its fading into existance, then when turned off via console command, which works already, it would slowly fade out with a slowly decreasing size.[/QUOTE] Oh, well that's even simpler, hold on...
Brilliant! Thank you. That makes much more sense! I'm still up for alternatives, and or, I still have a couple uses for that math, like a dynamic light size, among other things.
[lua] local time_to_fadein = 1 local time_to_fadeout = 1 local next_fadein local next_fadeout concommand.Add("toggle_outline_script", function() togglescript = not togglescript if togglescript then next_fadein = CurTime() + time_to_fadein next_fadeout = nil else next_fadein = nil next_fadeout = CurTime() + time_to_fadeout end end) function your_stencil_function() local scale if togglescript then -- fading out if next_fadeout and CurTime() < next_fadeout then scale = (next_fadeout - CurTime()) / time_to_fadeout else next_fadeout = nil scale = 0 end else -- fading in if next_fadein and CurTime() < next_fadein then scale = 1 - ((next_fadein - CurTime()) / time_to_fadein) else next_fadein = nil scale = 1 end end -- add your stuff here end[/lua] Basically, what the console command does here is tell WHEN to fully fade your stencil effect in or out. CurTime() is basically the current time, it holds an arbitrary value that you don't really need to care about. The interesting aspect of it is that you can store its value in a variable, and then compare that value with the new current time, so basically, you can know how much time elapsed since that last time. When I turn the effect on, it sets next_fadein to CurTime() + 1, this means "the current time + 1 second". In the function where you should draw your stencil stuff, if next_fadein exists (if it has been defined and its value isn't nil) and if the current time hasn't reached it yet, you should do the fading math. It's really nothing complicated at all, just a matter of substracting and dividing. Same goes for fading out. After that bunch of code, you should have a variable called "scale" which is between 0 and 1. Multiply it by 255, and you have a value between 0 and 255, perfect for transparency. Same goes for the size of your effect. [editline]09:33AM[/editline] If you're having trouble understanding exactly how CurTime() works and how you can use it for that kind of stuff: [img]http://uppix.net/0/f/b/7ca46e6b0390589a53ba12a5bdec2.png[/img] This is the moment when the fading in starts. CurTime() gives you a certain value you don't care much about. next_fadein is equal to CurTime() + time_to_fadein. Obviously, next_fadein - CurTime() is then equal to time_to_fadein. Divide that by time_to_fadein, and you get 1. 1 - 1 = 0. You have just started fading in, the effect is invisible yet. [img]http://uppix.net/a/6/2/ed84425a1ad679ae2ff2a4d4cdf6a.png[/img] You are exactly halfway fading in. CurTime() moved forwards since then, but next_fadein is fixed, it doesn't change at all. next_fadein - CurTime() is then equal to time_to_fadein / 2. Divide that by time_to_fadein, and you get 0.5. 1 - 0.5 = 0.5. You can see the effect, but it's still half transparent. [img]http://uppix.net/a/e/c/098615d112b30b1930061cb35ccf0.png[/img] This is the exact moment when you are done fading in, we'll say CurTime() is equal to next_fadein (that actually won't happen, or if it does, very rarely). next_fadein - CurTime() is equal to 0, divide that by time_to_fadein, and you still have 0, 1 - 0 = 1. Your effect is fully visible! Then, if CurTime() is higher than next_fadein, you don't care anymore, because you know that at this point, you don't need to do any fading anymore.
ARGH, it hates me, i honstly give up, it wont do what I want it to do, it wont progressively change from 0 to 9 and backwards, here is a paste of a the entire function, its only part of the overall script, but I need this part most of all to actually FADE IN, im tired as heck, and this is only bringing me more and more frustration. [B]IF YOU CAN FIX IT[/B] please for god sake, i beg of you. It wont work and I don't know why, let alone, im still only a beginner to lua script. [I]It would see that even if I tell it to multiply the scale by 9, so it fades from 0 to 9, it wont, and consistently replies back as a value of 1, I dont think this even has a '"Think"' to it like a hook does, to reiterate it, so it might not even be updating the value at all. Any help is appreciated.[/I] [code] local time_to_fadein = 5 local time_to_fadeout = 5 local next_fadein local next_fadeout concommand.Add("toggle_outline_script", function() togglescript = not togglescript if togglescript then next_fadein = CurTime() + time_to_fadein next_fadeout = nil else next_fadein = nil next_fadeout = CurTime() + time_to_fadeout end end) ////////Player Brightness////////// local matOutline = CreateMaterial("WhiteOutline2", "UnlitGeneric", { [ "$basetexture" ] = "mat2" }) function your_stencil_function() local scale if togglescript then -- fading out if next_fadeout and CurTime() < next_fadeout then scale = (next_fadeout - CurTime()) / time_to_fadeout else next_fadeout = nil scale = 0 end else -- fading in if next_fadein and CurTime() < next_fadein then scale = 1 - ((next_fadein - CurTime()) / time_to_fadein) else next_fadein = nil scale = 1*9 end end //////CODE INSERTED HERE///// ----------------------- hook.Add("PostDrawOpaqueRenderables", "PostDrawing", function() if togglescript then return end //Togglescript Control Toggle of Whole Script! render.ClearStencil() render.SetStencilEnable(true) render.SetStencilFailOperation(STENCILOPERATION_KEEP) render.SetStencilZFailOperation(STENCILOPERATION_REPLACE) render.SetStencilPassOperation(STENCILOPERATION_REPLACE) render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_ALWAYS) render.SetStencilReferenceValue(1) cam.IgnoreZ(false); //This line of code, to my knowledge and testing, controls whether or not you see the players through walls and props. render.SetBlend(1) for _, ent in pairs(player.GetAll()) do ent:DrawModel() render.SetMaterial(matOutline); render.SetColorModulation(scale, scale, scale); //This line of code controls the color of the player and brightness. render.SuppressEngineLighting(true); //Suppress the natural lighting on the player. render.SetStencilReferenceValue(1); end render.SetBlend(0.1) cam.IgnoreZ(false); render.SetStencilEnable(false) render.SuppressEngineLighting(false); end) //////CODE INSERTED HERE///// ^^^^^^^^^^^^^^^^^^^^^^^^ end [/code][B] render.SetColorModulation(scale, scale, scale); //This line of code controls the color of the player and brightness.[/B] is the main line of code that needs to be faded in from 0 to 9, and backwards when turned off.
Pffrt, no. "your_stencil_function" should be your PostDrawOpaqueRenderables hook, use common sense. :v: [lua]local time_to_fadein = 5 local time_to_fadeout = 5 local next_fadein local next_fadeout concommand.Add("toggle_outline_script", function() togglescript = not togglescript if togglescript then next_fadein = CurTime() + time_to_fadein next_fadeout = nil else next_fadein = nil next_fadeout = CurTime() + time_to_fadeout end end) ////////Player Brightness////////// local matOutline = CreateMaterial("WhiteOutline2", "UnlitGeneric", { [ "$basetexture" ] = "mat2" }) hook.Add("PostDrawOpaqueRenderables", "PostDrawing", function() local scale if togglescript then -- fading out if next_fadeout and CurTime() < next_fadeout then scale = (next_fadeout - CurTime()) / time_to_fadeout else next_fadeout = nil scale = 0 end else -- fading in if next_fadein and CurTime() < next_fadein then scale = 1 - ((next_fadein - CurTime()) / time_to_fadein) else next_fadein = nil scale = 1*9 end end //////CODE INSERTED HERE///// ----------------------- if scale==0 then return end render.ClearStencil() render.SetStencilEnable(true) render.SetStencilFailOperation(STENCILOPERATION_KEEP) render.SetStencilZFailOperation(STENCILOPERATION_REPLACE) render.SetStencilPassOperation(STENCILOPERATION_REPLACE) render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_ALWAYS) render.SetStencilReferenceValue(1) cam.IgnoreZ(false); //This line of code, to my knowledge and testing, controls whether or not you see the players through walls and props. render.SetBlend(1) for _, ent in pairs(player.GetAll()) do ent:DrawModel() render.SetMaterial(matOutline); render.SetColorModulation(scale, scale, scale); //This line of code controls the color of the player and brightness. render.SuppressEngineLighting(true); //Suppress the natural lighting on the player. render.SetStencilReferenceValue(1); end render.SetBlend(0.1) cam.IgnoreZ(false); render.SetStencilEnable(false) render.SuppressEngineLighting(false); //////CODE INSERTED HERE///// ^^^^^^^^^^^^^^^^^^^^^^^^ end)[/lua] Also, togglescript should not make the function return, remember? Even if the script is off, it takes a short while for the effect to disappear.
It would seem that it still is not fading out the variable in the [B]render.SetColorModulation(scale, scale, scale); [/B] from 0 to 9, its just shooting from 0 to 9 instantly, like it did without the math.. =\ Would you mind at all coming to my server and testing this with me? we might have more progress that way. You could run the script yourself from what that has in itself, if you wanted, and then you would see what i was talking about. [b]EDIT:[/b] Well, it's 2:46AM, I'm heading off to bed, if you want to make another suggestion to it, by all means, else, you can test it yourself in gmod while I sleep. I will check back here after I wake up. Good Night, and thank you for all your help.
Damn, you should pay attention to what I'm writing and try to understand it, I wouldn't want to write all the code for you. I did a small mistake in the concommand, I made it fade in when togglescript is true, and fade out when it's false, while it should be the other way. Also, what's with this "9"? Do you even know why you put it there? You should realize what you just did made no sense at all, because first of all, scale would progressively go from 0 to 1... and then suddenly become 9 for no reason. If you want to extend scale so it's between 0 and 9 instead of 0 and 1, you have to multiply it by 9 AFTER all the calculations have been done. Else you'll just break the whole thing. SetColorModulation takes parameters which are between 0 and 1 anyway, so there's no point in doing that. [lua]local time_to_fadein = 5 local time_to_fadeout = 5 local next_fadein local next_fadeout concommand.Add("toggle_outline_script", function() togglescript = not togglescript if togglescript then next_fadein = nil next_fadeout = CurTime() + time_to_fadeout else next_fadein = CurTime() + time_to_fadein next_fadeout = nil end end) ////////Player Brightness////////// local matOutline = CreateMaterial("WhiteOutline2", "UnlitGeneric", { [ "$basetexture" ] = "mat2" }) hook.Add("PostDrawOpaqueRenderables", "PostDrawing", function() local scale if togglescript then -- fading out if next_fadeout and CurTime() < next_fadeout then scale = (next_fadeout - CurTime()) / time_to_fadeout else next_fadeout = nil scale = 0 end else -- fading in if next_fadein and CurTime() < next_fadein then scale = 1 - ((next_fadein - CurTime()) / time_to_fadein) else next_fadein = nil scale = 1 end end //////CODE INSERTED HERE///// ----------------------- if scale==0 then return end render.ClearStencil() render.SetStencilEnable(true) render.SetStencilFailOperation(STENCILOPERATION_KEEP) render.SetStencilZFailOperation(STENCILOPERATION_REPLACE) render.SetStencilPassOperation(STENCILOPERATION_REPLACE) render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_ALWAYS) render.SetStencilReferenceValue(1) cam.IgnoreZ(false); //This line of code, to my knowledge and testing, controls whether or not you see the players through walls and props. render.SetBlend(1) for _, ent in pairs(player.GetAll()) do ent:DrawModel() render.SetMaterial(matOutline); render.SetColorModulation(scale, scale, scale); //This line of code controls the color of the player and brightness. render.SuppressEngineLighting(true); //Suppress the natural lighting on the player. render.SetStencilReferenceValue(1); end render.SetBlend(0.1) cam.IgnoreZ(false); render.SetStencilEnable(false) render.SuppressEngineLighting(false); //////CODE INSERTED HERE///// ^^^^^^^^^^^^^^^^^^^^^^^^ end)[/lua] Nothing too important, but you should get used to Lua style comments, -- for line comments, and --[[ and ]] for block comments, instead of the C style comments, //, /* and */. The latter don't exist in native Lua, so you'll have a nasty surprise if you ever get to use Lua on something which isn't GMod. Also semicolons ";" are useless most of the times. If you have a newline, don't put a semicolon because it's horribly redundant.
Not to sound like an ass, but your wrong about the SetColorModulation parameters. The SetColorModulation takes parameters which are between 0 and 9. For example, [B]render.SetColorModulation(0, 9, 0);[/B] Makes a bright green overlay, while (0, 0, 9) makes a bright blue overlay, and (9, 0, 0); makes bright red,, and respectively, Transitioning from (0,0,0) to (9, 9,9); would make my desired fade in effect from dark gray to bright white. The only reason I say this is because I have toyed with it for over 3 hours in itself, so I know what it does, but I dont really have a need for colors like (9,0,9) unless its GONNA be PINK for some reason. its much like 255,255,255 = 9,9,9, just a different concept to grasp. If it were simply (1, 1, 1) it would be a dark whiteish gray, which would have no point what so ever, so I need to multiply it by 9, to make it go from (0, 0, 0)(Off, so it doesnt show black, just a starting number, to go to (9, 9, 9) ((REALLY FREAKING WHITE)) color replacement, I can make screenshots if you want further evidence about this, but thank you a lot, i just woke up, didnt like the color modulation comment, but still, thank you. [b]EDIT:[/b] Thank you so much, it works, has a wierd fadedown to 0, then to normal lighting, but its alright. Thank you, I will be able to use this for reference for a couple things, and yea, I read over and over all your pictures and explanations, I like reading it.
Well, it's not what you think, but looks like you found something pretty useful there. So if what you're saying is true, SetColorModulation can be used to oversaturate the color of an entity, and that's quite useful. I'm going to look into that, there's some great potential. But yeah, the values really go from 0 to 1. [b][url=http://wiki.garrysmod.com/?title=Render.SetColorModulation]Render.SetColorModulation [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] 1,1,1 is white, so basically the color of the entity isn't changed at all.
[QUOTE=_Kilburn;22534544]Well, it's not what you think, but looks like you found something pretty useful there. So if what you're saying is true, SetColorModulation can be used to oversaturate the color of an entity, and that's quite useful. I'm going to look into that, there's some great potential. But yeah, the values really go from 0 to 1. [B][URL="http://wiki.garrysmod.com/?title=Render.SetColorModulation"]Render.SetColorModulation [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B] 1,1,1 is white, so basically the color of the entity isn't changed at all.[/QUOTE] Glad I enlightened you as well.
Sorry, you need to Log In to post a reply to this thread.