2D array causing crashes when accessing arrays within array

I’m making a physics based game that breaks down it’s walls into smaller chunks so only the nearest walls are checked for collision, and add them into a 2D array on the fly. I’ve had this working under actionscript 2 and 3 for a long time, so I know how to get it over to haxe/openFL. However, at the point where I need it to check if an array has already been made within the main array, it crashes on startup. The code I have below is how it should look.

public static var grid:Array<Dynamic>; //Have tried it as grid:Array<Array<Object>>;, no success

function register(wall:Floor, x:Int, y:Int)
        {
			var obj:Object = new Object();
			obj.storage2 = new Array();
			if (grid[x] == null) {
				grid[x] = new Array(); //trace shows this array is being made without a problem
			}
			if (grid[x][y] == null) //this line is where the crash occurs
			{
				grid[x][y] = obj;
			}
			grid[x][y].storage2[wall.count] = wall;
			return;
        }

Output:

Build succeeded
Done(0)
haxelib run lime run “project.xml” windows -release
Running process: C:\HaxeToolkit\haxe\haxelib.exe run lime run “project.xml” windows -release
Done(1)

Help is much appreciated, thank you.

I like to use Reflect.field to get values and Reflect.setField to set them. I’ve had problems in the past trying to get and set Dynamic types on targets other then Flash.

Array<Dynamic> definitely won’t work. At the very least it should be Array<Array<Dynamic>>.

For sparse arrays, use Map rather than Array:

public static var grid:Map<Int, Map<Int, Dynamic>>;

function register(wall:Floor, x:Int, y:Int)
        {
			var obj:Object = new Object();
			obj.storage2 = new Array();
			if (grid[x] == null) {
				grid[x] = new Map();
			}
			if (grid[x][y] == null)
			{
				grid[x][y] = obj;
			}
			grid[x][y].storage2[wall.count] = wall;
        }