How do I use the mathematical function "sign"

Get help using Construct 2

» Wed Jun 18, 2014 2:24 am

Hi there,

anyone knowing if there is a possibility to use the "sign" function? It seems to be missing - is it missing or called differently?
If missing, is there any workaround?

( I mean this: http://en.wikipedia.org/wiki/Sign_%28mathematics%29)

It would be really, really useful

B
8
S
3
Posts: 197
Reputation: 1,207

» Wed Jun 18, 2014 3:13 am

A real number is said to be positive if it is greater than zero, and negative if it is less than zero.
B
71
S
22
G
288
Posts: 3,859
Reputation: 158,424

» Wed Jun 18, 2014 4:06 am

What do you need it for ?
Typically you would use System.Compare Variable with >0 or <0 instead.
B
12
S
3
G
3
Posts: 73
Reputation: 2,647

» Wed Jun 18, 2014 6:07 am

If you are trying to use it in an expression somewhere, the workaround I've been using is to just have a variable that is always either -1 or 1. It makes the >0 or <0 check right before the expression. Of course this has limited application and doesn't really substitute a proper sign function...
B
5
S
1
Posts: 10
Reputation: 395

» Wed Jun 18, 2014 6:11 am

define your own function which calculates the sign, you can call it in expressions with Function.Call("Sign", x)
B
6
S
2
Posts: 79
Reputation: 608

» Wed Jun 18, 2014 11:07 pm

The problem is, I need to be able to calculate like if sign(x) = sign(y) then A else B. And I like to be effektive, so trying to keep things short

After Posting, I exactly figured the workaround of Zhon, that works well of course, since I only need the function for just one value.

If I needed the function often in the script, I might use monitz87`s solution, though I am not sure how to do that in an effective way. I think that could become a bit complex, which makes it only worth to implement, if you need it all the time for a lot of different variables. Still a great solution, also I did not know at all, you could do that. So this Answer was very, very useful

Thanks for the quick and helpful replies!
B
8
S
3
Posts: 197
Reputation: 1,207

» Thu Jun 19, 2014 4:37 am

MultipleChoice wrote:The problem is, I need to be able to calculate like if sign(x) = sign(y) then A else B. And I like to be effektive, so trying to keep things short

After Posting, I exactly figured the workaround of Zhon, that works well of course, since I only need the function for just one value.

If I needed the function often in the script, I might use monitz87`s solution, though I am not sure how to do that in an effective way. I think that could become a bit complex, which makes it only worth to implement, if you need it all the time for a lot of different variables. Still a great solution, also I did not know at all, you could do that. So this Answer was very, very useful

Thanks for the quick and helpful replies!

The bottom line is, whenever you are 'coding' in construct, there will be some times when you will think to yourself "god, how I wish I could just use javascript/python/C#/whateverlanguage and just tell the engine exactly what I want to do". AFAIK, the engine has a workaround for most of these cases. Scirra is working towards a Framework that will allow developers to never hit that hurdle, but in the meantime, you can stick with what you've got.

As far as workarounds go, implementing your own sign expression as a Function is pretty straightforward, although I fully understand how just having sign(x) as a system expression would save you a lot of time
B
6
S
2
Posts: 79
Reputation: 608

» Thu Jun 19, 2014 8:24 pm

monitz87: I'm happy with Construct 2 completely, getting along awesome, though i NEVER wrote any code before... Also I am faster than most of my programming friends, so construct already saves you massive time, I get that.

So no complains at all, at the end of the day, I just thought, let's ask people with more experience. And it was worth it, since I learned something new

Thanks again!
B
8
S
3
Posts: 197
Reputation: 1,207