Home › Forums › General › Programming › [resolved] if variable == nil then how did you get here?
- This topic has 10 replies, 2 voices, and was last updated 10 years, 9 months ago by msepsis.
-
AuthorPosts
-
July 19, 2013 at 10:43 pm #10975
I’m seeing a callback error when I start up a panel i’m deving that has me a little mystified.. Here’s the error:
lua runtime error at line 15, attempt to perform arithmetic on global WAVMOD_Sample01, a nil value..
I have a script on a knob called executeWaveformMix that mixes samples 1-64 of two waveforms if their sample data is defined.. here are the first few lines of my code:`executeWaveformMix = function(mod, value)
fileFunctionsOk = panel:getModulatorByName(“testButton”):getModulatorValue()
–check if panel is loading…
if fileFunctionsOk == 0 then
elseif fileFunctionsOk == 1 thenif WAVDSigned_XT_Sample01 ~= nil then
if WAVMOD_Sample01 ~= nil thenwaveshaperColor = 0x22FFFFFF –low white
mixedWaveColor = 0x2200FF00 –low green
waveFormColor = 0xFF55BBff –full bluelocal mix= (panel:getModulatorByName(“Wave Gen Mix”):getModulatorValue()*.05)
WAVDSigned_XT_Sample01 = ((WAVDSigned_XT_Sample01)+(WAVDMOD_Sample01*mix)) – (WAVDSigned_XT_Sample01 * mix) — this is LINE 15, where the error occurs
WAVDSigned_XT_Sample02 = ((WAVDSigned_XT_Sample02)+(WAVDMOD_Sample02*mix)) – (WAVDSigned_XT_Sample02 * mix)`so how is the program even getting to line 15 if WAVMOD_SAMPLE01 == nil?!?
- This topic was modified 10 years, 9 months ago by msepsis. Reason: fixed my own issue
- This topic was modified 10 years, 9 months ago by msepsis.
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
July 19, 2013 at 10:57 pm #10976ahh.. fixed my own issue.. I hadn’t locally declared WAVMOD_SAMPLE01 in the opening line of the script. Adding it within the parenthesis cleared the error. The give-away was that the error referred to the variable as a GLOBAL.
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
July 19, 2013 at 11:00 pm #10978Are you sure ?
1. indent your code, use tabs and indent any for,if,while
also when you do one line IF statements you need to be careful. I don’t believe that Lua is broken, split those if.elseif into something more readable.
July 19, 2013 at 11:08 pm #10981either yeah I’m sure or I fixed it before you replied 🙂
I’m a complete noob compared to you with programming but AFIK Lua is a freeform syntax which cares not about whitespace.
from wikipedia:
whitespace characters do not change the functionality of the code, and are ignored by the lua interpreter.Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
July 19, 2013 at 11:16 pm #10983You misunderstood me 🙂
Ofcourse whitespaces are ignored that’s why you should use them. Code identation is for you not for the compiler, it will help you read your code later on.
July 19, 2013 at 11:23 pm #10984got it. I agree it’s helpful to tab and format for clear readability – sometimes I end up adding if statements to a number of things while I add in new features.. it becomes inevitable.
Do you know of any programs that you can use after changing the structure of your script to do auto formatting for you, like a code sweeper? I used to use those when I was an HTML and actionScript “programmer”.. Homesite was what I used back then in the 90s.. a google search hasn’t pulled anything up like that for lua…
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
July 19, 2013 at 11:47 pm #10989crap. not resolved. so declaring that variable in line 1 eliminated the error but now the button the script is attached to doesn’t do a damn thing. There’s no error on the script.. I’ll dig through the formatting, did you happen to see anything there that prompted the “are you sure” question? I might have misunderstood that too as i took that as you were able to find an error in my code. It all looks good to me. probably one of things i need to step away from for a bit as I’ve been chasing this bug for about a day
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
July 19, 2013 at 11:57 pm #10992I can’t follow the logic exactly, especialy with lines like
if fileFunctionsOk == 0 then elseif fileFunctionsOk == 1 then
I’d rather have clear exit points in my method, like
if fileFunctionsOk == 0 then return elseif fileFunctionsOk == 1 then -- do something end
in code like that i clearly know what happens, BUT THAT’S ME, you need to write code that YOU will understand.
- This reply was modified 10 years, 9 months ago by atom.
July 20, 2013 at 12:05 am #10996got it. Since we’re on the topic… I didnt realize an “elseif” will work if you’ve already closed the first if.. I thought that was the point of using elseif – that you don’t need to “end” the first “if” statement.
I get your point though. Contrary to how my example looked I’ve got a terrible case of OCD!
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
July 20, 2013 at 12:06 am #10999I just changed it you are correct you can’t use end and elseif. But that’s not really the point like you wrote.
Put your code in blocks, this is much easier to understand how code is executed. Also when you go back to this in a month or so, you’ll undestand this much quicker, unless ofcourse you have the discipline to comment your code (i don’t)
July 20, 2013 at 1:29 am #11007oh my god. it was a freaking typo.
WAVMOD_Sample01 ~= WAVDMOD_Sample01
ok. move along.. nothing to see here . .
/facepalm
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
-
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.