About static methods in P5js

Hello,

Is it possible to access P5js functions in a static functions ?

I tried in this sketch but it doesnt work

"use strict";

function setup() {
  createCanvas(800, 600);
  fill(255);
}


function draw() {
  background(0);
  text(V.get("Hello", "number", this), 100, 100);
}

class V {
  static verif=true;

  static get(a, s, n) { // (anyType, type in a string, this) : anyType
    if (!V.verif) {
      return a;
    } else if ( typeof a === s) {
      return a;
    } else {
      let type = typeof a;
      bugMessage("Erreur V, in : "+n+", asked : "+s+", but it was : "+type,true);
      return 0;
    }
  }
}


function bugMessage(aString, aBoolean) {
  print(aString);
  if (aBoolean) {
    noLoop();
  }
}

I haven’t any error in the browser console

but it shows me something in P5 and it doesn’t work :

I wanted the sketch to stop (noLoop()) and my error message written in the console.

it works when I don’t use bugMessage(), it doesn’t work when I have noLoop() in the static method either.

I tried copy and pasting your code into the p5 web editor, and besides having to add p5.disableFriendlyErrors = true at the top due to FES not parsing the class with initialized properties in it (on the latest release, to be fixed soon!), it seems to work for me: Golden saga by davepagurek -p5.js Web Editor

1 Like