• Use past localized variable value if nil?
    3 replies, posted
Sorry for the vague title, I'm not sure how else to summarize what I'm asking for. So, say you have a function that runs once and isn't updated again. It calls for another function like so: [code]function Test1( ) Test2( CurTime( ) ) end[/code] Test2 runs every frame because it is in the Think hook, and it's job is to print its results. [code]function Test2( theTime ) print( theTime ) end[/code] What I'm trying to do in this scenario is get Test2 is revert to the last non-nil value of theTime so it doesn't output nil, but rather the last CurTime( ) that was input. Is it possible to do this without having to use global values?
Wait what? I'm not sure if I understand what you're trying to explain... This is what I'm thinking you're talking about: [code]local fLastTime = -1; function Test1() Test2( CurTime() ); end function Test2( fTime ) if ( !fTime ) then if ( fLastTime == -1 ) then fLastTime = fTime; end fTime = fLastTime; end print( fTime ); end[/code] Could you give an example of some real world usage for what you're trying to do?
Should be as simple as this: [lua] local t function Test2( theTime ) t = theTime or t return t end[/lua] Console output: [code] ] lua_run_cl Test2() nil ] lua_run_cl Test2(1) 1 ] lua_run_cl Test2() 1 ] lua_run_cl Test2(4) 4 ] lua_run_cl Test2() 4 [/code] If you don't understand the "or" statement, basically it's a case structure like "if this or this then do this", because the first statement is nil, which is FALSE, it does the second operator in the case statement instead. You can do something similar like this: [lua] local t function Test2( theTime ) t = theTime or t return theTime and theTime + CurTime() or t end[/lua] Here, if theTime is not nil, it will perform the math there and return the value, if it is nil it will return t instead.
Thanks, that's totally the solution OzymandiasJ!
Sorry, you need to Log In to post a reply to this thread.