UltimateZoneBuilder/Build/UDBScript/Scripts/Examples/reorderthingsindices.js
biwa 5eb438e3ba
Add map scripting capabilities to UDB (#656)
Added map scripting capabilities to UDB (#656). Documentation at https://biwa.github.io/udbscript-docs/
2021-11-28 14:00:24 +01:00

31 lines
No EOL
928 B
JavaScript

`#name Reorder Things Indices`;
`#description Reorderts the thing inddices of the selected things, so that the thing indices are ascending in the order the things were selected.`;
let things = Map.getSelectedThings();
if(things.length < 2)
die('You have to select at least 2 things.');
let sorted = [...things].sort((a, b) => a.index - b.index);
let copies = things.map(t => {
let nt = Map.createThing([ 0, 0 ]);
t.copyPropertiesTo(nt);
return nt;
});
for(let i=0; i < sorted.length; i++)
copies[i].copyPropertiesTo(sorted[i]);
// Create new things and copy the properties of the original thing to them
//things.forEach(t => {
// let nt = Map.createThing(t.position);
// t.copyPropertiesTo(nt);
//});
// Delete the old things. Have to do it in an extra loop sice it'd just fill
// up the old thing indexes
//things.reverse().forEach(t => t.delete());
copies.reverse().forEach(t => t.delete());