Negative variables


#1

Hello everyone! I am making a game where you can have a number that is negative. I have treasury income, I want it to make it so that when I have <= then -100 it displays “We need to adjust the taxes! We are in debts!”
I made it like this :
*if income <= -100
“We need to adjust the taxes! We are in debts!”

But this only make out this error :
startup line 328: Invalid expression at char 14, expected no more tokens, found: OPERATOR [-]
I found this problem on forum but it haven´t been solved. I know that negative numbers don´t make much sense for PC but there should be some way around it. I tried using

*if income <= 100-200

But it ends up the same as previous. Read the wiki but there is nothing about negative numbers. Just the positive ones


#2

Try it with parentheses around the subtraction, like this: *if income <= (100 - 200)


#3

ChoiceScript has no way to tell if you’re trying to use a negative number, or subtract when you enter - 100. Generally it’s recommended that you avoid negative numbers.

There are two normal solutions to this problem, then a third non-recommended one:

Rezero

Use a higher number as you “zero” and calculate from there. For example, if you know income is never going below 1000, rather than start it as *create income 0, you use *create income 1000, and rather than *if income <= -100, you check *if income <= 900 (and your display of income just becomes ${(income-1000)}, if you need to).

Convert it

Go to a subroutine whenever you *set income (either adding or subtracting), you’ll also need to check (off the top of my head and untested):

*if debt > 0
  *set income - debt
  *set debt 0
*if income < 0
  *set debt (0 - income)
  *set income 0

Use work around (Not recommended—may not work in all cases)

To ‘use’ a negative number, use (0-foo) instead. So, for that *if, you’d need to use *if income <= (0-100). (Alternatively to (0-foo), you can also use "-foo" as pointed out by @Szaal below.)


#4

Actually, you can. Of course, I doubt that CScript register it as a numeric variable, but

*set money "-1"

works.
Applying normal mathematical operations to this variable will also work, although, again, I’m not sure whether CS register it as a numeric variable or a string variable.

*set money "-1"
*set money +100
${money}

This will make your [money] = 99


smol ignorable mundane edit:
Oh, you get a notification when someone edited their post to mention you.
Neat.


#5

Hmmm… It seems like Szaal “” tip worked but after the income is set to (for example) 1000 the massage stays there! It ownt get off. I am thinking about something like:
*if income >= “-99”

It will display nothing. But I don´t know if this is possible.
Also it wont show up on first encouter. You have -499 income first when you enter the menu. But it don´t shown your money problems. Only after changing the taxes to not sufficent amout of money it displays it


#6

I believe it should be <, not >.

As my teacher said, pac-man will always eat the biggest pellet.


#7

That still doesen´t solve the not showing at first sight problem :smiley: But thanks for the first one


#8

Hm. Can I see your code?
Maybe you write it in such a way that the first encounter will always be ignored/passed.


#9

*label overview
*page_break
*if income <= “-100”
“We need to adjust the taxes! We are in debts!”
*if income >= “-99”

*set permit 1
*line_break
Overview
Date : TBA

*choice
#What is today’s State of affairs?
*gosub random

#Advisor! There are things that have to be done!
*goto advisor_options


#10

I was just writing the code you sended so it is not tested yet

When I cahnged
*if income <= “-100”
to
*if income >= “-100”
It started to show the line on first sight but after the income is big again it stays there and it also don´t show the other massage


#11

Ah, I see it now.

Go this

*label overview
*page_break
*if income <= “-100”
   “We need to adjust the taxes! We are in debts!”
*elseif income >= “-99”
   *set permit 1
*line_break
Overview
Date : TBA

You used *if for both checks; this means that both of them will be tested.
If you want to go “if the thing goes this, only do this,” you have to use the trio *if/*else/*elseif.


#12

Oh sorry I forgot :smiley:
But will this make it to appear on first sight?

No, it just don´t work! It won´t show the text at all!


#13
  1. I doubt *if foo <= "-100" works correctly in all circumstances.
  2. Just use
*if` foo <= "-100"
  blah
*else
  blah2

#14

This symbol ` seems like it causes troubles… I delete it and then no foo variable I change it to income and like previously! It just shows that the treasury is ok instead of debts

Oh I am stupid guy… I think I found it. I changed the income only in Stats. I thought that would be enought for the game to change the whole game $!{income} but it proppably changed only for Stats screen and not for any other .txt file

OK sorry to everyone! I found out that the income doesen´t rewrite itself to all txt files and that I need to do it manually. Don´t know if there is some wayaround but after I rewrite it I will write the result. Thanks everyone!