How can I make it so that while one variable increases another is lower?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RubRub

Ejemplo: quiero hacer algo como esto, Si la variable a aumenta, la variable b disminuya, It is that I will implement it in an attempt of a catapult system like plants vs zombies

:bust_in_silhouette: Reply From: Mitsos

Lets say you have a variable x and a variable z.
Lets also assume that in the staring state x=200 and z=300.
You take the sum of x and z which is 500 in this case.
If you increase variable x in your code you can make z decrease the same amount x increases by setting z = 500 - x. I don’t know which programming language you are using to help you futher but this should give a pretty good idea of what you should do.

:bust_in_silhouette: Reply From: ponponyaya

It’s seem like a math peoblem, try to use subtraction or division.

For example:

  1. Subtraction case:
    y = 1 - x, when x in [0,1]

  2. Division case:
    y = 1/x, when x > 0.

Then x increase implies y decrease.

:bust_in_silhouette: Reply From: Inces

I don’t understand spanish. but just from the title of this question : if You want to bind some behavior to change in one particular variable You can use setget keyword.

var _myvar : int setget onvarset
var anothervar : int

code below subtracts one from anothervariable whenever _myvar increases :

func onvarset(value) :
       if value > _myvar :
           anothervar -= 1
     _myvar = value
  

Setters require some setup to use properly, read about in documentation before You paste this code