Sign in to follow this  
Andrey Zimin

The syntax of the engine

Recommended Posts

Why at one and the same result. I have to write in different

For example

new BABYLON.Vector3(0, 0, 0)

and

BABYLON.Vector3.Zero()

Or here
It would seem that logic itself dictates that if you create need to put the new modifier, but no

BABYLON.Mesh.CreateSphere

And then when creating the light he there is
 
new BABYLON.HemisphericLight

Please explain to me, or I don't understand something or I need constantly the source code to look at?

How I guess when you need to use NEW and when not?

Share this post


Link to post
Share on other sites
1 minute ago, Andrey Zimin said:

I know!

Then why do you ask :) 

The static functions are just there for your convenience. You don't have to use them. You can create a sphere anyway you like...

Share this post


Link to post
Share on other sites
5 minutes ago, Milton said:

Then why do you ask :) 

The static functions are just there for your convenience. You don't have to use them. You can create a sphere anyway you like...

Probably because of my bad English you didn't understand my question.

The question is not, what is it? But that - As to me in writing the code to figure out when to put NEW, and when not? 

Me that you need to constantly to the source code to look at?

Share this post


Link to post
Share on other sites
18 minutes ago, Milton said:

Then why do you ask :) 

The static functions are just there for your convenience. You don't have to use them. You can create a sphere anyway you like...

If I create a sphere it is logical that I create a new object, but in the code it looks like a simple function. But if I set the position, then there are two solutions, either it will be a new object new BABYLON.Vector3(0, 0, 0or a simple function BABYLON.Vector3.Zero()

Where is the logic?

Share this post


Link to post
Share on other sites
9 minutes ago, Andrey Zimin said:

If I create a sphere it is logical that I create a new object, but in the code it looks like a simple function. But if I set the position, then there are two solutions, either it will be a new object new BABYLON.Vector3(0, 0, 0or a simple function BABYLON.Vector3.Zero()

I remain confused... You are complaining that there is a Zero() function that returns a new Vector3? If you don't like it, don't use it.

Share this post


Link to post
Share on other sites
6 minutes ago, Milton said:

I remain confused... You are complaining that there is a Zero() function that returns a new Vector3? If you don't like it, don't use it.

))) No, no,)) I'm not complaining about the function! How do I determine when to write new and when not if I can't see the source code the BabylonJS? How am I supposed to know?

Share this post


Link to post
Share on other sites

Actually static functions in BJS are helpers to instanciate quickly an object with default parameters. You can do by your own everything that static functions do (so by using "new") if you prefer. Static functions usually use under the hood the class constructor "new"

When you do CreateSphere() ... well, there's no Sphere class in BJS. A sphere is just a kind of mesh with a particular geometry.

The easy way : call to static CreateSphere() function

The personal way : create a new Mesh object, a new VertexData object, set the sphere geometry in the VertexData object, apply the VertexData to the mesh, etc ...

Share this post


Link to post
Share on other sites
3 minutes ago, Andrey Zimin said:

))) No, no,)) I'm not complaining about the function! How do I determine when to write new and when not if I can't see the source code the BabylonJS? How am I supposed to know?

If you don't know if a function is static, you don't know if you can use 'new'. Since JS is not statically typed, maybe you could use typescript within some IDE, they usually immediately tell you the definition.

Share this post


Link to post
Share on other sites

Looking in the documentation we can see if a function is static. Then it's a question of habit.

IF you write new BABYLON.Vector3 (0, 0, 0), it is faster to do BABYLON.Vector3.Zero (); however, if x or y or z is greater than 0, you can not use Vector3.Zero ()

new BABYLON.Vector3.Zero (); will work too. likewise to instantiate a mesh : new BABYLON.Mesh.CreateSphere will also work

Only the documentation can teach you what you need to use. Then with the habit of use you will not need to know if 'new' or not

look here, you can see if a function or property is static

http://doc.babylonjs.com/classes/3.1/mesh#static-createbox-name-size-scene-updatable-sideorientation-rarr-mesh-classes-3-1-mesh-

exemple :

static CreateBox(name, size, scene, updatable, sideOrientation)  Mesh

Share this post


Link to post
Share on other sites

I think the main 'problem' is that Factory methods are capitalized. JS doesn't really lend itself well for that. You have to know that Zero is not a class...
It would be simpler if you could 'new' everything that starts with a Capital, since you can be sure it's a class.

Share this post


Link to post
Share on other sites

Creation Patterns are a software paradigm to make object creation easier and more intuitive - sounds like you got that.  Let your editor do the hard work for you... here are some screenshots from VS Code on Zero and missing new:

-- Zero (you see a new Vector3 is being created for you);

zero.thumb.png.55edf8e1d5cdb69b05714672839fb617.png

-- Calling constructor - I hover the red squiggly line to see that I have forgotten to new it up.
new.thumb.png.a765892d0f1f44af8b412306ae3e510d.png
 

Share this post


Link to post
Share on other sites

also if you don't have the code completion things you can do a "check" by console loging the object after you create it.  If it comes back with its scope as the window object then you need to do that function new in front most likely.  Not in all instances, but as a quick half attempted check. 

Its mainly just experience and looking that crap up.

 

Share this post


Link to post
Share on other sites
5 minutes ago, Pryme8 said:

Its mainly just experience and looking that crap up.

I think there is always a learning curve learning a new API, but BabylonJS is quite consistent.  If it's UPPER_CASE then you know it's a static property, camelCase then it's an instance method, ProperCase and you know it's static...  You can infer a lot from the well designed API :) Pryme8 is right - it's mainly experience!

Share this post


Link to post
Share on other sites
Just now, brianzinn said:

 If it's UPPER_CASE then you know it's a static property, camelCase then it's an instance method, ProperCase and you know it's static...

There is your cheat sheet for BJS.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.