Jump to content

Warning witcher script moders: operator % (modulo) seems to be broken if >8bit number


Recommended Posts

I am just trying to adapt to my needs mod SortEverything by grouping similar items in Inventory. I encountered surprising difficulties when trying to calculate positions for Armor (Armors takes a significant amount of space in Inventory). After day of debugging, I found that operator % (modulo, reminder), that I use to calculate column, returns incorrect number if left hand operand is bigger than 255 (so no longer 8bit). In particular, following test code:

// ROTFL
eighteen = 18; // 18 is two rows and is needed to calculate placement for armors
if ((256 % eighteen != 256 - 256 / eighteen * eighteen)) {
    for(i = 253; i <= 258; i += 1){
        insanity += IntToString(i) + " % " + IntToString(eighteen) + " = " + IntToString(i % eighteen) + "; ";
    }
    semodLog("Operator %(modulo) is broken: " + insanity + "<ROTFL>");
}

Returns following log line:

[modSortEverything] Operator %(modulo) is broken: 253 % 18 = 1; 254 % 18 = 2; 255 % 18 = 3; 256 % 18 = 3; 257 % 18 = 3; 258 % 18 = 3; <ROTFL>

So if number bigger then 255 calculation is incorrect. There is obvious work-around, like:

    row = startingPosition / rowSize;
    col = startingPosition - row * rowSize;

This post is just FYI, that if something is not working for you, Scripting Engine itself can be bugged.
I am using 1.31 + unification patch.

PS. When looking for Quick Sort implementation I noticed that file "The Witcher 3 Wild Hunt\content\content0\scripts\core\array.ws" includes incorrect implementations of QSort. Its first recursive sub-range is hardcoded to start at 0, instead of parent start position. So be careful what you copy.

PS2. On official Witcher 3 mod forum there was an information, that use of operator ?: (ternary) may also leads to generation of incorrect code (thread https://forums.cdprojektred.com/index.php?threads/possible-script-compiler-bug.56159/). Be aware.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...