Javascript -> create an Object by Name

hello everyone, i would like to create an object at a given X/Y-position and layer/z-Index. (X/Y of that object that holds the behavior-script)

ive tried “runtimeScene.createObject(obj)” it works, but it spawns the object somewhere random with random z-index on a random layer and position. i thought i would have a look at the other method
createObjectsFrom but i dont understand this method especially the InstanceData paramter.

wiki: https://docs.gdevelop-app.com/GDJS%20Runtime%20Documentation/gdjs.RuntimeScene.html#createObjectsFrom

can anyone explain the parameters of this method? or give me an example how to use this method/where its been used already? or another way of creating an object?

thanks in advance

RuntimeScene.createObjecsFrom is an internal methods that creates objects from serialized data coming from the IDE. I am pretty sure you wouldn’t need it here.

To set the position and zOrder and whatever simply use the methods on the object:

const obj = runtimeScene.createObject("MyObjectName"); // returns the gdjs.RuntimeObjects
obj.setPosition(69, 420); // https://docs.gdevelop-app.com/GDJS%20Runtime%20Documentation/gdjs.RuntimeObject.html#setX
obj.setLayer("MyCoolLayer"); // https://docs.gdevelop-app.com/GDJS%20Runtime%20Documentation/gdjs.RuntimeObject.html#setLayer
// etc...
5 Likes