I know that, on the whole, CS is not designed to play nicely with negative numbers. I’m not asking about assigning negative values to stats. That’s not my problem.
My problem is that I have a subroutine that receives numeric values, and fairmath adds them to different stats, depending on the values of certain game flags. So far I have only tested it with addition. For subtraction, I planned to use the same subroutine, but pass along negative integer values. Basically, %- 1 should be the same as %+ (-1). I mean, arithmetically they’re equivalent, right? The end values would still conform to the standard 0-100 for CS.
That bit of code gave me some trouble late last night, and it occurred to me this morning that CS does not play nice with negative values. So I thought the smartest thing would be to ask at the source. Can CS subroutines receive negative integers as arguments? Or do I need to duplicate my subroutine and create a decrementor?
ETA: This is the text of the error:
Error: 2heir line 1473: Invalid expression at char 3, expected NUMBER, STRING, VAR or PARENTHETICAL, was: OPERATOR [-]
ChoiceScript is built on JavaScript which is weakly typed and capable of implicit conversions between types. I think in this case I am mistaken though (I just tried it in a JS console), it seems to prefer a string in this scenario. @Carlos.R’s idea could very well work though, and if it doesn’t just do:
I can cheerfully report that both methods work as intended. At least with regular subtraction. I will have to run a more rigorous test on fairmath later on.
It seems the core of the problem is not to do with weak typing, but just that whatever parses CS code does not recognize the negative symbol as anything but an arithmetic operator. Judging from the text of the error message, that is.
facepalm never thought to cast negative numbers as a string before. Seems obvious now. I’ve been using the (0 - 1) trick myself. Its definitely the parser that causes it. Having programmed an expression parser in other languages before, I can attest to the fact that implementing the ability to use negative numbers isn’t a simple task. While it would be a nice addition to ChoiceScript, I can understand why the devs don’t want to go through the hassle of implementing it. It’s a lot of effort for a feature not a lot of people will use.