How to form formulas

For questions about using Classic.

Post » Wed Nov 24, 2010 6:46 am

Take it easy. It'll come
Ampersand (&) is for strings(text), so that's really not where you want to go here.

There's another page you might want to take a look at: system expressions.
[url:1xcn8spr]http://sourceforge.net/apps/mediawiki/construct/index.php?title=System_Expressions[/url:1xcn8spr]

If you look there you'll find clamp:
[quote:1xcn8spr]Clamp(Value, Lower bound, Upper bound)
Returns Value if it is between Lower bound and Upper bound. Returns Lower bound if Value is less than Lower bound. Returns Upper bound if Value is greater than Upper bound [/quote:1xcn8spr]

So...
clamp(variable,40,70)
Image Image
B
161
S
48
G
90
Posts: 7,347
Reputation: 66,749

Post » Wed Nov 24, 2010 7:04 am

[quote:37w7yvur]i want var ShieldsCurrent value to be downgraded to ShieldsTotal value. so you know, whenever it goes over ShieldsTotal var value it is fixed down.[/quote:37w7yvur]

In that case, it's always better to simply not allow that to happen. You can either test it first, then only set it if it will remain valid, or clamp it's value while setting it.

I made a simple example .cap (with Construct v0.99.96) that shows a bit of dealing with percentages, clamping values, and testing for ranges.

http://dl.dropbox.com/u/5868916/irbis1.cap

Good luck. :)
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Wed Nov 24, 2010 7:48 am

[quote="newt":3ffo3wza]Take it easy. It'll come
Ampersand (&) is for strings(text), so that's really not where you want to go here.

There's another page you might want to take a look at: system expressions.
[url:3ffo3wza]http://sourceforge.net/apps/mediawiki/construct/index.php?title=System_Expressions[/url:3ffo3wza]

If you look there you'll find clamp:
[quote:3ffo3wza]Clamp(Value, Lower bound, Upper bound)
Returns Value if it is between Lower bound and Upper bound. Returns Lower bound if Value is less than Lower bound. Returns Upper bound if Value is greater than Upper bound [/quote:3ffo3wza]

So...
clamp(variable,40,70)[/quote:3ffo3wza]
ih man i knew this most be something obvious right in front of my nose!
Thank yiu for that link Newt. i did check the "espressions" page, didnt figure out theres another diffrent one hah
now - many things became so simple :)


[quote:3ffo3wza]In that case, it's always better to simply not allow that to happen.[/quote:3ffo3wza]

thats the problem. if the would be just couple of conditions that changes this var i would just turn them off by auto with conditions, but the problem is - theres lots of conndtidions that changes this one and its an ant-job to add off switch for all of them or even keep track of them.
thank you very much for the cap mate :)

EDIT:
okey guys it seems i screwed up once again

whats wrong with this?
[code:3ffo3wza]clamp(global('Spaceship - EnergyTotal'),global('Spaceship - EnergyTotal')*0.6,global('Spaceship - EnergyTotal')*0.69)[/code:3ffo3wza]

ImageImageImageImage
B
26
S
7
G
7
Posts: 572
Reputation: 6,097

Post » Wed Nov 24, 2010 8:25 am

I think your over complicating things. Try getting rid of the Else's and put every condition as a separate condition.
If that works, then you can go back and try to simplify it more.
Image Image
B
161
S
48
G
90
Posts: 7,347
Reputation: 66,749

Post » Wed Nov 24, 2010 8:38 am

[quote="newt":1x7rh1xs]I think your over complicating things. Try getting rid of the Else's and put every condition as a separate condition.
If that works, then you can go back and try to simplify it more.[/quote:1x7rh1xs]

done and ready. still not working.

New issue #1:
another weird thing that i have just discovered is weird effect of "every X milisec's" contidion.

for value regeneration i use plain "Each X miliseconds" conditions and offten my values are like 1.20 0.80 3.33 ect. my problem is on example of Shields Regeneration. it starts with
0 value with 1.20 regen rate and goes all the way to 38 and then BLAM 40.7999999999999, and becomes even more and more messed up with each second. now how 1.2 value multiplyd by any round number can become 40.7999999999999??? O.o
i know that i can fix at last the shown value by adding round() around the var but its just like pushing trash under the carpet instead of cleaning it.

