Structs and Arrays hit me with confusion
-
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?
-
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
-
@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...
-
@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
-
@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!