Can we add objects by script?

Hi everyone! I’m completely new to needle engine and this forum. I am using Needle in a personal project and want to borrow the idea with sockets and understand the basics of needle for creating a floorplan by serializables. I am currently struggling how to add a simple Cube in a grid with Typescript. I want to prevent use another toolkit in combination of - like BabylonJS.

Hope someone can help me and please feel free to inform me if I am hasty to asking questions here.

Thanks!

tags: scripting

Original Post on Discord

by user 1087667860928729148

by user 1087667860928729148

Here :

You can instantiate prefab and add it to your gameObject or your scene

by user 224464722878005248

Thanks @AeistDesmos ! I got it. Is it possible to set position of my cube in a grid by something like a coordinate β€œ2x3”?
( {x: number}x{y: number} )

by user 1087667860928729148

Yes by using of the tools of Needle or three
Like

cube.position.set(x, y, z)
Or cube.translateX(x) (same with y and z
Etc

by user 224464722878005248

Great! So I could calculate by this format {x}x{y},
how it should translate my object, right? And translate will always start from 0?

by user 1087667860928729148

Translate start from the actual position of your object (if x is 10 and you translateX(5) then x is 15)
Set use the local position of your object

If you want to use a string to place your object, then yes you need to calculate it like with split(β€˜x’) in js and convert your string to number
Or with {x}x{y} as you said etc

  • the position depend of the scale of your objects, you need to think to it too

by user 224464722878005248

Amazing! How could I add DragControls as component to this object

by user 1087667860928729148

e.g. GameObject.addNewComponent(cube, DragControls)

Hi @marcel :cactus:,
thank you always for quick response :slightly_smiling_face:
I tried that, no error occurs but the cube does not recognize a drag event.

by user 1087667860928729148

It continues just with rotating and moving the camera

by user 1087667860928729148

You need to add an ObjectRaycaster somewhere in the hierarchy too (either the object itself or some parent)

Ok, what is an object raycaster for? There is somewhere a raycaster component in the scene (right panel). But maybe this is not enough?

by user 1087667860928729148

Wait, fancyyy - it works :)) But how? Why is the object raycaster so important?

by user 1087667860928729148

Are you adding the cubes to this.scene or something? Because then they’re outside the ObjectRaycaster. If you want them in the same hierarchy add them to this.gameObject

Ah yeah the script above looks like GridHelper is added to the scene instead of this.gameObject. The Scene is really the root of everything and the componets/hierarchy you load is put inside that

Sorry if the naming is confusing (SceneRoot being inside the scene not the scene itself)

You can name it however you want :slightly_smiling_face:

And the ObjectRaycaster is just the thing that the EventSystem uses to raycast instead of always raycasting the whole scene (that way you have more control by e.g. adding it to a few objects only).

Maybe that component should be added automatically to objects that have DragControls but no raycaster