Skip to content

Commit

Permalink
Avoid defining a new method and add checks on arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreSi committed Oct 3, 2023
1 parent da8e527 commit a595cb7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 57 deletions.
70 changes: 17 additions & 53 deletions GDJS/Runtime/RuntimeInstanceContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,80 +231,44 @@ 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.
*/
createObjectsFrom(
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;
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion GDJS/Runtime/debugger-client/hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions GDJS/Runtime/events-tools/runtimescenetools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion GDJS/Runtime/runtimescene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ namespace gdjs {
}

//Create initial instances of objects
this.createObjectsFrom2(
this.createObjectsFrom(
sceneData.instances,
0,
0,
Expand Down
1 change: 1 addition & 0 deletions GDJS/Runtime/scenestack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ namespace gdjs {
externalLayoutData.instances,
0,
0,
0,
/*trackByPersistentUuid=*/
true
);
Expand Down

0 comments on commit a595cb7

Please sign in to comment.