# How to form formulas

### » Mon Nov 22, 2010 12:22 am

[quote="irbis":1yfih4yu]i have problem with setting up a basic "ON/OFF" switch based on variables. of course - if you now a way to do this other then by variables feel free to enlighten me.

what i want/need:
When pressing "F1" i want something to move from one place to another, and when pressing F1 again to go back. imagine it like a UI moving from outside of the screen down vertical into the screen, and then going back. bear in mind its not about X/Y position, but actual sliding from point A to B and from B to A.

what i used:

ive set up variable "Switching"

[code:1yfih4yu]IF key F1 pressed=>
If variable =0 :
1. move Object verticaly 150
2. Set variable to 1

if variable =1:
1. move Object verticaly -150
2. Set variable to 0[/code:1yfih4yu]

and that should work... but it doesnt. most of the time it goes randmly up and down like it would ignore the chanching variable. is there a way to make so that F1 key will be disabled until the variables switches?[/quote:1yfih4yu]

Arima's solution is an elegant one for this case, but it may not apply well to all conditional branching situations that you may come across, so I'll point out another way: ELSE.

ELSE is a system condition that can be used to make sure that only one of the conditions is true in any tick. You had the logic very close to correct for that case:

[code:1yfih4yu]IF key F1 pressed=>
If variable =0 :
1. move Object verticaly 150
2. Set variable to 1

ELSE:
1. move Object verticaly -150
2. Set variable to 0[/code:1yfih4yu]

Also you could extend it to a 3-state or more check in similar fashion:

[code:1yfih4yu]IF key F1 pressed=>
If variable =0 :
1. move Object verticaly 150
2. Set variable to 1
ELSE
If variable =1 :
1. move Object verticaly -150
2. Set variable to 2
ELSE
If variable =2 :
1. spin Object 360 degrees
2. Set variable to 0[/code:1yfih4yu]
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

### » Mon Nov 22, 2010 3:07 am

[url:2b4efwhb]http://www.scirra.com/forum/viewtopic.php?f=8&t=6880&p=53876#p53876[/url:2b4efwhb]

B
24
S
8
G
10
Posts: 1,822
Reputation: 8,291

### » Mon Nov 22, 2010 9:54 am

i tried this

[code:45guzdhm]- if key F1 pressed, set variable to 1-variable (set the variable to 1-itself)
-- if variable = 0, set position to .x+150
-- if variable = 1, set position to .x-150[/code:45guzdhm]

and its not working. well, more propable i just implemented it wrong.
But i dont understand, so whenever i press F1 the variable will be changed to 1

so how the second sub-event gone trigger?
what have i done wrong?

EDIT oh sorry guys, i havent seen your posts before. lemme check your info and see if it works

thank you

EDIT2

okey i have tried this:
[quote:45guzdhm]
IF key F1 pressed=>
If variable =0 :
1. move Object verticaly 150
2. Set variable to 1

ELSE:
1. move Object verticaly -150
2. Set variable to 0[/quote:45guzdhm]

and it works almost right. the problems shows when i press the F1 rapidly. then it offten move twice down and one up or the opposite. ill try and add "every 500milisec" as condition to F1 key pressed and see how itll work.

EDIT3
nope. it fixed one problem adding another one. now - yes, the event cant be triggered twice or more in row with the same moving values but also, because of the times it now works just half of the time lol so every 1/4 F1 hit acutlay works, then again when i decrese the times to the point when it works 100% of the time, the event overlaping stricks back.

EDIT4
WOHOW! ive found an solution. its anything but elegant but it works!
heres the deal:

i needed to add timer and a new variable, and it didnt worked at first. but when i changed the timer for 1000milisecs now its all good.

thankc guys for support and stimulating my brain cells
B
29
S
8
G
7
Posts: 643
Reputation: 6,482

### » Mon Nov 22, 2010 2:42 pm

The first image is almost correct. The reason it didn't work is because of the action in the first event. You have it so the variable is set to 1. Instead, set it to:

Set global variable 'BattleEjectorsWeapons' to 1-global('BattleEjectorsWeapons')

Get the logic? Because the way you had it, the variable was always set to 1.
Moderator
B
96
S
34
G
33
Posts: 3,006
Reputation: 27,931

### » Mon Nov 22, 2010 3:21 pm

nah its working already i am too scared to mess with it to not break it lol

and NOW i finaly understood this "1-global('BattleEjectorsWeapons')"
didnt knew i can use such expression this will get very handy as my spaceships armor aside from having durability they also have % dmg ressistance. so thx to this formula ill be able to calculate final damage with "global('Damage') - global('DamageRessistance%') = global('DamageFinal')

so on example 100dmg - 90% = 90

em i correct? can i use such a formula?
B
29
S
8
G
7
Posts: 643
Reputation: 6,482

### » Mon Nov 22, 2010 4:00 pm

Yep. No need for fear, tho - copy and paste the code, toggle the copied event off (in the ribbon under events) and you can mess with the code without worrying about messing up what you had.
Moderator
B
96
S
34
G
33
Posts: 3,006
Reputation: 27,931

### » Mon Nov 22, 2010 9:15 pm

another problems... aparently i am a good friend of them.

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. now what did i screwed up this time?

EDIT:
and my second problem. i never been good in math but i have even more problem forming a proper expression chain.
basicly what i want:
i want "Something X" to happend when Value of var "EnergyCurrent" is at 50% of value of var "EnergyTotal".

how should i form it in conditions?

i know "IF variable "EnergyCurrent" is Equal or Lower... what now?
[code:x2xlir3c]50 % global('EnergyTotal')[/code:x2xlir3c]...? is that correct

EDIT2: adding to my last question what formula i should use when value X is LOWER then 90% but HIGHER then 50%?
B
29
S
8
G
7
Posts: 643
Reputation: 6,482

### » Tue Nov 23, 2010 2:17 pm

bumpy bumper bump.
B
29
S
8
G
7
Posts: 643
Reputation: 6,482

### » Tue Nov 23, 2010 10:56 pm

% does not give you percent, it gives you the remainder after division.
[url:2k66q63i]http://sourceforge.net/apps/mediawiki/construct/index.php?title=Expressions[/url:2k66q63i]

To get 50% of a value just multiply by 0.5.
B
101
S
39
G
134
Posts: 5,591
Reputation: 85,520

### » Wed Nov 24, 2010 6:25 am

[quote:1pwehmyb]To get 50% of a value just multiply by 0.5.[/quote:1pwehmyb]
now i feel like an retard lol. damn you elementary math!
Thank you R0J0hound!

so the condition would be IF global('ShieldsTotal') & = & (global('ShieldsTotal') & * & 0.5) ?

even so - i still dont know how to form a range of values (e.g all betwen 40 and 70)

and obviously i still havent figured out how to make a variable change its value to another variable value. if i use
IF variable X = or > then variable Y
then
set variable X equal variable Y

hell breaks loss and both variables becomes linked togather - so if i change any of them the second follows.
B
29
S
8
G
7
Posts: 643
Reputation: 6,482

PreviousNext