trick for functions with optional parameters by overloading functions, where the overloaded functions use previously declared functions.
Code:
function setcolor(r, g, b) (
gfx_r = r;
gfx_g = g;
gfx_b = b;
);
function setalpha(a) (
gfx_a = a
);
function setcolor(g) (
setcolor(g,g,g)
);
function setcolor(g,a) ( //g as in grey
setcolor(g,g,g);
setalpha(a);
);
function setcolor(r, g, b, a) (
setcolor(r,g,b);
setalpha(a);
);
for easy swapping of variables
Code:
function swap(a*,b*) local(t) (a = t; a = b; b = t);
usage:
a=1;
b=2;
swap(a,b);
now a=2 and b=1
bit twiddling hack to get the power of 2 of an integer, useful for checking trigger buttons
Code:
function bitPow2(a) local(b) (
b=-1;
while(a) (
a = a >> 1;
b+=1;
);
b;
);
2^5 is 32
trigger button #5 is 32
bitPow2(32) returns 5.
this is how to make any Log function, here's how to make Log2.
Code:
@init
log2 = log(2); //establish your log constant
function log2(f) ( log(f)/log2 ); //create your function