How should I organise this? 256 array with conditional content - Class?

Hi All,
Festive greetings. What’s the best way to do this? I will have up to 256 different sections of a live AV performance - each potentially featuring a different bit of cool codey Processing stuff. Should I be using a for loop (<=255, i++?) How I then could I implement variable content depending upon a current pattern? See my idiot code below - while I think this would work, it seems far from optimal.

  if (PatternNewNumber!=-1){
    textSize(48);
    text(PatternNewNumber, 10, 100);
    
      if (PatternNewNumber == 0){
      // cool Processing Stuff 1..
      }
      
      if (PatternNewNumber == 1){
      // cool Processing Stuff 2..
      }
      
      if (PatternNewNumber == 2){
      // cool Processing Stuff 3..
      }
     
      ...&etc 
    
      if (PatternNewNumber == 256){
      // cool Processing Stuff 255
      }
}

Hi, Try looking at switch statements, far more effective at things like this

Like this right? But should I use a loop somehow? Or classes or something?

 if (PatternNewNumber!=-1){
    textSize(48);
    text(PatternNewNumber 10, 100);
    
      switch (PatternNewNumber){
        
        case 1:
          // cool Processing Stuff 1..
        break;
        
        case 2:
         // cool Processing Stuff 2..
        break;
        
        case 3:
          // cool Processing Stuff 3..
        break;     

        case 4:
          // cool Processing Stuff 4..
        break;
    
       ..... etc   
    
        case 255:
         // cool Processing Stuff 255..
        break;  
}

If those cool stuffs can fit into a method you could create a base interface or abstract class w/ 1 abstract method display():

@FunctionalInterface interface CoolStuff {
  void display();
}
abstract class CoolStuff {
  abstract void display();
}

Then create a CoolStuff[] array w/ length 256:

final static int STUFFS = 256;
final CoolStuff[] stuffs = new CoolStuff[STUFFS];

And in a separate tab file have a function createCoolStuffs() where you anonymous-instantiate your 256 CoolStuff objects.

Each instance would have a different implementation of display():

void createCoolStuffs() {
  stuffs[0] = new CoolStuff() {
    @Override public void display() {
      // Cool Processing Stuff 000...
    }
  };

  stuffs[1] = new CoolStuff() {
    @Override public void display() {
      // Cool Processing Stuff 001...
    }
  };

  stuffs[2] = new CoolStuff() {
    @Override public void display() {
      // Cool Processing Stuff 002...
    }
  };
}
4 Likes

Oh my brain hurts!! Excellent. thanks for the guidance. What’s the rational for approaching the problem in this way? What are the advantages over the simple switch example?

@GoToLoop provides a nice solution

It simplifies the creation and use of your coolstuff.

To create new coolstuff simply add extra stuffs in the createCoolStuffs() function then when you want to use it initialise it in setup and call it in draw like this

int currStuff = 0;

void setup(){

   createCoolStuffs();
}

void draw(){
  background(...);

  stuffs[currStuff].display();
}

You can change the stuff to display by simply changing currStuff to some other valid value in the array.

It avoids huge if or switch statements which would be difficult to maintain.

2 Likes

Hey quark and GoToLoop,
This is making total sense - thanks to you both for your clarity - you guys are great - making complex things easy to understand - a real gift :slight_smile: Thank you and Nadolig Llawen (Welsh for Merry Christmas)

1 Like

That’s impressive and could also be used to make the states
of a Sketch.

Am I right?

1 Like

Yes :smile:

Oh dear I need 20 characters LOL

2 Likes

Hi Chrisir, An interesting question, and one beyond my understanding - could you expand on this a little?

Sorry, it has nothing to do with your initial question

We often use states here in the forum to distinguish between different screens of a program, for example welcome screen, game and high score.

These states are often distinguished with switch() command but this approach here is better

1 Like

Thanks Chrisir for the wider context. I’m interested in furthering my knowledge in all aspects, so the broader comments are always welcome! :slight_smile:

1 Like

As I am now trying to expand upon the CoolStuff abstract class, where would I incorporate, for example, movie playback. If I wanted to play a different video for each bankstuffs[i] where’s the best place for the following? Import the video library in the main sketch? If there are 16 different large video files (1GB+) will there be any potential memory issue with putting movie = new Movie(this, “BankA.mov”); x 16 in setup() as the example suggests? Do I put it in the CoolBanks class instead? and the movieEvent into the mainsketch too? What’s finally the best way to conditionally play each movie - bankstuffs[0], bankstuffs[1], bankstuffs[2] etc ? Any guidance would be awesome.

import processing.video.*; // this in main sketch?

Movie bankmovie;

void setup() {
  size(560, 406);
  background(0);
  bankmovie = new Movie(this, "BankA.mov"); /* there will be 16 of these - should this be an array
  any likely issue with 16 large video files here? */ 
  bankmovie.loop();
}

void movieEvent(Movie m) { // where should this go - main sketch or abstract class?
  m.read();
}

void draw() {
  if (bankmovie.available() == true) { 
  bankmovie.read(); 
  image(bankmovie, 0, 0, width, height);  /* make bankmovie an array index? bankmovie[i]? called within bankstuffs[i]?
 */ 
}
}