Question

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.

Was it helpful?

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} }
}

OTHER TIPS

What about constructing your array through function calls?

function MoveTo( a, b ) return { action="MoveTo", location={ a, b } } end
function AimTo( a, b ) return { action="AimTo", location={ a, b } } end
function Fire() return { action="Fire" } end

TaskList = { MoveTo( 3, 5 ),
             AimTo( 5, 2 ), 
             Fire(),
             MoveTo( 23, 21 ) }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top