Make scene param optional for NodeMaterial.ParseFromSnippetAsync?

Can the scene parameter to NodeMaterial.ParseFromSnippetAsync be optional like it is for ParseFromFileAsync? The latter passes the scene through to the constructor which defaults it to EngineStore.LastCreatedScene, but the former will produce a run time error.

It can be convenient though, especially when prototyping/testing, to leave out that param and unexpected that it doesn’t work IMO…

Fancy doing a PR ???

Happy to but I’m not sure what the preferred coding style here is thou? To keep TS happy seems need to either assign the default scene in the parameter declaration or use ! after each usage of scene in the inner function… :thinking:

Because if I do it like below at the start of the function then I have to do that again in the inner function for the event handler (or use ! after each usage in the event handler). But if assign it when the param is declared then it fixes it for both functions (that way seems simpler to me but not sure if it’s the preferred way).

scene = scene ?? EngineStore.LastCreatedScene!;

yup this is actually what we do in most of the other constructors, you can check in Material or Node for instance.

I did but they don’t have inner functions using the parameter which complicates the matter so still not sure which of the 3 ways I proposed is the preferred way? Or some other way I’ve haven’t seen or thought of?

The problem seems to be that TS doesn’t look to the outer function to see if it already guarded against null and undefined, so have to guard against it a second time in the inner function e.g. by repeating that same line again or by using ! each time it’s used in the inner function.

Or can just assign it in the param declaration instead and then no need to deal with it again in the inner functions…

Okay so here’s a PR for one way to do it, then can adjust if needed. Going for simplicity and less code, I handled the default param value in the param declaration, that way the matter doesn’t have to be revisited in the inner function. I just had to split it into multiple lines since the line got too long with that addition…

1 Like

looks all great to me :slight_smile: