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
    Слава Україні!
Sign In or Register to comment.