Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Lua: built-in macro — Gideros Forum

Lua: built-in macro

n1cken1cke Maintainer
edited October 2016 in Step by step tutorials
Since 2016.08 release we have added built-in macro support to Lua, so now you can use it anywhere. To denote macro definition we are using `@` sign (instead of `=`) so you can read `x@100` as "x at 100". Even in it's current basic form (which will be extended in the feature) it is already useful for:
◘ simple numeric and string constants to make your code little faster:
num1 @ -100.54
num2 @ 232
num3 @ 444.10
str1 @ 'hello'
str2 @ "world"
str3 @ [[
Hello,
world!
]]
◘ language sugar without any overhead:
on @ true
off @ false
fn @ function
ael @ addEventListener
◘ reducing amout of repetitive code:
add @ |stage:addChild|
Note: you can use any of this delimiters after `@` but they should be in pairs:
\`~ ! # $ % ^ & * / + = |
◘ to "include" files like in C:
-- define your lib in init.lua
include_my_lib @ \
local f = function(...) return ... end
-- your functions here
\
How to use defined macros:
You just need to type macro name you have defined before and it will be extended to it's macro body, for example:
pi @ \math.pi\
print(pi + pi / pi ^ pi)
All defined macros are constants, so you will get an error if you will try to redefine them with `@` instead.
You can also put one macro inside another macro and put them inside third. The sky's the limit:
what @ /
@ |
@ \
  LOL @ !
   "LOL"
  !
 \
|
/
what
what
what
print(LOL) -- guess what you will get <img class="emoji" src="https://forum.giderosmobile.com/resources/emoji/smile.png" title=":)" alt=":)" height="20" />
Some useful macros:

◘ commenting `print`:
print @ |--|
Usage:
print(x, y, z, x + y, y * z)
-- this line will be skipped

◘ benchmarking:
time_ @ |local _time_ = os.timer()|
_time @ |print("TIME:", os.timer() - _time_)|
Usage:
local x = 0
time_ for i = 1, 1e8 do x = x + i end _time
◘ random color:
rnd @ |math.random(0, 0xFFFFFF)|
Usage:
local pixel = Pixel.new(rnd, 1, 200, 200)
+1 -1 (+9 / -1 )Share on Facebook

Comments

  • @n1cke I can't get any of your examples to work

    Example: print @ |--/

    I get an unfinished macro definition. I've tried a few configuration, but I'm guessing.
  • hgy29hgy29 Maintainer
    @simwhi, the replacement should be enclosed into two vertical bars (pipes): |--|
  • n1cken1cke Maintainer
    edited August 2016
    @simwhi: sorry, I was a bit tired yesterday. I have checked and fixed all errors in examples.
    Every macro body if it is not simple (like identifier, string or number) should be enclosed into pairs of acceptable chars after `@`.
  • @n1cke I must be missing something really simple.

    print @ |--| results in an error: main.lua:91: 'end' expected (to close 'function' at line 57) near '' in Gideros Studio.

    Under settings, macro support is turned on.

    I think there's a problem with ZeroBrane Studio, too.
  • n1cken1cke Maintainer
    You need to turn off Macro in Settings. This is new version independent from Studio and it is built-in into Lua.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • I've replaced all my actual constants with macros, should speed things up a bit. But what about values that are constants in principle, such as application:getLogicalTranslateX()? Is there a way to use macros to access those, instead of calling the function or saving it in self (self.translate_x)?
    My Gideros games: www.totebo.com
  • n1cken1cke Maintainer
    edited September 2016
    Macro doesn't have an access to Gideros libraries (like `application`) while exporting so you cannot use it to precalculate such values (or it won't work in exported app). That's because on export each file is compiled into Lua bytecode without opening any Gideros libs. The only way is to make function call shortcut if you don't want to store translateX in a variable:
    LTX @ |application:getLogicalTranslateX()|
  • Got you. Great info, thanks!
    My Gideros games: www.totebo.com
  • You should therefore make it a variable.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • Yeah. When I find a way to optimise I get an urge to do it everywhere. :)

    To date, these beauties are indispensable:

    Callbacks instead of events
    Local vars (abs instead of math.abs)
    Pooling
    Macros (NEW!)

    I using these religiously now whenever it's possible. Do let me know of other speedup beauties!
    My Gideros games: www.totebo.com
  • gideros shows an error
    --init.lua
    HW @ |print("hello world")|
    --main.lua
     
     HW

    image.png
    525 x 84 - 4K
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • MoKaLuxMoKaLux Member
    edited December 2019
    A couple of optimisation questions please:
    Given this code:
    gp @ |getPosition()|
    xabs @ |math.abs|
     
    local deltax
    function Map:onEnterFrame(e)
    	playerposx, playerposy = self.player1:gp)
    	deltax = xabs(self.visibletiles[m]:getX() - self.player1:getX())
    end
    1- is it worth using gp @ |getPosition()|?
    2- is it better to declare the deltax outside of the gameloop?

    BONUS
    3- I've read somewhere (but can't find it anymore) that if you don't use application:setBackgroundColor(...) you can turn it off to save some frames. Do you know how?

    Thank you for your help.
    PS: I am also using gideros built-in profiler but wanted to ask.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • SinisterSoftSinisterSoft Maintainer
    edited December 2019
    You can save a few GPU cycles with:
    stage:setClearColorBuffer(false)
    That will stop the background from being cleared. You must draw something though that fills the background - don't assume it will be what was drawn on the previous frame (this is what happens on Windows).

    All macros do is replace text with other text, normally this doesn't speed things up, just allows you to code using less text or make code more readable or easy to maintain. The exception is if you use macros to simulate constants and they are evaluated before being turned into byte code, eg:
    maxLen=20
    pos=maxLen-1
    takes longer than
    maxLen@20
    pos=maxLen-1
    as this will be converted to
    pos=20-1
    and then further converted to
    pos=19
    Which is faster than the first example as there is no initial assignment and no subtraction calculation when converted to bytecode. You may only save a few cycles though.

    Macros simulating constants also saves on variables (Lua has some limits on the maximum number of variables).

    For macros replacing maths.abs, you should look at the other lua enhancements instead, see:
    x=-x<>x -- faster than math.abs
    More at:
    https://wiki.giderosmobile.com/index.php/Lua_enhancements


    Likes: MoKaLux, oleg

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+2 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited December 2019
    thank you very much SinisterSoft much appreciated. Will check the wiki :)
    MoKaLux said:

    BONUS
    3- I've read somewhere (but can't find it anymore) that if you don't use application:setBackgroundColor(...) you can turn it off to save some frames. Do you know how?

    (I knew I had read it somewhere :) in the wiki!)

    Likes: SinisterSoft

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • Here is the help for the setClearColorBuffer command:
    https://wiki.giderosmobile.com/index.php/Stage:setClearColorBuffer

    Likes: MoKaLux

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.