General Programming/Lua question

Home Forums General Programming General Programming/Lua question

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #120464
    Jsh
    Participant
      • Topics: 6
      • Replies: 12
      • Total: 18

      Heya guys,

      I have been working on my panel for quite a bit now and I’m no programmer so any time I’ve hit a brick wall I’ve just come here for the answers, now progress is coming along but I’m going back over things to tidy up a lot since there are a lot of methods and a lot of messy coding and obsolete code. One thing that I’m unsure how to do that would help with this a lot is – throughout my methods I have “utility” functions (at least thats how I’ll refer to them – nothing to do with the utility features etc in Ctrlr) that are used all over the place in different methods, I just copy and paste them into each script that needs to use it.

      What I’m looking for is how I can have the function declared in one place that I can access from each of my methods without having to have it written out in every other script…Kind of like how in c++ you would have a header file with functions, variables, etc and just #include to reference the functions from it…this stems from just a general lack of understanding of Lua & Ctrlr, (and to an extent programming in general – still pretty n00b).

      for example I have a function for decoding patch/program dumps used in a few places, where ideally I’d have it declared in one file:

      -- utility functions method
      function decodeDump(patchData)
      
        --my function code
      
      end

      then

      -- Load patch data method
      function loadPatchData()
      
        local patchData = {FO, 00, .. F7}
        local decodedData = decodeDump(patchData)
      
      end

      I assume this is pretty easily doable as it took me an age to realize that I can access non local variables from any method by simply…typing out the variable name. So yeah, any help is appreciated – probably something very obvious or easy to find elsewhere but this forum has proved super helpful so far so it’s pretty much my go to atm.

      Cheers.

      #120469
      dnaldoog
      Participant
        • Topics: 4
        • Replies: 480
        • Total: 484
        • ★★

        In the lua editor, Right click on the lua cube and create a function by clicking on Add Method.

        Then you can access that function globally and call it from multiple parts of your panel.

        
        ------global------function
        myRandom = function()
            return (math.random(0, 512))
        end
        ------global------function
        a = function()
            local b = myRandom()
            return b + b
        end
        
        b = function(n)
            if n == true then
                return myRandom()
            end
        end
        
        console(tostring(a()))
        console("" .. b(true))
        console(tostring(myRandom()))
        
        

        Regards,

        • This reply was modified 3 years, 6 months ago by dnaldoog.
        #120476
        Jsh
        Participant
          • Topics: 6
          • Replies: 12
          • Total: 18

          Sweeeet, thanks again Dnaldoog

        Viewing 3 posts - 1 through 3 (of 3 total)
        • The forum ‘Programming’ is closed to new topics and replies.
        There is currently 0 users and 98 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