Skip to content

_isGlobal is false when it should be true #7781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 17 tasks
quinton-ashley opened this issue Apr 28, 2025 · 3 comments · May be fixed by #7785
Open
1 of 17 tasks

_isGlobal is false when it should be true #7781

quinton-ashley opened this issue Apr 28, 2025 · 3 comments · May be fixed by #7785

Comments

@quinton-ashley
Copy link
Contributor

quinton-ashley commented Apr 28, 2025

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

v1.11.5

Web browser and version

any

Operating system

any

Steps to reproduce this

_isGlobal should be set to true when loading a global instance of p5 but it's always false in "init" addon methods.

This seems to have always been an issue in v1, not just in v1.11.5.

p5.prototype.registerMethod('init', function () {
  console.log(this._isGlobal); // returns false incorrectly
});

p5.prototype.registerMethod('afterSetup', function () {
  console.log(this._isGlobal); // returns true
});

function preload() {
  console.log(_isGlobal); // returns true
}

function setup() {}

function draw() {}
@VANSH3104
Copy link
Contributor

Hi @quinton-ashley,
I wanted to follow up on the issue regarding _isGlobal not being set correctly in p5.js. I have investigated the problem further and confirmed that _isGlobal is being set before the registerMethod('init') and preload() methods are executed.
It seems the root of the issue is that the initialization of _isGlobal occurs too early, before the instance is fully set up, which causes the method executions to reference the incorrect state. After analyzing this, I proposed a solution where I deferred the execution of registerMethod functions until after the instance was fully initialized, particularly ensuring that _isGlobal was properly set after the global bindings were applied. I made some changes but not sure its correct or not

@quinton-ashley
Copy link
Contributor Author

quinton-ashley commented Apr 29, 2025

@VANSH3104 I'd suggest only moving the proper initialization of _isGlobal before the "init" hooks are run. Other initialization code likely does need to run after the "init" hooks to ensure that addon props and functions are added to the global scope.

What is your proposed solution and changes? You didn't link to anything in your comment.

@VANSH3104 VANSH3104 linked a pull request Apr 29, 2025 that will close this issue
1 task
@VANSH3104
Copy link
Contributor

VANSH3104 commented Apr 29, 2025

@quinton-ashley Here's what I was thinking — it's not a perfect solution, but I opened the PR to demonstrate the idea and get your feedback.
The original code used this loop:

this._registeredMethods.init.forEach(function(f) {
  if (typeof f !== 'undefined') {
    f.call(this);
  }
}, this);

This passes the p5 instance as this, but it doesn't account for global mode, which causes issues for addons relying on the correct context during init.

To fix this, I moved the initialization of this._isGlobal = !sketch to the top of the p5 constructor—before the init hooks run—so that any addon methods depending on _isGlobal get the correct value.

I also replaced the manual forEach loop with a call to callRegisteredHooksFor('init'), which handles context switching correctly (window in global mode, this in instance mode). This ensures consistent and expected behavior for addon init methods.
It’s not a perfect solution yet, but it addresses the main issue.
i use this._isGlobal = !sketch || hasGlobalSetupOrDraw to automatically detect if the user is writing their p5 sketch in global mode, ensuring the library behaves correctly without requiring manual configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants