Skip to content

Duplicate level crashes with symetrical entity references (proposed fix included along with repo file). #1216

Description

@amirrajan

repro.ldtk.json

Repro / sample file attached.

Image
  1. Load file.
  2. Right click level and select duplicate. Exception occurs.

Possible fix:

diff --git a/src/electron.renderer/data/World.hx b/src/electron.renderer/data/World.hx
index b8afa9b8..93fb3598 100644
--- a/src/electron.renderer/data/World.hx
+++ b/src/electron.renderer/data/World.hx
@@ -170,7 +170,7 @@ class World {
 						var oldIid = fi.getEntityRefIid(i);
 						if( remaps.exists(oldIid) ) {
 							var tei = _project.getEntityInstanceByIid( remaps.get(oldIid) );
-							fi.setEntityRefTo(i, ei, tei);
+							fi.setEntityRefTo(i, ei, tei, false);
 							i++;
 						}
 						else {
diff --git a/src/electron.renderer/data/inst/FieldInstance.hx b/src/electron.renderer/data/inst/FieldInstance.hx
index 98a7e6e1..5e5a2129 100644
--- a/src/electron.renderer/data/inst/FieldInstance.hx
+++ b/src/electron.renderer/data/inst/FieldInstance.hx
@@ -636,7 +636,7 @@ class FieldInstance {
 				+ ( checkLevel==null || checkLevel!=ei._li.level ? " in "+ei._li.level.identifier : "" );
 	}
 
-	public function setEntityRefTo(arrayIdx:Int, sourceEi:EntityInstance, targetEi:EntityInstance) {
+	public function setEntityRefTo(arrayIdx:Int, sourceEi:EntityInstance, targetEi:EntityInstance, tidy=true) {
 		var oldTargetEi = getEntityRefInstance(arrayIdx);
 		_project.unregisterReverseIidRef(sourceEi, oldTargetEi);
 
@@ -671,9 +671,11 @@ class FieldInstance {
 		// setSymmetricalRef(arrayIdx, sourceEi);
 
 		// Tidy lost symmetries
-		if( oldTargetEi!=null )
-			oldTargetEi.tidyLostSymmetricalEntityRefs(def);
-		targetEi.tidyLostSymmetricalEntityRefs(def);
+		if( tidy ) {
+			if( oldTargetEi!=null )
+				oldTargetEi.tidyLostSymmetricalEntityRefs(def);
+			targetEi.tidyLostSymmetricalEntityRefs(def);
+		}
 	}
 
 	public function getTileRectObj(arrayIdx:Int) : Null<ldtk.Json.TilesetRect> {

Stack:

LDtk version: 1.5.3-
Cannot read properties of null (reading 'iid')
TypeError
TypeError: Cannot read properties of null (reading 'iid')
    at data_inst_FieldInstance.setEntityRefTo (file:///Applications/LDtk.app/Contents/Resources/app.asar/assets/js/renderer.js:49118:37)
    at data_World.duplicateLevel (file:///Applications/LDtk.app/Contents/Resources/app.asar/assets/js/renderer.js:37818:13)
    at Object.cb (file:///Applications/LDtk.app/Contents/Resources/app.asar/assets/js/renderer.js:27286:95)
    at HTMLButtonElement.<anonymous> (file:///Applications/LDtk.app/Contents/Resources/app.asar/assets/js/renderer.js:291276:8)
    at HTMLButtonElement.dispatch (/Applications/LDtk.app/Contents/Resources/app.asar/assets/js/jquery.min.js:2:43064)
    at v.handle (/Applications/LDtk.app/Contents/Resources/app.asar/assets/js/jquery.min.js:2:41048)

Processes:

#0 App
 |--- #3 page.Editor
       |--- #4 misc.FileWatcher
       |--- #5 display.WorldRender
       |--- #6 display.LevelRender [PAUSED]
       |--- #7 display.Camera
       |--- #8 display.Rulers[--]
       |--- #9 tool.SelectionTool[--]
       |--- #10 tool.lt.DoNothing[--]
       |--- #11 WorldTool
       |--- #12 tool.PanView[--]
       |--- #13 tool.lt.EntityTool[--]
       |--- #24 ui.modal.panel.WorldPanel
       |--- #28 ui.modal.ContextMenu

Log:

[BOOT]         Start args: path=null levelIndex=null
[BOOT]         Loading Home...
[GENERAL]      Page started: page.Home()
[FILE]         Loading page template: home from /Applications/LDtk.app/Contents/Resources/app.asar/assets/tpl/pages/home.html
[DEBUG]        samplesDir=/Applications/LDtk.app/Contents/extraFiles/samples
[UPDATE]       Found 1.5.3
[UPDATE]       No new update.
[GENERAL]      Progress created.
[GENERAL]      Progress started: : "repro.ldtk: Project...", 5 operation(s)
[LOAD]         Loading project repro.ldtk...
[LOAD]           Project appBuildId=473703 appJsonVersion=1.5.3 jsonVersion=1.5.3
[LOAD]         Loading complete.
[GENERAL]      Page started: page.Editor()
[FILE]         Loading page template: editor from /Applications/LDtk.app/Contents/Resources/app.asar/assets/tpl/pages/editor.html
[FILE]         Cleared all file watches
[EVENT]        LayerInstanceSelected 2
[EVENT]        ProjectSelected
[WARNING]      Full level render requested
[RENDER]       Rendering all world...
[RENDER]       Rendering world bg...
[RENDER]       Rendering world bounds...
[RENDER]       Rendering world level data.Level#c7f289b0-21a0-11f1-92ef-3b27c66ebd65 "Level_0"...
[RENDER]       Rendering world level fields data.Level#c7f289b0-21a0-11f1-92ef-3b27c66ebd65 "Level_0"...
[RENDER]       Rendered level UI
[RENDER]       Rendering world bg...
[GENERAL]      Progress completed: "repro.ldtk: Project..." (0.1s)
[RENDER]       Rendering world bg...
[RENDER]       Rendering world bg...
[USERACTION]   Opened panel #24 ui.modal.panel.WorldPanel
[FILE]         Loading HTML template worldPanel
[FILE]         Loading HTML template levelInstanceForm
[RENDER]       Rendering world bg...
[RENDER]       Rendering world bg...
[RENDER]       Rendering world level data.Level#c7f289b0-21a0-11f1-92ef-3b27c66ebd65 "Level_0"...
[RENDER]       Rendering world level fields data.Level#c7f289b0-21a0-11f1-92ef-3b27c66ebd65 "Level_0"...
[RENDER]       Rendering world bg...
[RENDER]       Rendering world bg...
[EVENT]        LevelSelected 0
[USERACTION]   Opened level data.Level#c7f289b0-21a0-11f1-92ef-3b27c66ebd65 "Level_0"
[RENDER]       Rendering world bg...
[RENDER]       Rendering world level data.Level#c7f289b0-21a0-11f1-92ef-3b27c66ebd65 "Level_0"...
[RENDER]       Rendering world level fields data.Level#c7f289b0-21a0-11f1-92ef-3b27c66ebd65 "Level_0"...
[GENERAL]      Page started: page.CrashReport()
[FILE]         Loading page template: crashReport from /Applications/LDtk.app/Contents/Resources/app.asar/assets/tpl/pages/crashReport.html
[ERROR]        Cannot read properties of null (reading 'iid') (TypeError)
[ERROR]        TypeError: Cannot read properties of null (reading 'iid')
    at data_inst_FieldInstance.setEntityRefTo (file:///Applications/LDtk.app/Contents/Resources/app.asar/assets/js/renderer.js:49118:37)
    at data_World.duplicateLevel (file:///Applications/LDtk.app/Contents/Resources/app.asar/assets/js/renderer.js:37818:13)
    at Object.cb (file:///Applications/LDtk.app/Contents/Resources/app.asar/assets/js/renderer.js:27286:95)
    at HTMLButtonElement.<anonymous> (file:///Applications/LDtk.app/Contents/Resources/app.asar/assets/js/renderer.js:291276:8)
    at HTMLButtonElement.dispatch (/Applications/LDtk.app/Contents/Resources/app.asar/assets/js/jquery.min.js:2:43064)
    at v.handle (/Applications/LDtk.app/Contents/Resources/app.asar/assets/js/jquery.min.js:2:41048)

[GENERAL]      
#0 App
 |--- #3 page.Editor
       |--- #4 misc.FileWatcher
       |--- #5 display.WorldRender
       |--- #6 display.LevelRender [PAUSED]
       |--- #7 display.Camera
       |--- #8 display.Rulers[--]
       |--- #9 tool.SelectionTool[--]
       |--- #10 tool.lt.DoNothing[--]
       |--- #11 WorldTool
       |--- #12 tool.PanView[--]
       |--- #13 tool.lt.EntityTool[--]
       |--- #24 ui.modal.panel.WorldPanel
       |--- #28 ui.modal.ContextMenu
 |--- #29 page.CrashReport

[FILE]         Cleared all file watches

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions