Coroutine annotation


#1

As much as I love the Flow module from LudoBits. I think a code annotation or “prefix” for the public script functions would be amazing.

co function init(self)
    co.yield_seconds(1)
    co.yield_frames(1)

    co.yield_until_true(function()
        return (self.lives < 0)
    end)

    for _,var in pairs(array) do
        go.animate(....)
        co.yield_seconds(0.5)
    end
end

Here I also added a “co” module as well to work with the basic flow functions. But by making this a concept of the editor, much the same as the msg module. It would encourage more developers to work in this fashion with cinematic functions.

In addition, this could also enable a reactive design where results are passed back from functions rather than callbacks if wrapped in a coroutine. But unlike the other this would be a breaking change, so would require a game.project checkbox for migration.

co function init(self)
    local id, response = http.request("www.google.com", "GET")
    print("Call finished")
end

function init(self)
    http.request("www.google.com", "GET", function(self, id, response)
        print("Call finished")
    end)
end

#2

Well, I mean, I like the whole idea of untangling callbacks using coroutines, but I’m not sure about making it a part of the language or making it into some kind of Defold specific version of the Lua language. Wouldn’t your proposed syntactical changes mess with everything from linters to stand alone Lua interpreters?


#3

True that would be the case. My initial thought was that if all Defold functions would be called from an engine coroutine, that would enable the second part of sync behaviour. An option to preserve linting would be to use a checkbox in game.project or the particular script to do it for all public script calls. Even if that leaves you with less flexibility, the overall idea would be the same.