hscript - how to use same script, but different return result?

48 views
Skip to first unread message

BP

unread,
Mar 29, 2013, 1:47:31 PM3/29/13
to haxe...@googlegroups.com
Hey! I'm new to hscript, and I was wondering how to make a script return a different function result.

For example, say I had something like this as an hscript (pseudocode):

function a()
{
  return 5;
}

function b()
{
  return 1;
}


How can I compile that script once, and decide from Haxe which function to call to get the result of my script, a or b? I know I could pass in variables, and those variables could call something different at the end of the script, like so:

if (useA)
{
  return a();
}
else
{
  return b();
}

but I'm planning on having lots of user defined scripts, and lots of functions in my scripts, and I don't want them to have to have this pseudocode at the end of them all. I know of course that I could just compile the script a bunch of different times with different function calls at the end, but it seems like it'd be nice to be able to just compile it once...

Thanks,

Brad

Juraj Kirchheim

unread,
Mar 29, 2013, 2:04:43 PM3/29/13
to haxe...@googlegroups.com
Off the top of my head, this should work

    var source = "{
        a: function () return 5;
        b: function () return 1;
    }";
    var f:{ a:Void->Int, b:Void->Int } = parseAndInterpret(source);

Regards,
Juraj

BP

unread,
Mar 29, 2013, 10:16:17 PM3/29/13
to haxe...@googlegroups.com
hey! I tried that, and it worked for simple cases, but not complex ones. For example, if function a called b, then it failed, as the variables that should've been defined in b weren't.

But I am using your idea of returning a function reference, and executing it and it seems to work fine, so at least I can pass in data without actually setting it in haxe code.... which is nice!

I'll post back if I find a better solution.... thanks!
Reply all
Reply to author
Forward
0 new messages