# Equivalent of p5's "on-demand global mode" for regular (Java) Processing?

**URL:** <https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701>\
**Category:** Beginners\
**Created:** [September 6, 2020, 1:07am UTC](https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701 "2020-09-06T01:07:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablohotsauce](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pablohotsauce/32/10113_2.png) [@pablohotsauce](https://discourse.processing.org/u/pablohotsauce)\
**Post date:** [September 6, 2020, 1:07am UTC](https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701/1 "2020-09-06T01:07:24Z")

</div>

Is there an equivalent of p5’s on-demand global mode …

```auto
new p5(); // in global space
```

… but for regular (Java) Processing? I want to be able to use all Processing functions / methods outside of `setup()` and `draw()`. Searched, didn’t find anything relevant.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [September 6, 2020, 4:57am UTC](https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701/2 "2020-09-06T04:57:45Z")

</div>

If you use processing ide then processing functions and constants are always available. In processing ide all code is within processing scope.

If you are using external IDE then you need to pass instance of processing as a parameter in a method and you can call processing methods with that parameter

---

<div class="post-metadata">

**Author:** ![pablohotsauce](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pablohotsauce/32/10113_2.png) [@pablohotsauce](https://discourse.processing.org/u/pablohotsauce)\
**Post date:** [September 6, 2020, 5:59am UTC](https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701/3 "2020-09-06T05:59:24Z")

</div>

Thanks. Yup, I’m using the Processing IDE.

Well then, maybe my syntax is wrong. Say I want to be able to, for example, create a global `PFont` var **and** assign it at the same time, without having to assign it in `setup()`. In Processing, this sample code doesn’t work:

```auto
PFont myFont = createFont("Helvetica.otf", 18); // error
void setup() {
    (etc)
}

```

Whereas the equivalent code in p5 with on-demand global mode works fine:

```auto
new p5(); // necessary for the following line:
let myFont = loadFont("Helvetica.otf"); // works
function setup() {
    (etc)
}

```

Is this doable in Processing (Java) or no?

---

<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:** [September 6, 2020, 6:02am UTC](https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701/4 "2020-09-06T06:02:59Z")

</div>

Internal field _sketchPath_ is initialized just before code reaches **settings()** callback.

Any method depending on it can’t be used before **settings()**.

---

<div class="post-metadata">

**Author:** ![pablohotsauce](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pablohotsauce/32/10113_2.png) [@pablohotsauce](https://discourse.processing.org/u/pablohotsauce)\
**Post date:** [September 6, 2020, 6:20am UTC](https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701/5 "2020-09-06T06:20:13Z")

</div>

> [@GoToLoop](#):
>
> Internal field _sketchPath_ is initialized just before code reaches **settings()** callback.
> 
> Any method depending on it can’t be used before **settings()**.

Are you saying I can assign the var in global space by placing it after either `settings()` or `setup()`? Just tried different combinations of both, it still threw up an error. E.g.:

```auto
void settings() {
    size(800, 600);
}
void setup() {
   (whatever)
}
PFont myFont = createFont("Helvetica.otf", 18); // nope

```

Or you’re saying the assignment simply can’t be done outside of `settings()` or `setup()`?

---

<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:** [September 6, 2020, 6:25am UTC](https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701/6 "2020-09-06T06:25:16Z")

</div>

Method PApplet::**createFont()** (all loading methods btW) depends on field PApplet::_sketchPath_.

PApplet::**createFont()** is ready earliest within callback PApplet::**settings()**.

---

<div class="post-metadata">

**Author:** ![pablohotsauce](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pablohotsauce/32/10113_2.png) [@pablohotsauce](https://discourse.processing.org/u/pablohotsauce)\
**Post date:** [September 6, 2020, 7:02am UTC](https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701/7 "2020-09-06T07:02:25Z")

</div>

OK, I looked up info on `sketchPath()`, and I invoked the function directly in my sketch. It returns the sketch’s correct path only when the invocation is within either `settings()` or `setup()`, but not in global space. So, that’s why the `PFont` assignment can’t be done in global? Is there some voodoo workaround that can be done to make the correct sketch path accessible in global, or no?

Also, does something like `createShape` rely on the sketch path too? The following example doesn’t work in Java Processing either, but … why not? Is `createShape` writing to / reading from the sketch path?

```auto
PShape myShape = createShape(RECT, 0, 0, 60, 60); // error
void setup() {
    (etc)
}

```

---

<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:** [September 6, 2020, 7:45am UTC](https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701/8 "2020-09-06T07:45:25Z")

</div>

> [@pablohotsauce](#):
>
> Also, does something like `createShape` rely on the sketch path too?

No, but it relies on the canvas to be properly initiated.

So your code needs to reach callback **setup()** before using **createShape()**.

---

<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:** [September 6, 2020, 7:49am UTC](https://discourse.processing.org/t/equivalent-of-p5s-on-demand-global-mode-for-regular-java-processing/23701/9 "2020-09-06T07:49:49Z")

</div>

> [@pablohotsauce](#):
>
> , but not in global space.

Java doesn’t have an actual global space.

Instead we’ve got a space where we can declare fields inside a class, which runs before its constructor.

Everything inside a “.pde” file belongs to a PApplet subclass.
