[resolved] if variable == nil then how did you get here?

Home Forums General Programming [resolved] if variable == nil then how did you get here?

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #10975
    msepsis
    Participant
      • Topics: 219
      • Replies: 732
      • Total: 951
      • ★★★

      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 then

      if WAVDSigned_XT_Sample01 ~= nil then
      if WAVMOD_Sample01 ~= nil then

      waveshaperColor = 0x22FFFFFF –low white
      mixedWaveColor = 0x2200FF00 –low green
      waveFormColor = 0xFF55BBff –full blue

      local 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

      #10976
      msepsis
      Participant
        • Topics: 219
        • Replies: 732
        • Total: 951
        • ★★★

        ahh.. 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

        #10978
        atom
        Keymaster
          • Topics: 159
          • Replies: 2945
          • Total: 3104
          • ★★★★★

          Are 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.

          #10981
          msepsis
          Participant
            • Topics: 219
            • Replies: 732
            • Total: 951
            • ★★★

            either 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

            #10983
            atom
            Keymaster
              • Topics: 159
              • Replies: 2945
              • Total: 3104
              • ★★★★★

              You 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.

              https://en.wikipedia.org/wiki/Indent_style

              #10984
              msepsis
              Participant
                • Topics: 219
                • Replies: 732
                • Total: 951
                • ★★★

                got 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

                #10989
                msepsis
                Participant
                  • Topics: 219
                  • Replies: 732
                  • Total: 951
                  • ★★★

                  crap. 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

                  #10992
                  atom
                  Keymaster
                    • Topics: 159
                    • Replies: 2945
                    • Total: 3104
                    • ★★★★★

                    I 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.
                    #10996
                    msepsis
                    Participant
                      • Topics: 219
                      • Replies: 732
                      • Total: 951
                      • ★★★

                      got 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

                      #10999
                      atom
                      Keymaster
                        • Topics: 159
                        • Replies: 2945
                        • Total: 3104
                        • ★★★★★

                        I 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)

                        #11007
                        msepsis
                        Participant
                          • Topics: 219
                          • Replies: 732
                          • Total: 951
                          • ★★★

                          oh 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

                        Viewing 11 posts - 1 through 11 (of 11 total)
                        • The forum ‘Programming’ is closed to new topics and replies.
                        There is currently 0 users and 123 guests online
                        No users are currently active
                        Forum Statistics
                        Threads: 2,495, Posts: 17,374, Members: 77,605
                        Most users ever online was 12 on January 22, 2019 3:47 pm
                        Ctrlr