Andrey Zimin Posted February 1, 2018 Share Posted February 1, 2018 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? Quote Link to comment Share on other sites More sharing options...
Milton Posted February 1, 2018 Share Posted February 1, 2018 You use 'new' to instantiate an object. A 'Zero()' or 'CreateSphere()' is not an object. They are static functions. Andrey Zimin 1 Quote Link to comment Share on other sites More sharing options...
Andrey Zimin Posted February 1, 2018 Author Share Posted February 1, 2018 34 minutes ago, Milton said: You use 'new' to instantiate an object. A 'Zero()' or 'CreateSphere()' is not an object. They are static functions. I know! Milton 1 Quote Link to comment Share on other sites More sharing options...
Milton Posted February 1, 2018 Share Posted February 1, 2018 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... Quote Link to comment Share on other sites More sharing options...
Andrey Zimin Posted February 1, 2018 Author Share Posted February 1, 2018 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? Quote Link to comment Share on other sites More sharing options...
Andrey Zimin Posted February 1, 2018 Author Share Posted February 1, 2018 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, 0) or a simple function BABYLON.Vector3.Zero() Where is the logic? Quote Link to comment Share on other sites More sharing options...
Milton Posted February 1, 2018 Share Posted February 1, 2018 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, 0) or 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. Quote Link to comment Share on other sites More sharing options...
Andrey Zimin Posted February 1, 2018 Author Share Posted February 1, 2018 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? Quote Link to comment Share on other sites More sharing options...
jerome Posted February 1, 2018 Share Posted February 1, 2018 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 ... Andrey Zimin and Pryme8 1 1 Quote Link to comment Share on other sites More sharing options...
Milton Posted February 1, 2018 Share Posted February 1, 2018 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. Quote Link to comment Share on other sites More sharing options...
Dad72 Posted February 1, 2018 Share Posted February 1, 2018 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 Andrey Zimin 1 Quote Link to comment Share on other sites More sharing options...
Milton Posted February 1, 2018 Share Posted February 1, 2018 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. Andrey Zimin 1 Quote Link to comment Share on other sites More sharing options...
Guest Posted February 1, 2018 Share Posted February 1, 2018 I guess the best option is to rely on intelisense in tools like VSCode: http://doc.babylonjs.com/How_To/how_to_start Quote Link to comment Share on other sites More sharing options...
brianzinn Posted February 1, 2018 Share Posted February 1, 2018 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); -- Calling constructor - I hover the red squiggly line to see that I have forgotten to new it up. GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted February 1, 2018 Share Posted February 1, 2018 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. brianzinn 1 Quote Link to comment Share on other sites More sharing options...
brianzinn Posted February 1, 2018 Share Posted February 1, 2018 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! Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted February 1, 2018 Share Posted February 1, 2018 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.