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

  • 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:

    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.

  • You can make your own sign() function, that returns 1, -1, 0, base on the parameters sign.

  • I like to use this expression:

    (var>0)-(var<0)

  • MushroomKingdomr

    You were nearly there with what you had:

    (var/abs(var))

    gives you 1 for positive nos and -1 for negative

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The problem with division is it doesn't allow for var=0.

  • 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

  • blackhornet Very true! Oops! Pay me no attention, MushroomKingdomr, and go for R0J0hound 's super elegant solution.

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)