# Calling Sketches

**URL:** <https://discourse.processing.org/t/calling-sketches/25457>\
**Category:** p5.js\
**Created:** [November 16, 2020, 10:15am UTC](https://discourse.processing.org/t/calling-sketches/25457 "2020-11-16T10:15:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpw](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@cpw](https://discourse.processing.org/u/cpw)\
**Post date:** [November 16, 2020, 10:15am UTC](https://discourse.processing.org/t/calling-sketches/25457/1 "2020-11-16T10:15:53Z")

</div>

Is there a way to call a sketch from another sketch?

Thanks for any pointers.

Chris

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 16, 2020, 11:24am UTC](https://discourse.processing.org/t/calling-sketches/25457/2 "2020-11-16T11:24:56Z")

</div>

Hi,

Calling a sketch from another sketch doesn’t really make sense.  
The two important parts are the `setup()` and `draw()` functions so you want all of your drawing instructions to go there.

Let’s suppose you have two sketches :

```auto
// sketch1.js

function setup() {
  createCanvas(400, 400);
}

function draw() {
  // rectangle
  rect(50, 50, 100, 100);
}

```

```auto
// sketch2.js

function setup() {
  createCanvas(400, 400);
}

function draw() {
  // ellipse
  ellipse(50, 50, 100, 100);
}

```

Then you just combine the two sketches :

```auto
// sketch_combined.js

function setup() {
  createCanvas(400, 400);
}

function draw() {
  // ellipse
  ellipse(50, 50, 100, 100);

  // rectangle
  rect(50, 50, 100, 100);
}

```

This is possible when your sketches are small but when they get bigger, the easiest way is to encapsulate them in classes and functions and then call them in a single sketch.

---

<div class="post-metadata">

**Author:** ![cpw](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@cpw](https://discourse.processing.org/u/cpw)\
**Post date:** [November 16, 2020, 12:08pm UTC](https://discourse.processing.org/t/calling-sketches/25457/3 "2020-11-16T12:08:19Z")

</div>

I agree. I will combine the code with different state variables to run the code I want to see.
