# Instance Mode - calling other sketches

**URL:** <https://discourse.processing.org/t/instance-mode-calling-other-sketches/18893>\
**Category:** Coding Questions\
**Created:** [March 22, 2020, 5:22am UTC](https://discourse.processing.org/t/instance-mode-calling-other-sketches/18893 "2020-03-22T05:22:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![meh](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@meh](https://discourse.processing.org/u/meh)\
**Post date:** [March 22, 2020, 5:22am UTC](https://discourse.processing.org/t/instance-mode-calling-other-sketches/18893/1 "2020-03-22T05:22:21Z")

</div>

Hello,  
I wanted to know if this is possible. I want to be able to call others sketches back and forth. For instance for simplicity I created 2 sketches when i click on Sketch2 it will show that sketch now in this sketch I have button to show sketch1 but that is not working. I get this message TypeError: fxn is undefined[Show stack]

My goal is to create a mini game type of scenario where the player can choose or I can choose randomly different games the player can play and in these mini games be able to go back to the main screen and select another minigame. Can I do this in [openprocessing.org](http://openprocessing.org)?  
here is the code  
[https://www.openprocessing.org/sketch/860688](https://www.openprocessing.org/sketch/860688)

---

<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:** [March 23, 2020, 2:51am UTC](https://discourse.processing.org/t/instance-mode-calling-other-sketches/18893/2 "2020-03-23T02:51:07Z")

</div>

> [@meh](#):
>
> Can I do this in [openprocessing.org](http://openprocessing.org)?

Even though I use & like that Processing sketch hosting site very much, for more complex sketches, I’d rather choose other hosting options w/ more code flexibility. 🤭

> [@meh](#):
>
> … and select another minigame.

There are many approaches we can go with for multi-pick sketch. 🛣

But b/c all the sketches you intend to run are using “instance mode”, that makes things much easier. 😁

So I’ve come up with an example which uses **createRadio()** to pick the _activeSketch_ from among those stored in _sketches[]_: 🔘

> **[reference | p5.js](https://p5js.org/reference/#/p5/createRadio)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

Every time the current **option()** is **changed()**, it calls back **runPickedSketch()**; which in turn calls **destroyMe()** for the _activeSketch_, then calls **createMe()** using the index - 1 from method **value()**. 🤓

You can see it running online at this JS hosting site below: 🏃‍♂️

> **[Radio Pick Active Sketch](https://glitch.com/~radio-pick-active-sketch)**
>
> Simple, powerful, free tools to create and use millions of apps.

And also edit it here: [Glitch :･ﾟ✧](http://Glitch.com/edit/#!/radio-pick-active-sketch)  
Fullscreen presentation: [radio-pick-active-sketch.Glitch.me](http://radio-pick-active-sketch.Glitch.me)

P.S.: Due to a bug in methods **option()** & **value()**, I had to store _sketches[]_'s indices starting at `1`, b/c those methods can’t store the value `0`! 🐛

---

<div class="post-metadata">

**Author:** ![meh](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@meh](https://discourse.processing.org/u/meh)\
**Post date:** [March 24, 2020, 4:03am UTC](https://discourse.processing.org/t/instance-mode-calling-other-sketches/18893/3 "2020-03-24T04:03:30Z")

</div>

Thank you for the thorough explanation. Unfortunately since the course I’m taking requires me to use this website my options are limited.

---

<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:** [March 24, 2020, 12:29pm UTC](https://discourse.processing.org/t/instance-mode-calling-other-sketches/18893/4 "2020-03-24T12:29:46Z")

</div>

> [@meh](#):
>
> … requires me to use this website my options are limited.

It does work on OpenProcessing too: 🥳

> **[Radio Pick Active Sketch - GoToLoop - OpenProcessing](https://openprocessing.org/sketch/861749)**

However, b/c we can’t control the order the multi-tabs are loaded there, the global variable _sketches[]_ defined within the main tab may not exist yet when the other tabs happen to be run 1st. 😬

As a workaround, we can have this statement on each tab: 🧰  
`var sketches = window.sketches || [];`

It makes sure to create an Array and assign it to _sketches[]_ if 1 isn’t already present in it. 💡
