How do I create an object using script?

Solved 2 replies · 4687 views

pluginsscript

Lizzard 0 likes received

In "load" event at Scene.Map, I would like to create a simple object in loaded scene. Can I do that? How?
Thanks

Answers

Wano 38 likes received Admin Accepted answer

You should check how the event command work, it implies to change some of the map properties to avoid bugs/side effects. It's in EventCommand/CreateObjectInMap.js:

let id = ++Scene.Map.current.maxObjectsID;

let position = Position.createFromVector3(currentState.position);

let globalPortion = position.getGlobalPortion();

Scene.Map.current.allObjects[id] = position;

let newObject = new MapObject(System.MapObject.createFromModelID(this.modelID.getValue(), id), currentState.position);

if (this.isStockID) {

Game.current.variables[this.stockID.getValue(true)] = id;

}

Game.current.getPortionDatas(Scene.Map.current.id, globalPortion).m.push(newObject);

Game.current.getPortionDatas(Scene.Map.current.id, globalPortion).min.push(newObject);

newObject.changeState();

Lizzard 0 likes received

@wano, it's works very well! You can look at my adapted code, in case anyone has the same question!

Captura-de-tela-2023-09-06-182717.png

Back to Questions