From a595cb78b3932af26f5604799b6b9f15327df40e Mon Sep 17 00:00:00 2001 From: AlexandreSi <32449369+AlexandreSi@users.noreply.github.com> Date: Tue, 3 Oct 2023 11:29:05 +0200 Subject: [PATCH] Avoid defining a new method and add checks on arguments --- GDJS/Runtime/RuntimeInstanceContainer.ts | 70 +++++-------------- GDJS/Runtime/debugger-client/hot-reloader.ts | 2 +- .../Runtime/events-tools/runtimescenetools.ts | 9 ++- GDJS/Runtime/runtimescene.ts | 2 +- GDJS/Runtime/scenestack.ts | 1 + 5 files changed, 27 insertions(+), 57 deletions(-) diff --git a/GDJS/Runtime/RuntimeInstanceContainer.ts b/GDJS/Runtime/RuntimeInstanceContainer.ts index 88ba1e989204..fea0d5555b42 100644 --- a/GDJS/Runtime/RuntimeInstanceContainer.ts +++ b/GDJS/Runtime/RuntimeInstanceContainer.ts @@ -231,14 +231,13 @@ namespace gdjs { } /** - * @deprecated See createObjectsFrom2 that uses Z axis. - * * Create objects from initial instances data (for example, the initial instances * of the scene or the instances of an external layout). * * @param data The instances data * @param xPos The offset on X axis * @param yPos The offset on Y axis + * @param zPos The offset on Y axis * @param trackByPersistentUuid If true, objects are tracked by setting their `persistentUuid` * to the same as the associated instance. Useful for hot-reloading when instances are changed. */ @@ -246,65 +245,30 @@ namespace gdjs { data: InstanceData[], xPos: float, yPos: float, + zPos: float, trackByPersistentUuid: boolean - ) { - for (let i = 0, len = data.length; i < len; ++i) { - const instanceData = data[i]; - const objectName = instanceData.name; - const newObject = this.createObject(objectName); - if (newObject !== null) { - if (trackByPersistentUuid) { - // Give the object the same persistentUuid as the instance, so that - // it can be hot-reloaded. - newObject.persistentUuid = instanceData.persistentUuid || null; - } - newObject.setPosition(instanceData.x + xPos, instanceData.y + yPos); - newObject.setAngle(instanceData.angle); - if ( - gdjs.RuntimeObject3D && - newObject instanceof gdjs.RuntimeObject3D - ) { - if (instanceData.z !== undefined) newObject.setZ(instanceData.z); - if (instanceData.rotationX !== undefined) - newObject.setRotationX(instanceData.rotationX); - if (instanceData.rotationY !== undefined) - newObject.setRotationY(instanceData.rotationY); - } + ): void { + let zOffset: number; + let shouldTrackByPersistentUuid: boolean; - newObject.setZOrder(instanceData.zOrder); - newObject.setLayer(instanceData.layer); - newObject - .getVariables() - .initFrom(instanceData.initialVariables, true); - newObject.extraInitializationFromInitialInstance(instanceData); - } + if (arguments.length === 5) { + zOffset = zPos; + shouldTrackByPersistentUuid = trackByPersistentUuid; + } else { + /** + * Support for the previous signature (before 3D was introduced): + * createObjectsFrom(data, xPos, yPos, trackByPersistentUuid) + */ + zOffset = 0; + shouldTrackByPersistentUuid = arguments[3]; } - } - /** - * Create objects from initial instances data (for example, the initial instances - * of the scene or the instances of an external layout). - * - * @param data The instances data - * @param xPos The offset on X axis - * @param yPos The offset on Y axis - * @param zPos The offset on Z axis - * @param trackByPersistentUuid If true, objects are tracked by setting their `persistentUuid` - * to the same as the associated instance. Useful for hot-reloading when instances are changed. - */ - createObjectsFrom2( - data: InstanceData[], - xPos: float, - yPos: float, - zPos: float, - trackByPersistentUuid: boolean - ) { for (let i = 0, len = data.length; i < len; ++i) { const instanceData = data[i]; const objectName = instanceData.name; const newObject = this.createObject(objectName); if (newObject !== null) { - if (trackByPersistentUuid) { + if (shouldTrackByPersistentUuid) { // Give the object the same persistentUuid as the instance, so that // it can be hot-reloaded. newObject.persistentUuid = instanceData.persistentUuid || null; @@ -316,7 +280,7 @@ namespace gdjs { newObject instanceof gdjs.RuntimeObject3D ) { if (instanceData.z !== undefined) - newObject.setZ(instanceData.z + zPos); + newObject.setZ(instanceData.z + zOffset); if (instanceData.rotationX !== undefined) newObject.setRotationX(instanceData.rotationX); if (instanceData.rotationY !== undefined) diff --git a/GDJS/Runtime/debugger-client/hot-reloader.ts b/GDJS/Runtime/debugger-client/hot-reloader.ts index 2e8bbeb55bd9..3646737d7847 100644 --- a/GDJS/Runtime/debugger-client/hot-reloader.ts +++ b/GDJS/Runtime/debugger-client/hot-reloader.ts @@ -1213,7 +1213,7 @@ namespace gdjs { ) { // Instance was created (or object name changed, in which case it was destroyed previously) // and we verified that runtimeObject does not exist. - runtimeScene.createObjectsFrom2( + runtimeScene.createObjectsFrom( [newInstance], 0, 0, diff --git a/GDJS/Runtime/events-tools/runtimescenetools.ts b/GDJS/Runtime/events-tools/runtimescenetools.ts index 92a4b92e8f09..fe48cd1d904f 100644 --- a/GDJS/Runtime/events-tools/runtimescenetools.ts +++ b/GDJS/Runtime/events-tools/runtimescenetools.ts @@ -293,11 +293,16 @@ namespace gdjs { // trackByPersistentUuid is set to false as we don't want external layouts // instantiated at runtime to be hot-reloaded. - scene.getScene().createObjectsFrom2( + scene.getScene().createObjectsFrom( externalLayoutData.instances, xPos, yPos, - zPos, + /** + * When 3D was introduced, zPos argument was added to the signature. + * Existing calls (in JS events) to createObjectsFromExternalLayout will + * have zPos undefined. So it is set to 0 in that case. + */ + zPos || 0, /*trackByPersistentUuid=*/ false ); diff --git a/GDJS/Runtime/runtimescene.ts b/GDJS/Runtime/runtimescene.ts index c3699ee77741..03061a6fff98 100644 --- a/GDJS/Runtime/runtimescene.ts +++ b/GDJS/Runtime/runtimescene.ts @@ -161,7 +161,7 @@ namespace gdjs { } //Create initial instances of objects - this.createObjectsFrom2( + this.createObjectsFrom( sceneData.instances, 0, 0, diff --git a/GDJS/Runtime/scenestack.ts b/GDJS/Runtime/scenestack.ts index 9b7e4a7fd86e..be2cecbcee6c 100644 --- a/GDJS/Runtime/scenestack.ts +++ b/GDJS/Runtime/scenestack.ts @@ -113,6 +113,7 @@ namespace gdjs { externalLayoutData.instances, 0, 0, + 0, /*trackByPersistentUuid=*/ true );