Navigation

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

    Structs and Arrays hit me with confusion

    Coding
    2
    5
    380
    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.
    • Tratax
      Tratax F last edited by Tratax

      So i think i may need a refresher on how structs work..

      I'm attempting to make a small demo that setups a struct with a position and color variable then runs a for loop to set this up with some randomised colors and then plot that group of 5 to the screen

      This is my first attempt using structs so I may just be missing something basic?

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

        So I think that you need something like this:

        gw = gWidth()
        gh = gHeight()
        
        struct pixel 
          vector pos
          vector col
        endStruct
        
        pixel pixels[5]
        
        for i = 0 to 5 loop
          pixels[i] = [ .pos = { random(gw), random(gh) }, .col = { random(1.0), random(1.0), random(1.0), 1 } ]
        repeat
        
        loop
          clear()
          for i = 0 to 5 loop
            plot(pixels[i].pos.x, pixels[i].pos.y, pixels[i].col )
          repeat
          update()
        repeat
        
        Tratax 1 Reply Last reply Reply Quote 0
        • Tratax
          Tratax F @pianofire last edited by Tratax

          @pianofire - Thanks! I decided to switch back to arrays and this is now working.. I still don't understand the value/purpose of structs over arrays...

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

            @Tratax No problem. Whatever you are more comfortable with. It is just a way of grouping related information really so instead of having several different arrays you have a single array of structs

            array enemyPosition[10]
            array enemyHitpoints[10]
            
            enemyPosition[0] = { 100, 100 }
            enemyHitpoints[0] = 100
            
            \\ is equivalent to
            
            struct enemy
              vector Position
              int Hitpoints
            endStruct
            
            enemy enemies[10]
            enemies[0].Position = { 100, 100 }
            enemies[0].Hitpoints = 100
            
            Tratax 1 Reply Last reply Reply Quote 1
            • Tratax
              Tratax F @pianofire last edited by

              @pianofire Ok awesome , thank you - I've really only stepped up from single variables to vectors and arrays in the last months so I'll need to practice more use of different variable types I think - Thanks again!

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