Action scheduling with Lua

One of the downsides to moving between programming languages is that you inevitably lose all the base code you’ve gotten used to using. When I was using Actionscript 3 regularly, I wrote Slang to meet the lack of a scripting language that ran inside of Flash and ended up using it for a number of purposes in my subsequent game projects. The most useful application I found for it was for setting up cutscenes and scripted sequences, and I’ve missed using it now that I’m spending more of my time using C#.

Rather than porting Slang to C# (which would have been quite an ordeal), I’ve been playing around with Lua in an attempt to recreate a similar system. Here’s the first draft of how a scene might be set up.

--    file: humphrey.lua
action(function()
    print("start")
    delay(3)    --    wait three seconds
    done()      --    move to the next action
end)

action(function()
    print("after delay")
    signal("humphrey done")    --    send a signal to the other actors
    done()
end)
--    file: abe.lua
action(function()
    print("start abe")
    done()
end)

await("humphrey done", function()    --    wait for a signal
    print("got signal")
    message("walk", {x = 100, y = 50})  --  send a message to the entity
    done()
end)

await("walk done", function()     --    signals can be sent from the application too
    signal("abe done")
    done()
end)

The above scripts run in parallel with each other, communicating through signals and message passing. Since no actor is directly aware of another, or even of the type of entity it’s controlling, the system is very flexible. By configuring entities to respond to certain messages, gameplay logic can be reused during scripted sequences; for example, to make characters animate as they walk around, or play audio as they talk.

Since Lua is a complete language all by itself, using it automatically extends the system with a bunch of cool features I was missing in Slang, like loops and arrays. It seems to be working in C# quite well so far thanks to the MonoBoxedLua library. I don’t care much for Lua as a language overall; I find its lack of static typing to be problematic on big projects, and I’d much rather use a different scripting library for more in-depth gameplay code, but for small applications like cutscenes I think it’s a great fit.