Arrays of arrays and the Q6 Save/Load system

A few more trials and tribulations...
So as it turns out, arrays of arrays are not currently supported in QJS (NOTE: my game is built off of a heavily modified v0.7 that I didn't want to update to newer versions because of said edits, but this is, as far as I can tell, still true in v1.1) - they simply aren't retained across loads or even undos and become undefined. That was a bit of a bummer.

Just in case this helps anyone else, I created a few edits to the SaveLoad system to make 2D arrays of strings supported. If anyone wants 2D arrays of numbers, this could be extended to those as well with a few tweaks. These are the modifications I had to make (all in _saveload.js):

  1. In the encode(key, value) function, I added a check for 2D arrays and a new attType to recognize 2D arrays:
if (Array.isArray(value)) {
      if (value.length === 0) return key + ":emptyarray;";
      //NEXT LINE ADDED
      if (Array.isArray(value[0])) return key + ":2darray:"+saveLoad.encode2DArray(value) + ";";
      if (typeof value[0] === 'string') return key + ":array:" + saveLoad.encodeArray(value) + ";";
      if (typeof value[0] === 'number') return key + ":numberarray:" + saveLoad.encodeNumberArray(value) + ";";
      return '';
    }
  1. I created functions that can encode and decode 2D arrays, using the '%' symbol as a new identifier for separating the second array dimension:
  encode2DArray:function(ary) {
	let ary2d = [];
	for(let i=0;i < ary.length;i++){
	  ary2d.push(saveLoad.encodeArray(ary[i]));
	}
    return ary2d.join('%');
  },

  decode2DArray:function(s) {
	  let ary1d = s.split('%');
	  let ary2d = [];
	  for(let i = 0; i < ary1d.length;i++){
		  ary2d.push(ary1d[i].split('~'));
	  }
	  return ary2d;
  },
  1. In the decode(hash, str) function, I added an element to properly call the decode2DArray function for "2darray" type attributes:
else if (attType === "2darray") {
	  hash[key] = saveLoad.decode2DArray(s)
}
  1. I added an escape for '%' characters in text:
  replacements:[
    { unescaped:':', escaped:'cln'},
    { unescaped:';', escaped:'scln'},
    { unescaped:'!', escaped:'exm'},
    { unescaped:'=', escaped:'eqs'},
    { unescaped:'~', escaped:'tld'},
    //NEXT LINE ADDED
    { unescaped:'%', escaped:'prc'},
  ],

And there you go! Now you can save / load 2D arrays of strings.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums