Void...question

If I create something like this:

void setup () {

}

void draw () {

if (mousePressed == true) // SCENE is the function that contains the scene number (such as 00, 01, 02, …)
SCENE = SCENE + 1;
}

void scene00 () {

}

void scene01 () {

}

void scene02 () {

}
etc!

Basically,
SCENE = 00 corresponds to scene00 ();
SCENE = 01 corresponds to scene01 (); etc.

There is no way to create something that allows me to write:
scene [SCENE] ();
?

Thank you!

We can invoke void parameterless functions by using method() and passing a String argument as the name of the desired function: :bulb:

/**
 * Invoking Functions by Name (v1.0)
 * GoToLoop (2018/Dec/20)
 * https://Discourse.Processing.org/t/void-question/6792/2
 */

static final String SCENE = "scene";
static final char SCENES = 16; // number of scenes
static final int DIGITS = Integer.toString(SCENES).length();

int scene; // current scene

void setup() {
  noLoop();
}

void draw() {
  background((color) random(#000000));
  method(SCENE + nf(scene, DIGITS)); // invoke current scene
}

void mousePressed() {
  scene = (scene + 1) % SCENES; // next scene
  redraw = true;
}

void scene00() { // 1st scene
  println(scene);
}

void scene01() { // 2nd scene
  println(scene);
}

// ...

void scene15() { // 16th scene
  println(scene);
}
4 Likes

I could not get it to work:

  1. I see the change of scenes in println, but the scenes do not change!
  2. I can not put in Loop, because in the different scenes I have movements and animations. - However even putting on Loop, it remains the problem of 1).
    Do you know how to help yet? Where is the problem?

Thanks

W/o seeing the code any help would be just a far shot blind guess! :dark_sunglasses:
Most I can do is posting a link to a more elaborate sketch that uses method() too: :link:

Although the sketch above also got noLoop() on it, you can simply comment that out w/o any problems. :wink:

One very simple way to handle this is a switch statement to make your collection of functions callable by index.

void scene(int num){
  switch(num) {
    case 0: 
      scene0();
      break;
    case 1: 
      scene1();
      break;
    case 2: 
      scene2();
      break;
  }
}

Then you interact with them through a current_scene variable and calling the scene() switch. When you add new scenes, write a function and add it to the switch.

int current_scene = 0;
scene(current_scene);
current_scene++;
scene(current_scene);
// all scenes
for(int i=0; i<3){
  scene(i);
}
if(restart){
  current_scene = 0;
  scene(current_scene);
}

Another way to handle this problem is to use objects.

class Scene {
  void run(){};
}

Now you can declare a new Scene and @override its run().

Instead of having hard-coded indexes, now you have a list of scenes:

Scene[] scenelist = new Scene[3];

Now you maintain the index, get your scene by index, and call .run() on it:

scenelist[current_scene].run()

This allows you to do things like shuffle the scene order, or add / remove / replace scenes based on game events. Since a Scene is an object, you could add properties like names, or timers, or board size, etc. to each scene. You could also use ArrayList for more flexibility.

ArrayList<Scene> scenelist = new ArrayList<Scene>;
// ...
scenelist.get(current_scene).run();
1 Like

The Pjs version of my “button scene” sketch uses switch () / case: :sunglasses:
Studio.ProcessingTogether.com/sp/pad/export/ro.9eDRvB4LRmLrr

While its Java Mode version goes w/ method() instead. :money_mouth_face:

1 Like