Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
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 @ [[
◘ 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 @ !
print(LOL) -- guess what you will get <img class="emoji" src="/resources/emoji/smile.png" title=":)" alt=":)" height="20" />
Some useful macros:

◘ commenting `print`:
print @ |--|
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_)|
local x = 0
time_ for i = 1, 1e8 do x = x + i end _time
◘ random color:
rnd @ |math.random(0, 0xFFFFFF)|
local pixel = Pixel.new(rnd, 1, 200, 200)
+1 -1 (+9 / -1 ) Share on Facebook


  • @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
  • 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)
    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
    HW @ |print("hello world")|

    525 x 84 - 4K
    my games:
    мій блог по гідерос 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())
    1- is it worth using gp @ |getPosition()|?
    2- is it better to declare the deltax outside of the gameloop?

    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.
  • SinisterSoftSinisterSoft Maintainer
    edited December 2019
    You can save a few GPU cycles with:
    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:
    takes longer than
    as this will be converted to
    and then further converted to
    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:

    Likes: MoKaLux, oleg

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

    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

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