I've decided to try making my combat system using a multi-dimensional table, but I'm having some trouble: The indices of the tables (excluding the last one in the stack) are no longer numerical, and I'm therefore unable to work with them as I normally do. I went around this by ending each table's name with a number, then using tonumber(string.sub(tableName, endofTableName)). However, this doesn't help me when I'm attempting to find the length of the table, because #Table simply values at 0. Any ideas on how to fix this? Thanks a ton! :) --EDIT-- To clear up the question a bit, since it's pretty hard to understand, let me give an example;
Attack = { Combo = { Pose1 = { 1, 2, 3 }, Pose2 = { 1, 2, 3 } } }
How would you find the length of the table Combo? #Attack.Combo wouldn't work, as that would simply return 0. Sorry about the question being hard to understand, I often have a hard time explaining myself.
I got something for you.
mda = { t1 = { t2 = { hi = "hi" } } } function length(t) assert(type(t) == "table", "Argument must be a table.") local elements = 0 for _, v in pairs(t) do elements = elements + 1 end return elements end print(length(mda.t1.t2))
Oh ok, Thanks for more detailled it. What I'll do for now it show you how to got the lenght of a table. First we'll create a function named "getTableLenght" with parametre "t"(table) in this function we'll declare a variable named "lenght" or whatever you want with the value 0. After we'll get all thing in the table you want to got the childs and adding 1 when he found child.Finally we returnning the lenght of the table.
Attack = {-- the multi-table here Combo = { Pose1 = { 1, 2, 3 }, Pose2 = { 1, 2, 3 } } } function getTableLenght(t)--t is table assert(type(t) == "table", "Table expected !") -- check if the parametre 't' is a table else ending the function local lenght = 0; -- declared the lenght of the table for i, v in pairs(t) do -- got all thing in the table lenght = lenght + 1; -- add 1 to the variable "lenght" when found a child end return lenght; -- Returnning the lenght(should return 2) end local t = getTableLenght(Attack.Combo); --running the function print(t);--printting lenght of the table
for more nice you can also do:
Attack = {-- the multi-table here Combo = { Pose1 = { 1, 2, 3 }, Pose2 = { 1, 2, 3 } } } function getTableLenght(t)--t is table assert(type(t) == "table", "Table expected !") -- check if the parametre 't' is a table else ending the function local lenght, result = 0, false; -- declared the lenght of the table and the result with a defaul value as "false" for i, v in pairs(t) do -- got all thing in the table if type(v) == "table" then --look if a table is in the table result = true; --set the value of result end lenght = lenght + 1; -- add 1 to the variable "lenght" when found a child end return lenght, true; -- Returnning the lenght(should return 2) and true end local t, otherTable = getTableLenght(Attack.Combo); --running the function print(t, otherTable);--printting lenght of the table and true
Instead of using # (which only works for numerical indexes), you could just add one to a variable for each index that is found when looping through a table using a for loop.
For example, you could createa function like so which returns the number of indexes in a table.
function GetTabLength(table) if type(table) ~= "table" then return end --Stop the function if table is nil. local len = 0 --Start with a length variable of 0. for i,_ in pairs(table) do --Loops through each index of the table. len = len + 1 --Adds one to the length variable for each table index. end return len --Return the final len value for use. end print(GetTabLength(Attack.Combo)) --Should print 2 as there is two indexes.
I hope my answer helped you. If it did, be sure to accept it.