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

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

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.

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

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:

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

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

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

Is this doable in Processing (Java) or no?

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.:

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

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

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

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

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?

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

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

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

1 Like

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.

1 Like