# How do I execute variable calculations

Get help using Construct 2

### » Fri Feb 13, 2015 10:03 am

Hi, Im working on a small economy-based project and feeling kinda lost.

So the general idea is basically Buying and Selling things between players, and there is an algorithm in place for determining the price for said item based on the amount of buyers and sellers.

For instance, player1 wants to buy 6 units of ItemA, player2 happens to sell 10 units of ItemA.

Since there is an excess amount of ItemA units, the value of ItemA should decrease by a certain percentage.

So far i've managed to give each Item a Global Variable, a value. But im stuck on executing the calculations correctly when all the players have input their Buy/Sell values.

Is there an efficent way of doing this that I'm missing?

Ill try to elaborate a little more:

ItemA= initial value 3, per unit

Player 1: Wants to buy 6 units of ItemA
Player 2: Sells 10 units of ItemA

4 units of ItemA excess, value of ItemA should decrease by 20%. So I need to subtract 20% from the ItemA value, which is 3.

Then I need the algorithm to work proactively, if the excess of ItemA exceeds a certain amount of units, the value decrease percentage should increase.

Any kind of help would be appreciated!

Thanks
B
2
Posts: 2
Reputation: 154

### » Fri Feb 13, 2015 10:20 am

It seems like you are using instance variables. I would say in this case use global variables instead, since their values can be compared and edited in the event sheet. Thats how I make my playerHealth, Ammo, Currency, Etc. Work.
B
3
Posts: 6
Reputation: 187

### » Fri Feb 13, 2015 10:51 am

I am using Global variables, im just having trouble making the calculations work in the event page, simple if/else statements was my first thought.
B
2
Posts: 2
Reputation: 154

### » Fri Feb 13, 2015 10:24 pm

Do you know what calculations you want to make, you're just having trouble implementing them? Or do you not actually know what calculations to make (in which case I don't think anyone will be much help, since it's your game )
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

### » Fri Feb 13, 2015 10:56 pm

It's not you, it really is tricky doing simple things like this in C2 given the nature of event sheets, which are constantly running.

If you have an 'if variable1 equals 4 then add 1 to variable 2' (or whatever) then when variable1 hits the value of 4, that expression will then be run 60 times a second and variable2 will increase by a lot more than 1. So then you add a 'System | Once while true' which is fine, but that has its gotchas as well. I tend to have an 'UpdateValues' function(s), which is what it sounds like, but this could also be called many times inadvertently as above. If you have a turn based game (sounds like you might) then just have such a functіon and call it at the end of each turn. Otherwise, If a 'system | once' doesn't do it for you, you can track the 'previous' value of a variable and compare if 'previous does not equal current and current equals 4' and trigger on that.

The above assumes the triggering of when to do the calcs is your issue. If it's just the actual expressions, then you just do something like

system | set variable1 to variable2 + variable3.instanceVar - sin(23)

or whatever your expressіon might be. As sqiddster says though, if it's the algorithm itself, then you need to give more info (and I've wasted my time )

Or is it the if/else thing you're not sure of?
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596