new Issue #2:
Bonuses/buffs. in many games you have those. like a ring of power +9 to str, magic shield +10 to AC, skill that grants you a passive +10% to all dmg.
now how can i do this in Construct? what sort of conditions i should use? the only i can think of is hell of a work to make the Total value be (var value + value bonuses) and opening another var names bonuses and then enlisting ALL kinds of bonuses i can have.

on example of my game. i have variable EnergyTotal that is being set by the Reactor Function. but would like to also have like batteries, special perks, rare awards that add passive values to var EnergyTotal. and there would be no problem IF i would have just one type of reactor. i would just use var add value. but i have many types of reactors and i want the EnergyTotal value be always updated with the bonuses. like for example:
Reactor 1 with TotalValue 100 and a bonus +15 to TotalValue. so the total value is 115.
now i change Reactor 1 for a better one Reactor 2 which has value 200. so i want that bonus to be added as the last and combine with other bonuses. otherwise the previous TotalValue of ReactorValue + Bonus 1 will be overwrite with just the total value set by new Reactor 2, so instead of 215 ill get 200.
any ideas?
ImageImageImageImage
B
26
S
7
G
7
Posts: 572
Reputation: 6,097

Post » Wed Nov 24, 2010 10:20 am

[quote="irbis":1v2oqfvx]
whats wrong with this?
[code:1v2oqfvx]clamp(global('Spaceship - EnergyTotal'),global('Spaceship - EnergyTotal')*0.6,global('Spaceship - EnergyTotal')*0.69)[/code:1v2oqfvx][/quote:1v2oqfvx]
There's nothing wrong with it. It just doesn't make sense. The returned value of this expression will always conform to global('Spaceship - EnergyTotal') * 0.69.

clamp returns a bounded value. But, if you make the bounds relative to the value, it will always return the highest or lowest value, depending on the relation. Just replace global('Spaceship - EnergyTotal') with a value to see the problem. Let's say, global('Spaceship - EnergyTotal') equals 100:

clamp(100,100*0.6,100*0.69)

You're now ordering Construct to return 60, if the original value is lower than 60, to return the original number, if it is between 60 and 69, and to return 69, if the original value is greater than 69. But the original value always is 100, and so you are always getting 69.

And now think of all the different values, global('Spaceship - EnergyTotal') may have. They all will be greater than global('Spaceship - EnergyTotal') * 0.69

clamp(50,50*0.6,50*0.69) will return 34.5 (always and only)
clamp(260,260*0.6,260*0.69) will return 179.4 (always and only)

So, if you are comparing global('Spaceship - EnergyCurrent') with clamp(global('Spaceship - EnergyTotal'),global('Spaceship - EnergyTotal')*0.6,global('Spaceship - EnergyTotal')*0.69), you are comparing global('Spaceship - EnergyCurrent') with global('Spaceship - EnergyTotal')*0.69

This comparison will only be successful, if global('Spaceship - EnergyCurrent') conforms exactly to global('Spaceship - EnergyTotal')*0.69, and not to a range of values.

Don't make the bounds of clamp relative to its value. And, if you want global('Spaceship - EnergyCurrent') to be in a certain range for the condition to become true, do this:

[code:1v2oqfvx]+ Is global variable 'Spaceship - EnergyCurrent' greater or equal global('Spaceship - EnergyTotal') * 0.6
Is global variable 'Spaceship - EnergyCurrent' less or equal global('Spaceship - EnergyTotal') * 0.69
[/code:1v2oqfvx]
That's two conditions in one event, meaning they both need to be true for the action to be performed. And they are both true for every value between global('Spaceship - EnergyTotal') * 0.6 and global('Spaceship - EnergyTotal') * 0.69
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Wed Nov 24, 2010 11:50 am

hmmm. somethings wrong but i dont know what. now it works partialy. so when Current = Total it displays the 100% value but stays at it and doesnt change.

i triple checked the animation, its setting, constrollers, variables and all is okey.

from what i have traced - for some reason ONLY the first condition
[code:1cq7u4wl]IF global('Spaceship - EnergyTotal') * 1[/code:1cq7u4wl]
is being exectuded. the rest is just ignored.

sample of the rest:


so i presume they are to blame
ImageImageImageImage
B
26
S
7
G
7
Posts: 572
Reputation: 6,097

Post » Wed Nov 24, 2010 2:27 pm

It seems, you want to present some kind of a shield status display. This display has 11 frames, representing 11 states of the shield status. Something like this:

xxxxxxxxxx -> shield 100%
xxxxxooooo -> shield 50%
oooooooooo -> shield 0%

