Quick Links: Gideros Home | Download Gideros | Developer Guide
Using the < > and > < custom Gideros operators.
  • The Gideros version of Lua has some extra operators not present in regular Lua.

    Two of them are <> and >< (remember these are custom to Gideros Lua)

    <> is used to compare two numbers and return the greater of them
    >< is used to compare two numbers and return the lesser of them

    You may not think so initially, but these are two really handy operators and can save your game both a lot of code and cpu time.

    eg:

    Instead of this:
    pos=10
    function gameLoop(e)
    x=x-1
    if x<5 then x=5 end
    print(x)
    .
    .


    You can put this:
    pos=10
    function gameLoop(e)
    x=(x-1)<>5
    print(x)
    .
    .


    Both of these routines will produce:
    9
    8
    7
    6
    5
    5
    5 and so on...


    To limit a number going up just use the other operator:
    pos=10
    function gameLoop(e)
    x=(x+1)><15
    print(x)
    .
    .


    This will produce:
    11
    12
    13
    14
    15
    15
    15 and so on...

  • hgy29hgy29 +1 -1 (+2 / -0 )
    Maintainer
    When I need to ensure a value is within ttwo specific bounds (for exemple when positionning my scene so that the player is visible without showing blank spaces if the player goes to the corner of the scene), I use:
     x=(x<>min)><max

    Likes: SinisterSoft, pie

  • Furthering @hgy29 's very nice example - This is showing something you may do in your game:
    dir=-1
    x=10
    min=5
    max=15
    function gameLoop(e)
    x=x+dir
    if x<min then
    x=min
    elseif x>max then
    x=max
    end
    print(x)


    Can be turned into...
    dir=-1
    x=10
    min=5
    max=15
    function gameLoop(e)
    x=((x+dir)<>min)><max
    print(x)
  • Here is another example to always return the negative of a number:
    x=10
    function gameLoop(e)
    x=-x><x
    print(x)


    And another to always return the positive of a number:
    x=-10
    function gameLoop(e)
    x=-x<>x
    print(x)


    This should be faster than
    x=math.abs(x)


    or
    if x<0 then x=-x end


    But I haven't done any speed check to make sure. :)
  • hgy29hgy29 +1 -1 (+1 / -0 )
    Maintainer
    Nice tip for abs() replacement!

    Likes: SinisterSoft

  • I don't know if Lua (internally) does any caching of variables for the result, left or right sides. It would be good if it did - or maybe I could add a check for this in the comparison. Searching though tables for variables must be the thing that slows things down the most.
  • Interesting additions... incidentally, are the Gideros Lua extensions documented in one place anywhere? If not, what are they all?
  • to find the greater
    a <> b

    to find the lesser
    a >< b

    to convert from degrees to radian
    ^< a

    to convert from radians to degrees
    ^> a

    bitwise operators
    a|b bor
    a&b and
    ~a not
    a~b xor
    a<<b shift a left by b bits<br />a>>b shift a right by b bits
    a//b integer divide use instead of floor(a/b)

    macros
    pi@3.142 set pi to 3.142 - isn't a variable so it acts like a constant

    also the saving/loading of lua byte code is universal between different processor word sizes - eg 32 and 64 bit - unlike regular Lua.

  • snooks +1 -1 (+1 / -0 )
    Member
    Cool, thanks.

    Likes: SinisterSoft

  • SinisterSoftSinisterSoft +1 -1 (+2 / -0 )
    Maintainer
    Don't forget that x=-x<>x should also be faster than math.abs(x) or if x<0 then x=-x end<br />

    Likes: antix, Atavismus

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Login with Facebook Sign In with OpenID

In this Discussion

Top Posters