OpenFL - Chain functions (or queue functions)

Hello

I’m doing a little library to start working in Haxe + OpenFL, and I want to make a class which can execute queued functions stored in an array.

Researching I found this code in a Github and went very close to it, but it uses something called done, take a look:

private function chain(p_functions:Array<Dynamic>)
{
    var functions:Array<Dynamic> = p_functions.copy();
    if (functions.length > 0)
    {
        var firstFunction = functions.shift();
        var firstPromise = firstFunction();
        firstPromise.done(function ()
        {
            chain(functions);
        }
        );
    }
}

Original Github Code

I tried to implement it like this but I don’t know from where he’s using that “.done” in a variable function, maybe it’s custom or java only but my questions are…

  • Do Haxe or OpenFL have something similar that could work like this and cross platform?
  • If so, can be used even with Actuate events (without relying on their ‘OnComplete’)?
  • If doesn’t, there’s any other basic workaround without timers?

Thanks in advance!

May be “done” is a Haxe static extension ?

1 Like

Wouldn’t looping through the array do the same thing?

var ar:Array<Dynamic> = [function1, function2, function3];
startChain(ar);
function startChain(chain:Array<Dynamic>):Void
{
        for(i in 0...chain.length)
        {
            chain[i]();
        }
}
 function function1():Void
    {
        trace('1');
    }
    function function2():Void
    {
        trace('2');
        function2sub();
    }
    function function2sub():Void
    {
        trace('2sub');
    }
    function function3():Void
    {
        trace('3');
    }

logs 1, 2, 2sub, 3
Timers inside of chained functions would break this though.

1 Like

Thanks to both!

Yes @T1mL3arn it must be the Static Extension, I didn’t know about it but looks really nice, there’s much to learn in Haxe

@hcwdikk in theory it’s the same and running thanks a lot, but that chain will run everything asap and my main goal is to do one function at a time without relying on Actuate or timers, more like dispatch events

Take another look at this part:

var firstPromise = firstFunction();
firstPromise.done(/*...*/);

firstPromise isn’t a function, it’s a jQuery Promise object that was created and returned by firstFunction(). The Promise object is what implements the done() method.

1 Like

Awesome thanks @player_03, I saw the imported JS libraries but thought it was using just Haxe syntax, looks very nice, do we have something similar and basic in Haxe or OpenFL?

Well, you could return an EventDispatcher object, and then listen for the COMPLETE event on that object.

Of course, you have to make sure the EventDispatcher actually dispatches the event at the right time. Loaders already do this, so you can just return the contentLoaderInfo object, but otherwise you’ll have to implement it yourself.

1 Like

I should’ve known that was too easy to be true :smile:

Yes I’ll have to try that, it should work with a combination of EventDispatcher and the use of OnComplete (when using Actuate) to trigger the ‘chained’ event.

Thanks!

Take a look to https://github.com/juakob/SequenceCode
The idea of the lib is to execute functions sequentially.

1 Like

Awesome, thanks a lot! you got the same concept and it’s great, i’ll check it out soon.

Let me show you what I had done, simple but was trying to achieve the same.

class SceneManager {
    var collectedTasks = new Map<String, String->Void>();
    var sceneList = new Map<String, Array<String>>();    

    public function new() { }
    
    // Adding object functions
    public function setUpObjects(taskName:String, task:String->Void):Void {
        collectedTasks.set(taskName, task);
    }
    
    // Asociate Objects to Slides
    public function setUpScene(sceneName:String, sceneItems:Array<String>):Void {    
        sceneList.set(sceneName, sceneItems);
    }
    
    // Execute functions
    public function execFunctions(phase:String):Void {
        for (k in collectedTasks.keys()) {
            var newF = collectedTasks.get(k); 
            newF(phase);
        }        
    }
    
    // Slides transition
    public function switchScene(current:String, next:String):Void {   // Home to Page 1
        var currentList:Array<String> = new Array<String>();
        var nextList:Array<String> = new Array<String>();
        
        var functionsCurrent:Array<Dynamic> = new Array<Dynamic>();
        var functionsNext:Array<Dynamic> = new Array<Dynamic>();
                
        currentList = sceneList.get(current);
        
        // Actors parsed from Scene
        for (i in 0 ... currentList.length) {
            trace(currentList[i]);
            functionsCurrent.push(collectedTasks.get(currentList[i]));
        }
        
        chain(functionsCurrent);
    }
    
    // Chain functions
    private function chain(p_functions:Array<Dynamic>):Void {
        var functions:Array<Dynamic> = p_functions.copy();
        
        if (functions.length > 0) {
            var firstFunction = functions.shift();
            var firstPromise = firstFunction("fadeCurrent");

            // Testing .done first
            firstPromise.done(trace("ding!"));
        }
    }
)

I don’t understand all your code, because I don’t know the domain of your problem, but I think we are trying to solve the same problem.

I made that class because I was tired of messing up my classes with flags and ifs. The final code was always hard to understand and modify.

I use this class a lot and I think my code is 100% more clear and 1000% easier to experiment variations. Changing the behavior of an enemy is a peace of cake.

I use it specially in NPC or simple AI, but its also great for menu transitions. You just need to return true in your functions when the action is finish.

I posted a small explanation of how it works http://maleficgames.com/wp/?p=13

1 Like

You might also check the task library, which helps sequence chains of functions like this

1 Like

@juakob Yea sorry, it’s just a prototype and the chain purpose it’s mixed with another problem which I wanted to deal at the same time, managing ‘Scenes’ like switching pages or states like in a menu, where I would fade out or move away elements and then bring new ones. But like you say we are trying to address the same problem.

Awesome that’s the point, having a clear code where we can experiment with it without breaking anything lol, and focus on things that matters. Was looking at your blog and it’s very nice, I’ll keep an eye in it hoping you make more posts; also I’m from Mex, so if there’s way to contact you (aside twitter) in spanish would be great.

Thanks @singmajesty, taking a quick look it seems like my approach was more similar to your library than the @juakob lib.

Finally I’m seeing the light at the end of the tunnel, this has haunted me since the AS3 days of flash and quickly turned my work into spaghetti code rofl. I’ll try these libraries and surely gonna learn a lot, thanks for sharing them!

Btw this kind of post belongs to this category or should be in programming?

Thank you for finding my post interesting, I have to write more, but it’s not easy for me to write in english. @Shoobis you can send me PM