Console.warn for calling Scene.getNodeById with missing entity

At the moment finding entities like Meshes, Nodes, Materials by ID will return null if no object is found.

That’s fine but generally I’ve found that later down the line, this unexpected null object causes in an exception. Certainly in my case, if I try to find a Node which doesn’t exist by ID then it’s probably a bug in my code. Ideally I’d prefer to see at least a warning in the console - and in fact I have monkey patched this into my own project. I wondered if it would be useful as a feature for others (I’m happy to work it up to a pull request)?

I propose something like:

class Scene {
  ...
  /**
   * When true will issue a console.warn message when trying to
   * get a Node by ID which doesn't exist using getNodeById etc.
  */
  public static WarnOnEntityNotFound = false;

  public getMeshById(id: string): Nullable<AbstractMesh> {
        for (let index = 0; index < this.meshes.length; index++) {
            if (this.meshes[index].id === id) {
                return this.meshes[index];
            }
        }
      
        if(Scene.WarnOnEntityNotFound) console.warn(`No Mesh found with ID:'${id}'`);

        return null;
    }


}

I’d welcome comments and feedback on this idea!

I think this is very specific to your use case and should not be a part of the framework itself. You can wrap the function and log the error yourself. WOuld that work?

1 Like

Absolutely, I’m completely happy if it’s not of general use.

1 Like