Putting an existing table inside another existing table

I am working on an equipment screen for my current project.

I have one table that represents all of my characters, stats, and equipment.

local myData = { [1] = {name = "Blair", frame = 3, active = nil, class = "Warrior", maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = nil, shieldEquip = nil, trinket1 = nil, trinket2 = nil, }, [2] = {name = "Lily", frame = 4, active = nil, class = "Rogue", maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .16, weaponEquip = nil, shieldEquip = nil, trinket1 = nil, trinket2 = nil, }, [3] = {name = "Kylianna", frame = 2, active = nil, class = "Mage", maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = nil, shieldEquip = nil, trinket1 = nil, trinket2 = nil, }, }  

I have another table that represents all the equipment in the players inventory.

local equipInventory = { [1] = {name = nil, frame = 4, description = "Weapon", class = "Warrior", strength = 5, armor = 0, plusHealth = 0, plusSpeed = 0, }, [2] = {name = nil, frame = 2, description = "Armor", class = "Warrior", strength = 0, armor = 5, plusHealth = 0, plusSpeed = 0, }, [3] = {name = nil, frame = 3, description = "Trinket", class = "Warrior", strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, }

The first question is this, when i’m on the equipment screen, I am trying to change the value of myData[1].weaponEquip to equipInventory[1] that way when I look up the value of myData[1].weaponEquip, I can then see all the characteristics of the weapon that was equipped.

Would it be something as simple as creating a function that states myData[1].weaponEquip = equipInventory[1]? 

Second question, once the above is achieved, I need some guidance to access a specific table value. within equipInventory[1] you can see that I have a value for frame as 4. This represents an image on a sprite sheet.

I would want to be able to change the image of an object based on that value, would this value be found with myData[1].weaponEquip.frame? 

basically im trying to figure out how to look inside the character table to find the equipment table that was placed in a value and then find a single value on that table.

Sorry if its a little confusing, I’ll try to clear it up if need be 

Ethan,

Hope this info helps.  Made a few small suggestive changes that I hope you find make sense. 

  1. You can use the [1] = {  } for each table index, but that is not required.(maybe you already know that) but I took them out of the 

myData table just in case you were not aware of that.

  1. Just a suggestion :  fields like ‘active’ (i am guessing a flag that player is active or not active, I think are best not left nil) if it is a boolean type variable, mark it ‘false’ or ‘true’ … but again, maybe you already know this.  I normally try to avoid nil values for fields.

Also, there are a lot of ways to do what you are asking (if I understand your question correctly).

Here are just 3 ways I thought of fairly quickly.

These are a quick and direct answer to your question, although I think a modular or pseudo OOP type code would be ideal for this type of situation.

Develphant had a tutorial I believe a while back on some type of OOP method in a ‘RPG’ type game… it looked very good. If I remember correctly it was fairly short and very easy to understand and implement.  You can search goodle for that.  It might be even better answer to your situation.

I included in the top of this example code 2 utility type functions found on the corona forums that you may find helpful to use at different times when debugging. I included them, as they were needed in this example code.

Credit for the people who initially posted those are noted.

-- source : https://gist.github.com/nrk/31175 .. as posted by FoxB on the forum function print\_r ( t ) local print\_r\_cache={} local function sub\_print\_r(t,indent) if (print\_r\_cache[tostring(t)]) then print(indent.."\*"..tostring(t)) else print\_r\_cache[tostring(t)]=true if (type(t)=="table") then for pos,val in pairs(t) do if (type(val)=="table") then print(indent.."["..pos.."] =\> "..tostring(t).." {") sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8)) print(indent..string.rep(" ",string.len(pos)+6).."}") elseif (type(val)=="string") then print(indent.."["..pos..'] =\> "'..val..'"') else print(indent.."["..pos.."] =\> "..tostring(val)) end end else print(indent..tostring(t)) end end end if (type(t)=="table") then print(tostring(t).." {") sub\_print\_r(t," ") print("}") else sub\_print\_r(t," ") end print() end -- source : from mpappas on a forum post function deepCopy(object) local lookup\_table = {} local function \_copy(object) if type(object) ~= "table" then return object elseif lookup\_table[object] then return lookup\_table[object] end local new\_table = {} lookup\_table[object] = new\_table for index, value in pairs(object) do new\_table[\_copy(index)] = \_copy(value) end return setmetatable(new\_table, getmetatable(object)) end return \_copy(object) end local myData = { {name = "Blair", frame = 3, active = false, class = "Warrior", maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = nil, shieldEquip = nil, trinket1 = nil, trinket2 = nil, }, {name = "Lily", frame = 4, active = false, class = "Rogue", maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .16, weaponEquip = nil, shieldEquip = nil, trinket1 = nil, trinket2 = nil, }, {name = "Kylianna", frame = 2, active = false, class = "Mage", maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = nil, shieldEquip = nil, trinket1 = nil, trinket2 = nil, }, } local equipInventory = { [1] = {name = nil, frame = 4, description = "Weapon", class = "Warrior", strength = 5, armor = 0, plusHealth = 0, plusSpeed = 0, }, [2] = {name = nil, frame = 2, description = "Armor", class = "Warrior", strength = 0, armor = 5, plusHealth = 0, plusSpeed = 0, }, [3] = {name = nil, frame = 3, description = "Trinket", class = "Warrior", strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, } --METHOD 1 -- ASSIGN A POINTER TO THE WEAPONEQUIP TABLE INDEX myData[3].weaponEquip = 3 -- need to first make sure there is a valid value (not nil) in the weaponEquip field if myData[3].weaponEquip ~= nil then print(myData[3].name .. " is carrying " .. equipInventory[myData[3].weaponEquip].description) -- any change to the fields in the weaponEquip table will show when accessing them thru myData equipInventory[myData[3].weaponEquip].description = "change desc" print(myData[3].name .. " is carrying " .. equipInventory[myData[3].weaponEquip].description) end --METHOD 2 -- ASSIGN THE EQUIPINVENORY TABLE HANDLE TO THE FIELD WEAPONEQUIP myData[3].weaponEquip = equipInventory[2] if myData[3].weaponEquip ~= nil then print(myData[3].name .. " is carrying " .. myData[3].weaponEquip.description) -- same effect as Method 1 :any change to the fields in the weaponEquip table will show when accessing them thru myData equipInventory[2].description = "NEW DESC" print(myData[3].name .. " is carrying " .. myData[3].weaponEquip.description) end -- METHOD 3 -- OR INSERT A COPY OF WEAPON EQUIP TABLE IN THE MYDATA TABLE myData[3].weaponEquip = deepCopy(equipInventory[1]) if myData[3].weaponEquip ~= nil then print\_r(equipInventory[1]) print\_r(myData[3].weaponEquip) equipInventory[1].description = "broken weapon" --equipment table seesnew description as 'broken weapon' print\_r(equipInventory[1]) -- myData still sees it's weaponEquip description as the initial description "Weapon" print\_r(myData[3].weaponEquip) end

Good luck

Bob

Thanks Bob, interesting stuff you have posted, I’ll read into it some more, but it seems to make sense at first glance. Im on and off the computer today, so maybe tonight I can read into it in more detail and see if I come up with any specific questions.

Real quick, the first part of code you provided from github, is that just a debugger to show if anything is wrong with my tables while im stumbling along?

it is just a nice little function that prints out all the fields, sub tables, and their values of any table you pass into that function.  Quick easy way to print out onto your console to see if any values have changed in any particular table. There are various reasons you could use this, when debugging your code. As you code more and more lines of code, and you use more and more complex tables, you may find it helpful. 

Good Luck

Bob

Ethan,

Hope this info helps.  Made a few small suggestive changes that I hope you find make sense. 

  1. You can use the [1] = {  } for each table index, but that is not required.(maybe you already know that) but I took them out of the 

myData table just in case you were not aware of that.

  1. Just a suggestion :  fields like ‘active’ (i am guessing a flag that player is active or not active, I think are best not left nil) if it is a boolean type variable, mark it ‘false’ or ‘true’ … but again, maybe you already know this.  I normally try to avoid nil values for fields.

Also, there are a lot of ways to do what you are asking (if I understand your question correctly).

Here are just 3 ways I thought of fairly quickly.

These are a quick and direct answer to your question, although I think a modular or pseudo OOP type code would be ideal for this type of situation.

Develphant had a tutorial I believe a while back on some type of OOP method in a ‘RPG’ type game… it looked very good. If I remember correctly it was fairly short and very easy to understand and implement.  You can search goodle for that.  It might be even better answer to your situation.

I included in the top of this example code 2 utility type functions found on the corona forums that you may find helpful to use at different times when debugging. I included them, as they were needed in this example code.

Credit for the people who initially posted those are noted.

-- source : https://gist.github.com/nrk/31175 .. as posted by FoxB on the forum function print\_r ( t ) local print\_r\_cache={} local function sub\_print\_r(t,indent) if (print\_r\_cache[tostring(t)]) then print(indent.."\*"..tostring(t)) else print\_r\_cache[tostring(t)]=true if (type(t)=="table") then for pos,val in pairs(t) do if (type(val)=="table") then print(indent.."["..pos.."] =\> "..tostring(t).." {") sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8)) print(indent..string.rep(" ",string.len(pos)+6).."}") elseif (type(val)=="string") then print(indent.."["..pos..'] =\> "'..val..'"') else print(indent.."["..pos.."] =\> "..tostring(val)) end end else print(indent..tostring(t)) end end end if (type(t)=="table") then print(tostring(t).." {") sub\_print\_r(t," ") print("}") else sub\_print\_r(t," ") end print() end -- source : from mpappas on a forum post function deepCopy(object) local lookup\_table = {} local function \_copy(object) if type(object) ~= "table" then return object elseif lookup\_table[object] then return lookup\_table[object] end local new\_table = {} lookup\_table[object] = new\_table for index, value in pairs(object) do new\_table[\_copy(index)] = \_copy(value) end return setmetatable(new\_table, getmetatable(object)) end return \_copy(object) end local myData = { {name = "Blair", frame = 3, active = false, class = "Warrior", maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = nil, shieldEquip = nil, trinket1 = nil, trinket2 = nil, }, {name = "Lily", frame = 4, active = false, class = "Rogue", maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .16, weaponEquip = nil, shieldEquip = nil, trinket1 = nil, trinket2 = nil, }, {name = "Kylianna", frame = 2, active = false, class = "Mage", maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = nil, shieldEquip = nil, trinket1 = nil, trinket2 = nil, }, } local equipInventory = { [1] = {name = nil, frame = 4, description = "Weapon", class = "Warrior", strength = 5, armor = 0, plusHealth = 0, plusSpeed = 0, }, [2] = {name = nil, frame = 2, description = "Armor", class = "Warrior", strength = 0, armor = 5, plusHealth = 0, plusSpeed = 0, }, [3] = {name = nil, frame = 3, description = "Trinket", class = "Warrior", strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, } --METHOD 1 -- ASSIGN A POINTER TO THE WEAPONEQUIP TABLE INDEX myData[3].weaponEquip = 3 -- need to first make sure there is a valid value (not nil) in the weaponEquip field if myData[3].weaponEquip ~= nil then print(myData[3].name .. " is carrying " .. equipInventory[myData[3].weaponEquip].description) -- any change to the fields in the weaponEquip table will show when accessing them thru myData equipInventory[myData[3].weaponEquip].description = "change desc" print(myData[3].name .. " is carrying " .. equipInventory[myData[3].weaponEquip].description) end --METHOD 2 -- ASSIGN THE EQUIPINVENORY TABLE HANDLE TO THE FIELD WEAPONEQUIP myData[3].weaponEquip = equipInventory[2] if myData[3].weaponEquip ~= nil then print(myData[3].name .. " is carrying " .. myData[3].weaponEquip.description) -- same effect as Method 1 :any change to the fields in the weaponEquip table will show when accessing them thru myData equipInventory[2].description = "NEW DESC" print(myData[3].name .. " is carrying " .. myData[3].weaponEquip.description) end -- METHOD 3 -- OR INSERT A COPY OF WEAPON EQUIP TABLE IN THE MYDATA TABLE myData[3].weaponEquip = deepCopy(equipInventory[1]) if myData[3].weaponEquip ~= nil then print\_r(equipInventory[1]) print\_r(myData[3].weaponEquip) equipInventory[1].description = "broken weapon" --equipment table seesnew description as 'broken weapon' print\_r(equipInventory[1]) -- myData still sees it's weaponEquip description as the initial description "Weapon" print\_r(myData[3].weaponEquip) end

Good luck

Bob

Thanks Bob, interesting stuff you have posted, I’ll read into it some more, but it seems to make sense at first glance. Im on and off the computer today, so maybe tonight I can read into it in more detail and see if I come up with any specific questions.

Real quick, the first part of code you provided from github, is that just a debugger to show if anything is wrong with my tables while im stumbling along?

it is just a nice little function that prints out all the fields, sub tables, and their values of any table you pass into that function.  Quick easy way to print out onto your console to see if any values have changed in any particular table. There are various reasons you could use this, when debugging your code. As you code more and more lines of code, and you use more and more complex tables, you may find it helpful. 

Good Luck

Bob