choicescript icon indicating copy to clipboard operation
choicescript copied to clipboard

Temporary variables cannot be initialized with negative values

Open ChrisPC opened this issue 8 years ago • 2 comments

*create test -5 and *temp num -5 each result in errors.

*title test
*author me

*scene_list
    startup
    test

*create test -5

*finish

yields

F:\Docs\Choice of Games\test>java -jar js.jar -w -opt -1 -debug autotest.js mygame
startup.txt executing QUICKTEST FAILED

Error: startup line 8: Invalid create instruction, value must be a a number, true/false, or a quoted string: test -5

(and that error message duplicates the word "a"); also

*temp num -5

${num}

*finish

yields

F:\Docs\Choice of Games\test>java -jar js.jar -w -opt -1 -debug autotest.js mygame
startup.txt executing test.txt executing QUICKTEST FAILED

Error: test line 2: Invalid expression at char 2, expected NUMBER, STRING, VAR or PARENTHETICAL, was: OPERATOR [-]

ChrisPC avatar Mar 04 '16 01:03 ChrisPC

You can work around it like this:

*temp num 0-5

As for fixing it, this is a subtle issue. One thing that's clear is that users can't *set a variable directly to a negative value, because this line:

*set num -15

already means "subtract 15 from num", not "set num to negative 15."

We could put some special-case code in *temp to allow negative values, but I think it would be difficult to explain why *temp num -15 behaves so differently from *set num -15.

dfabulich avatar Apr 01 '16 16:04 dfabulich

Ah, that's a good point; I had forgotten how the overloaded *set would interact with the whitespace-lenient basic math operators like - in this case. Good thing I'd always initialized negative values at 0 first...

I'm surprised *temp num 0-1 works. I hadn't thought calculations could be done in any statement without parentheses. Regardless, this workaround isn't valid for *create statements.

Proposed syntax:

*create num (-1)

and

*temp num (-1)

Parentheses are currently illegal in *set and *create statements, so I don't believe this should break existing code.

(I'd like to see *set num (-1) allowed, as well. Parallelism, for one thing, but I also find this syntax is more clear and readable than (0-1).)

ChrisPC avatar Apr 01 '16 18:04 ChrisPC