Right. While we're speaking of the horrors of zscript, I realized I never mentioned the latest bit I cooked up:
It works wonderfully. The only problem is it's a tad bit slow.Code:////////////////////////////////////////////////////////////////// // Pointers are numbers holding a reference to a specific index of a specific // array. This library deals with the creation and manipulation of pointers. // // Pointers are numbers that are split into 2 parts. The integer portion of // the number (XXX.0000) is the pointer's index into the zscript array. The // decimal portion of the number (000.XXXX) is the number of the zscript array // which the pointer is pointing into. // // All functions, if the array number is 0, will instead treat the integer // portion as the array number, and assume an index of 0. This lets you treat // arrays themselves as pointers to their 0 index. //////////////////////////////////////////////////////////////// // Creation // This creates a pointer to a given array. If given a pointer itself, it will // do nothing and return it instead. int GP_Pointer(int arg) { // If arg is a pointer, just return it. if (10000 * (arg % 1)) {return arg;} // Otherwise, create a new pointer to the first spot in the array. return 0.0001 * arg;} // This takes an array or a pointer, and returns a new pointer offset from the // first by the given amount. int GP_Pointer(int pointer, int offset) { // If arg is a pointer, just add the offset to it. if (10000 * (pointer % 1)) {return pointer + offset;} // Otherwise, create a new pointer from the array. return 0.0001 * pointer + offset;} //////////////////////////////////////////////////////////////// // Manipulation // Return the value that the pointer is pointing at, with the index offset by // the given number. int GP_Pointer_Get(int pointer, int offset) { // If pointer is actually an array, just return the 0 position of that // array. int array = 10000 * (pointer % 1); if (!array) {return pointer[offset];} // Otherwise return the pointer's value. (Array lookup truncates, so we // don't need to.) return array[pointer + offset];} // Set the value of a pointer, with the index offset by the given amount. void GP_Pointer_Set(int pointer, int offset, int value) { // If pointer is actually an array, just set the offset position of that array. int array = 10000 * (pointer % 1); if (!array) {pointer[offset] = value; return;} // Otherwise, set the pointer normally. (Array lookup truncates, so we don't // need to.) array[pointer + offset] = value;}
Readability mostly. I find it easier to read when there's multiple parallel arrays than when there's one with a bunch of multiplication or an incomprehensible mess of nested setter and getter functions. I use the one big array method if I'm working with global arrays, but with FFC scripts I haven't been hurt yet by wasting a few variables.
There are currently 2 users browsing this thread. (0 members and 2 guests)