Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Using the < > and > < custom Gideros operators. - Gideros Forum

Using the < > and > < custom Gideros operators.

SinisterSoftSinisterSoft Maintainer
edited September 2017 in Code snippets
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...

Comments

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

    +1 -1 (+2 / -0 ) Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    edited September 2017
    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 Maintainer
    Nice tip for abs() replacement!

    Likes: SinisterSoft

    +1 -1 (+1 / -0 ) Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    edited September 2017
    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?
  • SinisterSoftSinisterSoft Maintainer
    edited September 2017
    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
    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.

  • Cool, thanks.

    Likes: SinisterSoft

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

    Likes: antix, Atavismus

    +1 -1 (+2 / -0 ) Share on Facebook
Sign In or Register to comment.