# Void...question

**URL:** https://discourse.processing.org/t/void-question/6792
**Category:** Beginners
**Created:** [December 20, 2018, 11:04pm UTC](https://discourse.processing.org/t/void-question/6792 "2018-12-20T23:04:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![IMTDSN](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@IMTDSN](https://discourse.processing.org/u/IMTDSN)
#### Post date: [December 20, 2018, 11:04pm UTC](https://discourse.processing.org/t/void-question/6792/1 "2018-12-20T23:04:26Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 20, 2018, 11:44pm UTC](https://discourse.processing.org/t/void-question/6792/2 "2018-12-20T23:44:33Z")

</div>

We can invoke [`void`](https://processing.org/reference/void.html) parameterless functions by using [**method()**](http://processing.github.io/processing-javadocs/everything/processing/core/PApplet.html#method-java.lang.String-) and passing a [String](https://processing.org/reference/String.html) argument as the name of the desired function: 💡

```java
/**
 * 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);
}

```

- [Forum.Processing.org/two/discussions/tagged?Tag=method()](http://Forum.Processing.org/two/discussions/tagged?Tag=method%28%29)

---

<div class="post-metadata">

### Author: ![IMTDSN](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@IMTDSN](https://discourse.processing.org/u/IMTDSN)
#### Post date: [December 21, 2018, 10:07am UTC](https://discourse.processing.org/t/void-question/6792/3 "2018-12-21T10:07:42Z")

</div>

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

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 21, 2018, 10:33am UTC](https://discourse.processing.org/t/void-question/6792/4 "2018-12-21T10:33:35Z")

</div>

> [@IMTDSN](#):
>
> Do you know how to help yet? Where is the problem?

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

- [Creating a Next Page button - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/558/creating-a-next-page-button#Item_8)

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

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [December 22, 2018, 5:57pm UTC](https://discourse.processing.org/t/void-question/6792/5 "2018-12-22T17:57:06Z")

</div>

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

```auto
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.

```auto
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.

```auto
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:

```auto
Scene[] scenelist = new Scene[3];

```

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

```auto
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.

```auto
ArrayList<Scene> scenelist = new ArrayList<Scene>;
// ...
scenelist.get(current_scene).run();

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 22, 2018, 6:03pm UTC](https://discourse.processing.org/t/void-question/6792/6 "2018-12-22T18:03:23Z")

</div>

> [@jeremydouglass](#):
>
> One very simple way to handle this is a `switch ()` statement…

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

While its Java Mode version goes w/ **method()** instead. 🤑
