Old 04-06-2012, 08:40 AM   #1
Justin's Avatar
Join Date: Jan 2005
Location: NYC
Posts: 11,697
Default JSFX function support discussion thread

Hello everybody,

Function support has the following features/restrictions:
  • Functions can only be declared at the top level of JS code (i.e. not within a () block etc).
  • Functions defined in @init can be used in any other section, but functions defined elsewhere can only be used in that section.
  • Functions can have 0 or more parameters, and any number of local variables.
  • Functions can not call recursively (a function can only call functions defined prior to that function)
  • Functions have very low overhead, and are automatically inlined if they are below a certain size.
  • The last statement to be executed in a function is effectively the return value (matching the logic used elsewhere in JS).

The most basic declarations and use of functions might be:

function getSomeValue()  (

spl1 = spl1 * getSomeValue();

function applyGain(spl)  (
  spl * 2;

spl1 = applyGain(spl1); // same as spl1 = spl1 * 2;


function applyGain(spl,scale)  (
  spl * scale;

spl1 = applyGain(spl1,2); // same as spl1 = spl1 * 2;
If you wish to define local variables for the function, you can do it by adding one or more local() definitions to the declaration:


function applyWave(spl)  
  local(somecounter, scale)
  scale < 1.0 ? scale += 1.0/srate;
  somecounter += 1.0/srate;
  spl * sin(somecounter) * scale;

spl1 = applyWave(spl1);
Local variable can be used for temporary storage within the function, but they also persist across calls. If you define a function in @init and use it within two other sections (say, @gfx and @sample), your local variables will (now as of April 14) be in separate spaces -- so if you need them shared, you would want to use instance variables.

OK, post any questions or feedback you have here (especially if you see any regressions on existing FX or other code).

Last edited by Justin; 01-17-2014 at 09:15 AM.
Justin is offline   Reply With Quote
Old 04-06-2012, 10:30 AM   #2
Human being with feelings
Banned's Avatar
Join Date: Mar 2008
Location: Unwired (probably in the proximity of Amsterdam)
Posts: 4,868

Wow... That is awesome.
˙lɐd 'ʎɐʍ ƃuoɹʍ ǝɥʇ ǝɔıʌǝp ʇɐɥʇ ƃuıploɥ ǝɹ,noʎ
Banned is offline   Reply With Quote
Old 04-06-2012, 01:18 PM   #3
Human being with feelings
dub3000's Avatar
Join Date: Mar 2008
Location: Sydney, Australia
Posts: 3,801

OK, this is pretty awesome. Thanks!
dub3000 is offline   Reply With Quote
Old 04-06-2012, 01:33 PM   #4
Human being with feelings
Tale's Avatar
Join Date: Jul 2008
Location: Holland
Posts: 2,906

Tale is offline   Reply With Quote
Old 04-06-2012, 02:31 PM   #5
Human being with feelings
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,343

IXix is offline   Reply With Quote
Old 04-06-2012, 02:56 PM   #6
Human being with feelings
captain_caveman's Avatar
Join Date: Dec 2011
Posts: 999

Great stuff!

It doesn't appear to be possible to use gfx_ draw stuff within functions....
@gfx  400 400

function drawSquare(tlx, tly, size)
    x = tlx; y = tly;
    gfx_x = tlx;
    gfx_y = tly;
    gfx_lineto(tlx+size, tly, 1);
    gfx_lineto(tlx+size, tly+size, 1);
    gfx_lineto(tlx, tly+size, 1);
    gfx_lineto(tlx, tly, 1);

function setColours(r,g b) 
    gfx_r = r;
    gfx_g = g;
    gfx_g = b;
    a = 5;


gfx_a = 255; 
//gfx_r = 255; gfx_g = 255; gfx_b = 255;

SetColours works and x and y are set in drawSquare, but nothing is drawn, although gfx_x and gfx_y change. gfx_lineto draws a line.

It's probably me, although it seems it should work.
captain_caveman is offline   Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -7. The time now is 04:57 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.