marshallward / TiledSharp

C# library for parsing and importing TMX and TSX files generated by Tiled, a tile map generation tool
http://marshallward.github.io/TiledSharp
Apache License 2.0
329 stars 87 forks source link

System.NullReferenceException in PropertyDict class #36

Closed EFLFE closed 8 years ago

EFLFE commented 8 years ago

Hi. I get a bug where the map has properties.

Sample:

In Tiled:

In *.tmx file:

...
 <properties>
  <property name="term:00">vallavllalvalvla</property>
 </properties>
...

In visual studio:

Result:

'System.NullReferenceException' in TiledSharp.dll in TiledSharp.PropertyDict..ctor(XContainer xmlProp) in \TiledSharp\src\TiledCore.cs:line 109

Problem solved following the replacement lines of next code:

var pval = p.Attribute("value").Value; to var pval = p.Value;

But, if property have type Attribute, so then pval = "".

I want to know - only i have a mistake? The error occurs when string property have line wrapping.

Tiled v 0.17.0 and TiledSharp v 0.15

marshallward commented 8 years ago

Use [ ] to access the properties in the Dictionary. If m is your map:

var v = m.Properties["term:00"];
marshallward commented 8 years ago

Sorry, I didn't realise that your tmx format was different. This is how they used to look:

<properties>
  <property name="term:00" value="vallavllalvalvla"/>
</properties>

Has this changed in newer versions of Tiled? I will check it out now.

marshallward commented 8 years ago

Tiled 0.17 seems to be producing the format shown in my last comment. Did you edit this manually? If not, could you tell me how you created the property in Tiled?

marshallward commented 8 years ago

It looks like Tiled does understand the format that you wrote, even though it doesn't produce it in that format, so yes, this is definitely a bug. Thanks very much for finding this, I will try to put in a patch today.

marshallward commented 8 years ago

I've put in a patch that should resolve this issue.

EFLFE commented 8 years ago

I'm using the last version of Tield (0.17). What i have described above, occurs when the value is have multiple lines.

<property name="text" value="some text"/>

then

i get

  <property name="text">some text
new line</property>
marshallward commented 8 years ago

Interesting, wonder if libtiled is reacting to the \n... anyway, very good to know, thanks!

bjorn commented 8 years ago

Interesting, wonder if libtiled is reacting to the \n...

libtiled is checking for newlines and will use the other format in that case, to make the property value more readable. It did that since years, but only since Tiled 0.17 is it possible to create multi-line property values from the UI.

I would prefer to always use the same format, but I kept the old format for non-multiline values to keep compatibility. Eventually I'd like to drop the value attribute.

marshallward commented 8 years ago

Thanks very much for the explanation, Bjørn. The only issue I can think is if the default behaviour changes at some point, but otherwise things are hopefully working now.

xSke commented 7 years ago

This is still an issue as of 0.15. PropertyDict.cs checks the "value" property and NREs if that doesn't exist, which it doesn't for multiline strings (it should use the actual property element body instead, in that case).

bjorn commented 7 years ago

@xSke Yes, if you take an old release you're of course going to run into old issues. The fastest way to get the fix is probably to take the source from master, but of course we could ask @marshallward for pushing a new release.

xSke commented 7 years ago

I hadn't noticed the build on NuGet was so old :) Yeah, pushing a new build would probably be a good idea.

marshallward commented 7 years ago

Sorry guys. As you can probably guess, I am not following the Tiled scene as closely as I used to.

I'll try to update NuGet soon. I'm also happy to start stepping back from things if others want to continue using/working on this.