Search code examples
luagideros

Create a list of tasks to do in Lua


I'm creating a turn-based game in Gideros (which uses Lua) and I'm running into a problem of recording the events of each actor. I want the player to be able to set a path, direction, and fire event for every actor they control. I had an idea of using multidimensional arrays like

TaskList = { 1 = { "MoveTo", {3,5} }, 
             2 = { "AimTo", {5,2} }, 
             3 = { "Fire" }, 
             4 = { "MoveTo", {23,21} }

however that seemed like an awfully annoying and inefficient way to do it. Is there any obviously better way? Thanks.


Solution

  • I would suggest the following:

    • Remove 1 =, 2 =, etc. This is implied with "array" initialization syntax.
    • Use whitespace to improve readability.
    • Consider being more verbose in the action tables by using names instead of indexes. (This will also make the structure more future-proof, as you can add/remove support for certain attributes without having to change the index where each attribute exists.)

    For example:

    TaskList = {
        { action="MoveTo", location={3,5} },
        { action="AimTo",  location={5,2} },
        { action="Fire" },
        { action="MoveTo", location={23,21} }
    }