private
function
runEvent(line:
String
):
void
{
if
(line.substring(
0
,
2
) ==
"::"
)
{
// Check if the function has arguments.
if
(line.split(
"@"
)[
1
] !=
null
)
// Execute function written in string with arguments.
this
[line.split(
"@"
)[
0
].substring(
2
)](line.split(
"@"
)[
1
]);
else
this
[line.split(
"@"
)[
0
].substring(
2
)]();
}
}
Reflect.callMethod(this, line.split(
"@"
)[
0
].substring(
2
), [
line.split(
"@"
)[
1
]
]
);
class Test { static function main() { new Test(); } function new() { var dialog = [ Line("First line of the dialog"), Line("Next we execute a function"), Event(eventWithoutArguments), Line("Next up, we use bind() to call a function with arguments"), Line("see http://haxe.org/manual/lf-function-bindings.html for more info"), Event(eventWithArguments.bind(-3, 10)), Line("Dialog done!") ]; runDialog(dialog); } function runDialog(dialog:Array<DialogEntry>) { for (entry in dialog) { switch (entry) { case Event(func): func();
case Line(text): trace(text); } } }
function eventWithoutArguments() { trace("No-argument-function called"); }
function eventWithArguments(x:Int, y:Int) { trace("Function called with x=" + x + " and y=" + y); }}
enum DialogEntry { Event(func:Void->Void); Line(text:String);}