Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    2d arrays (error)

    Coding
    7
    9
    508
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • sys64738
      sys64738 F last edited by MikeDX

      Hey folks. I've not used multi dimensional arrays before.

      Just trying to do

      Array menu[3][1]
      
      menu[1] = "speed"
      menu[1][1] = 1 //this line errors
      

      Attempted to index non array type.

      How do I add or change data to the second row without causing error?

      1 Reply Last reply Reply Quote 0
      • Eearslya
        Eearslya Donator last edited by

        I think you might be misunderstanding what a multi-dimensional array is.

        The problem is that you're assigning menu[1] to be a string, "speed".
        menu[1][1] means "the second object of the second array in the array menu". When you set menu[1] to "speed", menu[1] is no longer an array, and menu[1][1] is no longer a valid assignment.

        For most purposes, when using multi-dimensional arrays, you always want to specify both dimensions. Think of it like a table of data. The first number is the row, the second number is the column. What you've done is replaced the entire row with just one string and gotten rid of all of the columns for that row.

        P.S. The first object in an array is actually 0, not 1.

        sys64738 1 Reply Last reply Reply Quote 1
        • sys64738
          sys64738 F @Eearslya last edited by

          @Eearslya yes realise they start at 0. With a 1 dimensional array I can do
          Array fish[10]
          Fish[6] = "snapper"
          And it's totally ok with me skipping 0 or any other number until I need or decide to use them.

          So I can't use database in this language. I know I can do

          Array menurow1[20]
          Array menurow2[20]
          Pos = 7
          menurow1[Pos] = "speed"
          menurow2[Pos] = 1

          Just know there must be a better way

          Martin 1 Reply Last reply Reply Quote 0
          • pianofire
            pianofire Fuze Team last edited by

            You can use an array of structures. See here:

            https://fuzearena.com/help/view/Structures

            1 Reply Last reply Reply Quote 1
            • Martin
              Martin Fuze Team @sys64738 last edited by

              @sys64738 said in 2d arrays:

              @Eearslya yes realise they start at 0. With a 1 dimensional array I can do
              Array fish[10]
              Fish[6] = "snapper"
              And it's totally ok with me skipping 0 or any other number until I need or decide to use them.

              So I can't use database in this language. I know I can do

              Array menurow1[20]
              Array menurow2[20]
              Pos = 7
              menurow1[Pos] = "speed"
              menurow2[Pos] = 1

              Just know there must be a better way

              I think you may have overlooked the bit where @Eearslya said that you have modified menu[1] to be a string so it is no longer an array at that point, so the following line menu[1][1] is no longer valid. That's all that's wrong here.

              1 Reply Last reply Reply Quote 1
              • ITzTravelInTime
                ITzTravelInTime F last edited by ITzTravelInTime

                just use a single dimention array and then use some simple math to transform x and y into the position of the element into the 1D array.

                For exaple:

                arrayWidth = 30
                arrayHeight = 22
                
                array myArray[arrayWidth * arrayHeight]
                
                //get an element at position x, y from the array:
                myElement = myArray[y * arrayWidth + x]
                
                //write an element to the position x, y into the array
                myArray[y * arrayWidth + x] = someNumber
                
                

                I used this trick all the time while programming in dos (bacause of the limitations of the c compiler i use or just to calculate the correct address of the video buffer) and you have to use 2d arrays for games, espacially for things like maps and graphics

                1 Reply Last reply Reply Quote 1
                • Dave
                  Dave Fuze Team last edited by

                  @sys64738 To put this into more visual clarity, here's what you're doing in the first example:

                  Array menu[3][1]
                  

                  Is the same as:

                  menu = [
                     [ 0 ],
                     [ 0 ],
                     [ 0 ]
                  ]
                  

                  You've made an array of arrays. Each element of the first dimension is itself an array, each with 1 element.

                  When you write:

                  menu[1] = "Speed"
                  

                  You've done this:

                  menu = [
                      [0], // This element is unchanged
                      "speed", // This element has been redefined from an array with 1 element to a string
                      [0] // This element is unchanged
                  ]
                  

                  Now this has happened, we can no longer access menu[1][1] because there is no longer an array in menu[1].

                  I think an array of structures, as @pianofire mentioned, might be what you're looking for. However, it's hard to know without knowing the application if it's best. Perhaps you could share what you're planning?

                  You could have:

                  menu = [
                      [
                          .name = "Speed",
                          .value = 1,
                          .position = { gwidth() / 2, gheight() / 2 }, // Just an example
                          .colour = grey
                      ],
                      [
                          .name = "Defense",
                          .value = 2,
                          .position = { gwidth() / 2, gheight() / 3 }, 
                          .colour = grey
                      ]
                  ]
                  

                  Since it sounds like you're going to have different values and names for each part of the menu, it might be easier to lay it out this way. Here, we could access the speed value with:

                  print( menu[0].name, ": ", menu[0].value )
                  

                  Hope I haven't confused you further! There are lots of ways to set up data in Fuze!

                  1 Reply Last reply Reply Quote 1
                  • sys64738
                    sys64738 F last edited by

                    Thanks guys I was able to get it working using the 2d array holding a mixture of strings and floats and integers, I was initially thrown off track by the index bug I reported.

                    I have no education in programming but spent about 10 years programming stumbling my way along.The need for this was for a menu for my synth, you press up or down on dpad and cycles through all parameter names, like wave, volume , speed

                    so on screen you see Speed 3.6, press up and see Volume 0.5 up again to see Wavetable 3, then I press right and it shows Wavetable 4
                    So print (Menu[menupos][0] + " " + menu[menupos][1]) // actually I put this into a variable first as print errors
                    sp uimsg = Menu[menupos][0] + " " + menu[menupos][1]
                    print (uimsg)

                    So its working, and you can cycle through the paremeters and adjust them and it changes the sound, the way you have done it is more understandable to read.

                    I am going to add a 16 step sequencer next, so it will need to hold not just one set of paremeters, but 16

                    so I hope I just change Array menu[20][16] and now I have room for 16 sets of parameter values
                    so wavetable 1 3 4 0 2 2 3 1 4 3 1 2 3 0 0 1

                    so each step in the sequencer it can play an entirely different patch.

                    After this I am unsure as I want it to have a 16 channel sequencer as fuze supports 16 voices, so I will need 16 versions of my 16 parameter array. Again I am unsure about arrays, but at this point I will try to switch to a 3d array, which hopefully gives me what I need,

                    ------------------------------------------------------------------]
                    wave 0,3,1,2,3,4,2,3,1,2,4,0,2,3,0 ]
                    speed 1,0.4,3.6,6.5,0.1,1,0,2.3,1,2,3,4,5,6,7 ] x 16
                    volume 1,2,3,4,5,6,5,4,3,2,1,2,3,4,5,6 ]
                    etc ]

                    So I am imagining ^thats on a piece of paper and if i switch to a 3d array I will have 16 pieces of paper

                    My worries about using a structure is, I am unsure if I can use loops to cycle though them and can we do multidimensional structures as I would need 16 versions of an array structure if it does not.

                    1 Reply Last reply Reply Quote 0
                    • mixaal
                      mixaal F last edited by

                      You can use array of structures and iterate over an array and access the element of the structure. Don't have my switch here (so unfortunately can't take a look into help), but here's what I've done:
                      struct terrainElement
                      float elevation
                      integer hashIndex
                      endStruct

                      terrainElement terrainMap[4096]

                      for i=0 to elementsNo loop
                      print(terrainMap[i].elevation)
                      repeat

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post