[Solved] How do I get the sign of a variable?

Just started using Construct 2? Post your questions here

Post » Sun May 07, 2017 4:57 pm

What I mean by that is, for example: suppose there's a variable called vsp; if I wanted to get the sign of that variable, if it were positive, maybe at 121, it would be 1, but if it were negative, -23, it would be -1.

The function: sign, actually exists in GameMaker, so it would have been easy for me, but I couldn't find a similar option in C2, and this event:
Image

Will make the result negative, no matter if vsp was positive or negative to begin with, rendering it useless in determining the variable's sign.

I've tried other options similar to this one, but I've basically run out of ideas, and again, I don't want to go through any more trial and error to figure this out. Plus, again, searching for me is unreliable; when I looked it up, I couldn't get any results. Thank you for your support in advance.
Last edited by MushroomKingdomr on Wed May 10, 2017 2:07 pm, edited 1 time in total.
B
14
S
5
G
2
Posts: 21
Reputation: 1,996

Post » Sun May 07, 2017 6:03 pm

You can make your own sign() function, that returns 1, -1, 0, base on the parameters sign.
ImageImageImage
B
71
S
21
G
232
Posts: 3,696
Reputation: 129,868

Post » Sun May 07, 2017 6:08 pm

I like to use this expression:
(var>0)-(var<0)
B
92
S
32
G
107
Posts: 5,279
Reputation: 69,969

Post » Sun May 07, 2017 6:42 pm

@MushroomKingdomr

You were nearly there with what you had:

(var/abs(var))

gives you 1 for positive nos and -1 for negative
B
19
S
8
G
3
Posts: 432
Reputation: 3,978

Post » Sun May 07, 2017 10:19 pm

The problem with division is it doesn't allow for var=0.
ImageImageImage
B
71
S
21
G
232
Posts: 3,696
Reputation: 129,868

Post » Mon May 08, 2017 9:53 am

If you are not interested in the variable being possible zero ...

System > condition > Is number NAN > ..... sqrt(variable)
________________action ... here the action that goes with a negative var
Else
________________action ... here the action that goes with a pos var

Or ...

System > condition > Is number NAN > ..... sqrt(variable)
________________action(s) ... here the action that goes with a negative var
Else
System > compare 2 values > var = 0 ?
________________action(s) ... here the action that goes with a var = 0
Else
________________action(s) ... here the action that goes with a pos var
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon May 08, 2017 9:59 am

@blackhornet Very true! Oops! Pay me no attention, @MushroomKingdomr, and go for @R0J0hound 's super elegant solution.
B
19
S
8
G
3
Posts: 432
Reputation: 3,978

Post » Wed May 10, 2017 2:07 pm

I've just looked over both valid solutions; @R0J0hound 's and @99Instances2Go 's, and I consider the former to be the best one, especially for my skill level, since it's simpler. This means this problem is now solved; I don't even have to test out R0J0's expression; I already know how it works. Thank you all for the help.

By the way, @mekonbekon , had I figured out how your 'solution' worked, and tried it out for myself, I might would have gotten a nasty surprise. Thank you, @blackhornet , for catching that.
B
14
S
5
G
2
Posts: 21
Reputation: 1,996


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 4 guests