# Expression Need to help with one calculation

Get help using Construct 2

### » Sat Apr 08, 2017 7:10 am

I have 3 global variables:
Acurrent
Bincome
Cmax

Global variable "Acurrent" have 300
Global variable "Cmax" have 500
Global variable "Bincome" will be have random number lets for example set to 400

now when i click button

system should fill "Cmax" to his maximum variable (500)

BUT when "Acurrent" is 300 we can add only 200 from "Bincome"

so finally after clicking button
we should have calculation
A-500
C-500
B-200

SECOND EXAMPLE
when Cmax have only 50
and Acurrent have 0

We take from Bincome only 50
B
26
S
8
Posts: 229
Reputation: 2,228

### » Sat Apr 08, 2017 11:21 am

@IGDev

Add another variable to as a placeholder for the income to add, then do:

Set tempToAdd to Acurrent + Bincome > Cmax ? Cmax - Acurrent : Bincome

Set Acurrent to Acurrent + tempToAdd

Set Bincome to Bincome - tempToAdd
Find all the Construct 2 games that were on Steam Greenlight here
B
41
S
11
G
7
Posts: 513
Reputation: 7,330

### » Sat Apr 08, 2017 11:30 am

(Cmax - Acurrent) = MaxFill

If Bincome > MaxFill
. .... then NewAcurrent = Acurrent - MaxFill
& NewBincome = Bincome - MaxFill

If Bincome <= MaxFill
. ... then NewAcurrent = Acurrent - Bincome & NewBincome = zero

Or getting MaxFill out there again (it explained well with)

If Bincome > (Cmax - Acurrent)
. .... then NewAcurrent = Acurrent - (Cmax - Acurrent)
& NewBincome = Bincome - (Cmax - Acurrent)

If Bincome <= (Cmax - Acurrent)
. ... then NewAcurrent = Acurrent - Bincome
& NewBincome = zero

Under the assumption that Bincome cant be negative.
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Sat Apr 08, 2017 11:42 am

No, that is wrong, i suppose Acurrent cant be less than zero too.
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Sat Apr 08, 2017 12:07 pm

99Instances2Go wrote:No, that is wrong, i suppose Acurrent cant be less than zero too.

can you show this in capx?
B
26
S
8
Posts: 229
Reputation: 2,228

### » Sat Apr 08, 2017 12:13 pm

If all you care for is capping your Acurrent variable just go:

Set Acurrent to
Bincome+Acurrent>Cmax?Cmax:Bincome+Acurrent

If you really need Bincome changed upen exceeding that threshold

set Bincome to
Bincome+Acurrent>Cmax?Cmax-Acurrent:Bincome+Acurrent

Then you can add Acurrent and Bincome
B
18
S
9
Posts: 272
Reputation: 2,363

### » Sat Apr 08, 2017 12:24 pm

Haha, you quoted me, now i cant erase my stupid comments. Here goes :

Legal maximum for Acurrent = Cmax

Possible illegal maximum >> Bincome + Acurrent = bigger then Cmax
Everything bigger then Cmax goes back to Bincome

So .....

Local variable temp

temp = min(Bincome + Acurrent,Cmax)
Bincome = (Bincome + Acurrent) - temp
Acurrent = temp

https://www.dropbox.com/s/yajxehyx8sxre ... .capx?dl=0
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Sat Apr 08, 2017 1:15 pm

99Instances2Go wrote:Haha, you quoted me, now i cant erase my stupid comments. Here goes :

Legal maximum for Acurrent = Cmax

Possible illegal maximum >> Bincome + Acurrent = bigger then Cmax
Everything bigger then Cmax goes back to Bincome

So .....

Local variable temp

temp = min(Bincome + Acurrent,Cmax)
Bincome = (Bincome + Acurrent) - temp
Acurrent = temp

https://www.dropbox.com/s/yajxehyx8sxre ... .capx?dl=0

i see now what is missing

when i calculate first time
From B 200 goes to A abd 200 stay

its possible to make this without Textboxes?

For example when my A is equal to C
A should be changed to 0

and i we should have posibility to click again and add rest from B to A variable?
B
26
S
8
Posts: 229
Reputation: 2,228