Am I right? If so, there is a much simpler way achieving this. You have two values, EnergyTotal and EnergyCurrent. The first one is a value depending on the ship and its equipment. It may be 300 for one ship and 500 for another. If it was 300, then EnergyCurrent would have a start value of 300 and then lower whenever energy is used. And if EnergyCurrent is 150 it is 50% of EnergyTotal. But if the player now enhances the ship, the EnergyTotal may raise to 500. In result the 150 of EnergyCurrent represent no longer 50%, but 30%.

If all of these assumptions are right, then you only need to do the following:

[code:1fpmigt2]+ Always
-> EjectorShieldBar: Set animation frame to int((EjectorShieldBar.GetAnimationFrameCount - 1) / global('Spaceship - EnergyTotal') * global('Spaceship - EnergyCurrent')) + 1[/code:1fpmigt2]

Yes, you only need one event for all of your animation frames. The expression

int((EjectorShieldBar.GetAnimationFrameCount - 1) / global('Spaceship - EnergyTotal') * global('Spaceship - EnergyCurrent')) + 1

automatically sets the right frame in relation to the percentage from EnergyCurrent to EnergyTotal.

[size=85:1fpmigt2]EnergyTotal = 300
EnergyCurrent = 300
EjectorShieldBar.GetAnimationFrameCount = 11
--> frame = int((11 - 1) / 300 * 300) + 1 = int(10 / 300 * 300) + 1 = 10 + 1 = 11

EnergyTotal = 300
EnergyCurrent = 0
EjectorShieldBar.GetAnimationFrameCount = 11
--> frame = int((11 - 1) / 300 * 0) + 1 = int(10 / 300 * 0) + 1 = 0 + 1 = 1

EnergyTotal = 300
EnergyCurrent = 150
EjectorShieldBar.GetAnimationFrameCount = 11
--> frame = int((11 - 1) / 300 * 150) + 1 = int(10 / 300 * 150) + 1 = 5 + 1 = 6

EnergyTotal = 500
EnergyCurrent = 150
EjectorShieldBar.GetAnimationFrameCount = 11
--> frame = int((11 - 1) / 500 * 150) + 1 = int(10 / 500 * 150) + 1 = 3 + 1 = 4

EnergyTotal = 500
EnergyCurrent = 150
EjectorShieldBar.GetAnimationFrameCount = 21
--> frame = int((21 - 1) / 500 * 150) + 1 = int(20 / 500 * 150) + 1 = 6 + 1 = 7[/size:1fpmigt2]

You may change every value (including the number of frames you use in your animation to represent the status), it will always show the correct status of the ships shield.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Wed Nov 24, 2010 3:08 pm

[quote="irbis":39awx03g]
another weird thing that i have just discovered is weird effect of "every X milisec's" contidion.

for value regeneration i use plain "Each X miliseconds" conditions and offten my values are like 1.20 0.80 3.33 ect. my problem is on example of Shields Regeneration. it starts with
0 value with 1.20 regen rate and goes all the way to 38 and then BLAM 40.7999999999999, and becomes even more and more messed up with each second. now how 1.2 value multiplyd by any round number can become 40.7999999999999??? O.o[/quote:39awx03g]

That's because of rounding errors - floating point numbers like 1.2 can't be counted on to be calculated exactly. It's a limitation of processors. If you want exact numbers, stick to integers (whole numbers without decimals like 1).
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Wed Nov 24, 2010 3:18 pm

thats wicked.
and it almost works. and i dont know why.

the problem is, last frame suppose to be 100% ShieldEnergy only, just the first frame 0% should be displayed only when ShieldsCurrent hits 0%. with this the last frame 100% is for some reason never played, or should i say - it is but if ShieldCurrent is about 100% (100,000001% for example).

so anyway, two questions:
why my method doesnt work and why your does.
also - what the heck is int expression??? i never seen that before on both expresions and system expresions pages on wiki.

and well... many my previous issues are left unasweared :(

[quote:3th6hoph]That's because of rounding errors - floating point numbers like 1.2 can't be counted on to be calculated exactly. It's a limitation of processors. If you want exact numbers, stick to integers (whole numbers without decimals like 1).[/quote:3th6hoph]

wow. everyday i learn something new. never knew about such thing. how weird is that O.o like - it can easly count 2 + 2 but have troubles with 0,2 + 0,2?
ImageImageImageImage
B
26
S
7
G
7
Posts: 572
Reputation: 6,097

PreviousNext

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests