The tables in my combat system for aetolia look like this:
limbs_list = {
"left leg",
"right leg",
"left arm",
"right arm",
"head",
"torso"
} --limbs_list
limbs_default = { -- default values for all limbs
["damage"] = 0,
["parrying"] = false,
["needparry"] = false,
["triedparry"] = false,
["lasttriedparry"] = 0,
["lasthit"] = false,
["restoring"] = false,
["broken"] = false,
["damaged"] = false,
["mangled"] = false,
["worst"] = false
} -- limbs_default
limb_vars = {
"damage",
"parrying",
"needparry",
"triedparry",
"lasttriedparry",
"lasthit",
"restoring",
"broken",
"damaged",
"mangled",
"worst"
} -- limb_vars
my_limbs = {} -- initialize my limb table
for _, cur_limb in ipairs (limbs_list) do -- populate my limb table with limbs
my_limbs[cur_limb] = {}
for _, cur_var in ipairs (limb_vars) do -- populate my limb tables's limbs with variables
my_limbs[cur_limb][cur_var] = limbs_default[cur_var] -- set all those values for each limb to their default value
end -- for limb_vars
end -- for limbs_list
target_limbs = {} -- initialize my target's limb table
for _, cur_limb in ipairs (limbs_list) do -- populate my limb table with limbs
target_limbs[cur_limb] = {}
for _, cur_var in ipairs (limb_vars) do -- populate my limb tables's limbs with variables
target_limbs[cur_limb][cur_var] = limbs_default[cur_var] -- set all those values for each limb to their default value
end -- for limb_vars
end -- for limbs_list
There is a very similar structure for tables to track my and my opponent's balances, afflictions, etc.
Currently, my functions for retrieving useful values from those tables look like:
function GetLimbs(var, limb) -- returns the value of a given variable for a given limb
return (my_limbs[limb][var])
end -- func
function GetTargetLimbs(var, limb) -- returns the value of a given variable for my target's given limb
return (target_limbs[limb][var])
end -- func
Again a similar structure for the other types of information.
I would like a more general function, one to retrieve any value from any table, something like
function Get(who, table, key1, key2)
return (who_table[key1][key2])
end -- func
But that syntax isn't working, most likely because
Get("my", "limbs", "left arm", "parrying") is trying to return who_table too literally, or doesn't know what to do with the string values of those two arguments (who and table) in the context of processing them as variable names.
I recognize that this could be done by combining all my tables into one super table i.e.
function Get(who, table, key1, key2)
return (AMTS_vars[who][table][key1][key2])
end -- func
Get("my", "limbs", "left arm", "parrying")
--would then return the value of AMTS_vars["my"]["limbs"]["left arm"]["parrying"]
But surely that's not my only recourse! Halp?
For a more illustrative and slightly simpler example of what I am trying to do, have a look here: http://pastebin.com/S07m4u69
|