ceramic-engine / ceramic

Cross-platform 2D framework written in Haxe that can export natively to desktop (windows, mac, linux), mobile (ios, android), web (js + webgl) and to unity projects
MIT License
263 stars 22 forks source link

[ldtk] parse array type values in LdtkFieldInstance #123

Closed Gaboose closed 1 year ago

Gaboose commented 1 year ago

I noticed that if a field type is an array, value is set to rawValue. That's because

var isArray = type.startsWith('Array<');
if (isArray) {
    type = type.substring(1, type.length - 1);
}

transforms a type like Array<EntityRef> to rray<EntityRef. And that triggers the default switch arm, which is value = rawValue.

This change fixes the substring call to type.substring(6, type.length - 1) and converts/parses each item in the array individually.

trace(value) of an example Array<EntityRef> field type prints:

before the change

[{
    entityIid : a6ef0200-3b70-11ee-9c74-6bbcb7dc7313, 
    layerIid : 2bfdcfb0-c640-11ed-86f5-fb5c0168c734, 
    levelIid : d9fd3dc0-c640-11ed-8339-fd6e013c85c7, 
    worldIid : 963995f0-1460-11ee-8ba5-d7a793b95fd9
},{
    entityIid : a7d54350-3b70-11ee-9c74-c5e2355e556d, 
    layerIid : 2bfdcfb0-c640-11ed-86f5-fb5c0168c734, 
    levelIid : d9fd3dc0-c640-11ed-8339-fd6e013c85c7, 
    worldIid : 963995f0-1460-11ee-8ba5-d7a793b95fd9
}]

after the change

[LdtkEntityInstance(identifier=Rock def=... gridX=10 gridY=14 pxX=160 pxY=224 fieldInstances=[LdtkFieldInstance(identifier=sprite_states value=[idle 2] tile=null),LdtkFieldInstance(identifier=dialog value=null tile=null)] width=48 height=48 iid=a6ef0200-3b70-11ee-9c74-6bbcb7dc7313),LdtkEntityInstance(identifier=Rock def=... gridX=7 gridY=14 pxX=112 pxY=224 fieldInstances=[LdtkFieldInstance(identifier=sprite_states value=[idle 2] tile=null),LdtkFieldInstance(identifier=dialog value=null tile=null)] width=48 height=48 iid=a7d54350-3b70-11ee-9c74-c5e2355e556d)]
jeremyfa commented 1 year ago

Ah good catch :) I didn't really test the Array type yet as you can see, so thank you for proofreading and fixing my code 😄

Gaboose commented 1 year ago

No problem! Just ran into it while working on my own little project. Really cool engine ❤️