# Instance mode : accessing \`p5\` functions in an external class

**URL:** <https://discourse.processing.org/t/instance-mode-accessing-p5-functions-in-an-external-class/35875>\
**Category:** Libraries\
**Created:** [March 21, 2022, 12:06pm UTC](https://discourse.processing.org/t/instance-mode-accessing-p5-functions-in-an-external-class/35875 "2022-03-21T12:06:47Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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 21, 2022, 12:31pm UTC](https://discourse.processing.org/t/instance-mode-accessing-p5-functions-in-an-external-class/35875/2 "2022-03-21T12:31:23Z")

</div>

> [@sylcha](#):
>
> … where I would like to use p5 functionalities,

Request a p5 object as 1 of the constructor’s parameters:

> [@sylcha](#):
>
> ```auto
> class Chiffre {
> constructor(x, y, signe, p = p5.instance) {
> this.p = p;
> this.signe = signe;
> this.position = p.createVector(x, y);
> this.vitesse = p.createVector();
> this.acceleration = p.createVector();
> }
> }
> 
> ```

And pass it when instantiating the class:

> [@sylcha](#):
>
> ```auto
> import { Chiffre } from "./Chiffre.js";
> 
> export const sketch = s => {
> s.chiffre = new Chiffre(20, 20, '2', s);
> };
> 
> ```

The code above should work if that parameter _s_ happens to be of datatype p5.

For more examples of classes which also request a p5 object take a look at the sketch below:

> [@Converting coding challenge 'Self-avoiding walk backtracing' from p5.js to Processing (Java)](https://discourse.processing.org/t/converting-coding-challenge-self-avoiding-walk-backtracing-from-p5-js-to-processing-java/35047/19):
>
> I’m back w/ another “Self Avoiding Walk II” conversion. This time I’m using the [CoffeeScript](https://coffeescript.org/) language which directly transpiles to JavaScript. Processing 2 used to have a [CoffeeScript Mode](https://github.com/fjenett/coffeescript-mode-processing) a long time ago and I’ve ended up learning it. When I later decided to learn [Python Mode](https://py.processing.org/reference/) to help folks here I’ve relied on my previous [CoffeeScript](https://coffeescript.org/) knowledge b/c they have similar syntax. The old [CoffeeScript Mode](https://github.com/fjenett/coffeescript-mode-processing) relied on the [Pjs](https://gotoloop.github.io/processing-js.github.io/reference/) library in order to run its transpiled sketch on the web. Which by the way is …

---

_[View the full topic](https://discourse.processing.org/t/instance-mode-accessing-p5-functions-in-an-external-class/35875)._
