Help with Variable system

Hello, long time no see guys! how things are going?

So i have this issue with variables,
So let's say i have 1 variable signed as Apple and 1 variable signed as Pear.
they are both Fruits..
and when i add them in my system their variable count gain 1 point for each.
lets say i have both Apple and Pear (i have 2 points in my Fruits Variable....)
When i remove an Apple ( i am making the Variable -1) when i ask how many fruits i have it still says 2....
Whats the right format to make this work?
variables in the editor are not referenced, IIRC.
you have to perform the operation again to get an updated result.
variables in the code are referenced, tho.... so F = A + P actually reads as "F equals the content of object A plus the content of object P".
each object is then read itself, compared, and sent to the proper function that return the object mathematical sum of both.
the fact that both objects are addable integers is just a happy coincidence, otherwise it'd return a mismatch.
and if you're not a programmer, an easier way to understand is this:

control variable is the command that sets the number into a variable, and after that the variable keeps that number until the command is used again.
There is no automati8c update at all, only manual updates.

so basically after removing the apple you need to recalculate fruit to show the new value, othersie it keeps returning the value it got the last time fruit was used/updated.
after removing the apple you need to recalculate fruit to show the new value
which is what you'd do if you had the physical thing, only you do it subconsciously in a split second, but you still do it before walking away with the updated result.
I understand variables as a "Cartesian plane".
But to explain the concept of variables, there are negative numbers and positive numbers after zero.
If you have 0 apples and you reduce one, you will end up with -1.
@Eternidade_1 I don't think the basic math and existence of negative numbers was a part of the question (which, admittedly, is a bit hard to understand).

The OP was changing one variable and expecting another variable to change, which will never happen.

Variables only do exactly what you tell them to, they never automatically update.
i am so confused right now..
to be more spesific i have 3 variables, ok?
1 For Fruits
1 For Apples
1 for Pears
I am starting all of them with 0 right?
When i add an apple (Control variables: [002: Apples] + = 1)
the fruits variable automaticly goes to 1 value too, right?
Then i add (Control variables: [003: Pears] + = 1)
Fruits goes = 2
But when i remove an apple (Control variables: [002: Apple] - = 1)
The fruits are still 2 for some reason...
EVEN when i inform the fruit variable that i removed one fruit/point...
How did you inform them? The only way to update fruits is another control variable -= 1

Variables do not automatically update, ever. You always have to call them to change then. For *any* change.
i am so confused right now..
to be more spesific i have 3 variables, ok?
1 For Fruits
1 For Apples
1 for Pears
I am starting all of them with 0 right?
When i add an apple (Control variables: [002: Apples] + = 1)
the fruits variable automaticly goes to 1 value too, right?
Then i add (Control variables: [003: Pears] + = 1)
Fruits goes = 2
But when i remove an apple (Control variables: [002: Apple] - = 1)
The fruits are still 2 for some reason...
EVEN when i inform the fruit variable that i removed one fruit/point...
As ATT_Turan was trying to explain, nothing is automatic unless you have something in place that is monitoring and updating information.

When i add an apple (Control variables: [002: Apples] + = 1)
the fruits variable automaticly goes to 1 value too, right?

WRONG.

As I just said unless you have something that is automatically controlling this, by code or parallel event or something, fruit variable will never increase.

You would have to do something like
(Control variables: [002: Apples] + = 1)
(Control variables: [001: Fruits] + = 1)

But ideally you should be updating fruits variable with
(Control variables: [001: fruits] = (Control variables: [002: Apples])
(Control variables: [001: fruits] += (Control variables: [003: pears])

Does that make sense now?
When i add an apple (Control variables: [002: Apples] + = 1)
the fruits variable automaticly goes to 1 value too, right?
NO.
Variables only do exactly what you tell them to, they never automatically update.
the fruits variable automaticly goes to 1 value too, right?
no.
fruits is still 0, because it's still the result value of the sum of the contents of the variables apples and pears, before you added 1 to one of them.
you don't *condition the variable to be the operation*... you *execute the operation* and
*store the result*
the moment you *recall the result*, it'll be the result, not the operation.

you only *condition the variable to be the operation* in the code, not the editor.
when you call the variable in the code, you're actually calling the expression it stands for.
that's what I mean with referencing.
You put a chair in your room.

Then you put a sofa in your room, and line it up with the chair.

You go and push the chair back a few feet; do you expect the sofa will have also moved?

No, they're two separate objects - it doesn't matter that you used the chair for a point of reference when you put the sofa down.
But ideally you should be updating fruits variable with
(Control variables: [001: fruits] = (Control variables: [002: Apples])
(Control variables: [001: fruits] += (Control variables: [003: pears])
How do you always fix my problems like magic.. ?
this was exactly what i needed, the right format to connect those 3 together.
Thank you very much!
if you want fruits to update automatically when pears or apples change- you're gonna have to do more, ill write a simple algorithm for you!! ^^

first make 2 variables: Old_Apples and Old_Pears, them and Apples and Pears will all equal 0 to start.

then run 2 seperate if statements with the condition 'Apples != Old_Apples' and 'Pears != Old_Pears' respectively.

inside the apple if statement: have the contents be 'Fruits += Apples - Old_Apples' then 'Old_Apples = Apples' so it doesnt keep running the if statement.

and inside the pear if statement: 'Fruits += Pears - Old_Pears' then 'Old_Pears = Pears' for the same reason as the apple if statement.

that way it will account for both increases and decreases!
for example: if we added 2 apples, the statement 'Apples != Old_Apples' will be true cuz 2 does not equal 0, so we go into the contents, first comes Fruits += Apples - Old_Apples which translates to '0 += 2 - 0' adding the difference between the old apples and apples (current amount). Then comes 'Old_Apples = Apples' where we update Old_Apples with the value of the current apples which is 2, rendering the if statement false and thus wont run again till Apples has changed, same exact procedure goes for the pear if statement too! have them both on a loop and in a parallel event and you are good to go! this is just off the top of my head so im sure there are easier and clearer ways to do this but i hope i made it clear enough for you to understand! ^^ feel free to ask if you have any questions!
Perhaps this scriptlet of mine can help you a bit with your issue, but I gotta say that people don't really need solutions like this one very often.

The scriptlet needs you to define the game variable IDS each type of fruit will need to store its current value.

Ruby:
# * Simple Fruit Kounter * #
#    Scripter : Kyonides Arkanthes
#    2024-08-23

# - Script Calls - #
# kind stands for either :apple or :pear or any other fruit you can come up with.
#  add_fruit(kind, number)
#  eat_fruit(kind, number)

class Game_Interpreter
  FRUIT_VAR_IDS = { fruits: 1, apple: 2, pear: 3 }
  def add_fruit(kind, number)
    var_ids = FRUIT_VAR_IDS
    $game_variables[var_ids[kind]] += number
    $game_variables[var_ids[:fruits]] = $game_variables[var_ids[:apple]]
    $game_variables[var_ids[:fruits]] += $game_variables[var_ids[:pear]]
  end

  def eat_fruit(kind, number)
    var_ids = FRUIT_VAR_IDS
    $game_variables[var_ids[kind]] -= number
    $game_variables[var_ids[:fruits]] = $game_variables[var_ids[:apple]]
    $game_variables[var_ids[:fruits]] += $game_variables[var_ids[:pear]]
  end
end

Once you copied and pasted it in a new script section, save your project and use any of those script calls whenever you need to add or subtract fruits.

The checks on how many fruits you've got at a specific point can be done with your typical game variable event commands.
Thank you everyone! 🙂

Original thread: https://forums.rpgmakerweb.com/threads/help-with-variable-system.171347/