markdwags / Razor

Razor is a free tool designed to help with simple tasks while playing Ultima Online.
https://www.razorce.com
GNU General Public License v3.0
134 stars 90 forks source link

[BUG] Numbers stored incorrectly in Razor scripts #227

Closed cdsimmons closed 7 months ago

cdsimmons commented 7 months ago

Using ClassicUO, for Outlands.

See this script...

createlist TestList
clearlist TestList

@pushlist TestList 10
@pushlist TestList 80
@pushlist TestList 80000

overhead 'result...'
foreach val in TestList
    if val > 5000
        overhead 'y'
    else
        overhead 'n'
    endif
endfor

Expected output

result...
n
n
y

Actual output

result...
n
y
y

For some reason Razor scripts think that 80 is greater than 5000. It seems like numbers are stored as floats. This results in an incorrect clause like 8.0 > 5.0.

This issue seems to affect numbers saved in lists, and numbers saved as vars.

cdsimmons commented 7 months ago

I had a response on Jaseowns discord, these are workarounds...

createlist TestList
clearlist TestList

@pushlist TestList 10
@pushlist TestList 80
@pushlist TestList 80000

overhead 'result...'
foreach val in TestList
    if 5000 < val
        overhead 'y'
    else
        overhead 'n'
    endif
endfor

This passes the expected output.

If we want to compare var against var, we can do this...

@setvar! testVarSmall 80
@setvar! testVarLarge 400
createlist TestList
clearlist TestList
pushlist TestList testVarSmall
overhead 'result1...'
if poplist TestList front < testVarLarge
    overhead 'y'
else
    overhead 'n'
endif
markdwags commented 7 months ago

Razor compares two variables as strings, not as int values, so it's doing a string compare to to see if "2" > "16", not 2 > 16.

This isn't a bug, the Razor scripting engine wasn't just designed for that type of operation (though I can see how it could cause confusion).

I might explore a future update with this functionality.