Jump to content

The syntax of the engine


Andrey Zimin
 Share

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?

Link to comment
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?

Link to comment
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?

Link to comment
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.

Link to comment
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?

Link to comment
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 ...

Link to comment
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.

Link to comment
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

Link to comment
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
 

Link to comment
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.

 

Link to comment
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!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...