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:
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.gideros.rocks/resources/emoji/smile.png" title=":)" alt=":)" height="20" /> |
Some useful macros:◘ commenting `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) |
Comments
Example: print @ |--/
I get an unfinished macro definition. I've tried a few configuration, but I'm guessing.
Every macro body if it is not simple (like identifier, string or number) should be enclosed into pairs of acceptable chars after `@`.
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.
Likes: SinisterSoft
https://deluxepixel.com
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!
https://play.google.com/store/apps/developer?id=razorback456
мій блог по гідерос https://simartinfo.blogspot.com
Слава Україні!
Given this code:
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.
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:
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:
https://wiki.giderosmobile.com/index.php/Lua_enhancements
Likes: MoKaLux, oleg
https://deluxepixel.com
Likes: SinisterSoft
https://wiki.giderosmobile.com/index.php/Stage:setClearColorBuffer
Likes: MoKaLux
https://deluxepixel.com