Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to set Z offset when creating instances from external layout #5704

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Core/GDCore/Extensions/Builtin/ExternalLayoutsExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ BuiltinExtensionsImplementer::ImplementsExternalLayoutsExtension(
.SetDefaultValue("0")
.AddParameter("expression", _("Y position of the origin"), "", true)
.SetDefaultValue("0")
.AddParameter("expression", _("Z position of the origin"), "", true)
.SetDefaultValue("0")
.MarkAsAdvanced();
}

Expand Down
24 changes: 21 additions & 3 deletions GDJS/Runtime/RuntimeInstanceContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,38 @@ namespace gdjs {
* @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.
*/
createObjectsFrom(
data: InstanceData[],
xPos: float,
yPos: float,
zPos: float,
trackByPersistentUuid: boolean
) {
): void {
let zOffset: number;
let shouldTrackByPersistentUuid: boolean;

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];
}

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 @@ -262,7 +279,8 @@ namespace gdjs {
gdjs.RuntimeObject3D &&
newObject instanceof gdjs.RuntimeObject3D
) {
if (instanceData.z !== undefined) newObject.setZ(instanceData.z);
if (instanceData.z !== undefined)
newObject.setZ(instanceData.z + zOffset);
if (instanceData.rotationX !== undefined)
newObject.setRotationX(instanceData.rotationX);
if (instanceData.rotationY !== undefined)
Expand Down
1 change: 1 addition & 0 deletions GDJS/Runtime/debugger-client/hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,7 @@ namespace gdjs {
[newInstance],
0,
0,
0,
/*trackByPersistentUuid=*/
true
);
Expand Down
9 changes: 8 additions & 1 deletion GDJS/Runtime/events-tools/runtimescenetools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ namespace gdjs {
scene: gdjs.RuntimeInstanceContainer,
externalLayout: string,
xPos: float,
yPos: float
yPos: float,
zPos: float
) {
const externalLayoutData = scene
.getGame()
Expand All @@ -296,6 +297,12 @@ namespace gdjs {
externalLayoutData.instances,
xPos,
yPos,
/**
* 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
1 change: 1 addition & 0 deletions GDJS/Runtime/runtimescene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ namespace gdjs {
sceneData.instances,
0,
0,
0,
/*trackByPersistentUuid=*/
true
);
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