Issue with FlxCaveGenerator

27 views
Skip to first unread message

Florian Boeuf

unread,
Apr 14, 2015, 9:21:58 AM4/14/15
to haxef...@googlegroups.com
Hi everyone!

I am not sure where to report bug and issues on addons, so I am doing it here, unless someone can point me in the correct direction.

I am using FlxCaveGenerator addon in combination with FlxTileMap. The demo is working like a charm (http://haxeflixel.com/demos/FlxCaveGenerator/) so I tried doing the same with my own tileset. And it started crashing on my face. It happens only when autoTiling is activated.

After some investigation I discovered that the issue seems to be linked to the platform (I am using Neko for dev). The fact is that FlxCaveGenerator generate a CSV-like string, basically an array of int separated by commas and new-lines. 
public static function convertMatrixToString(Matrix:Array<Array<Int>>):String
{
var mapString:String = "";
for (y in 0...Matrix.length)
{
for (x in 0...Matrix[y].length)
{
mapString += Std.string(Matrix[y][x]) + ",";
}
mapString += "\n";
}
return mapString;
}

As you can see, this code in FlxCaveGenerator (line 20 or so) adds an extra \n at the end of the string. It is important because FlxTileMap parse this string using String.split() (line 315 approx.)

// Populate data if MapData is a CSV string
if (Std.is(MapData, String))
{
// Figure out the map dimensions based on the data string
_data = new Array<Int>();
var columns:Array<String>;
var rows:Array<String> = MapData.split("\n");
heightInTiles = rows.length;
widthInTiles = 0;
var row:Int = 0;
var column:Int;
while (row < heightInTiles)
{
columns = rows[row++].split(",");
if (columns.length < 1)
{
heightInTiles = heightInTiles - 1;
continue;
}
if (widthInTiles == 0)
{
widthInTiles = columns.length;
}
column = 0;
while (column < widthInTiles)
{
//the current tile to be added:
var curTile:Int = Std.parseInt(columns[column]);

//if neko, make sure the value was not null, and if it is null,
//make sure it is the last in the row (used to ignore commas)
#if neko
if (curTile != null)
{
_data.push(curTile);
column++;
}
else if (column == columns.length - 1)
{
//if value was a comma, decrease the width by one
widthInTiles--;
}
else
{
//if a non-int value was passed not at the end, warn the user
throw "Value passed wan NaN";
}
#else
//if not neko, dont worry about the comma
_data.push(curTile);
column++;
#end
}
}
}


Now look at String.split() documentation:

Having a \n at the end of the string is interpreted by the algorithm as an additional empty line. Under Neko, result return is [""], which means the check on columns.length < 1 passes and the algorithm continues. Then, it crashes later on because autoTile() attempts to perform a % operation with a value of 0.

Anyway, I have found two possibles fixes.

Either you can change FlxCaveGenerator convertMatrixToString.
public static function convertMatrixToString(Matrix:Array<Array<Int>>):String
{
var mapString:String = "";
for (y in 0...Matrix.length)
{
for (x in 0...Matrix[y].length)
{
mapString += Std.string(Matrix[y][x]) + ",";
}
if(y < Matrix.length - 1)
{
mapString += "\n";
}
}
return mapString;
}

My favorite choice as i makes more sense on the data reprensentation level.

Or you can change FlxTileMap safety check from:
columns = rows[row++].split(",");
if (columns.length < 1)
{
heightInTiles = heightInTiles - 1;
continue;
}

to =>
columns = rows[row++].split(",");
if (columns.length < 1 || (columns.length == 1 && columns[0] == ""))
{
heightInTiles = heightInTiles - 1;
continue;
}


Hope it helps!



Reply all
Reply to author
Forward
0 new messages