Hello - I’ve been using Corona on trial for about 20 days, and I like a lot about it. I was making very good progress, then blew out a tire.
I’ve successfully broken out my code into “classes”. Yes, I have read the various libraries that allow for some OOP in Corona, but I decided to stick with metatables. The OOP part is not the problem. The problem is “vanishing scope”. I have pulled back my classes so that they are all instantiated in my main file to avoid variables suddenly becoming ‘nil’. At the bottom of “main” you can see that I’ve frantically been trying to get the multi-dim table to output something. It loses all its data when I try and load one of my objects to a local variable, or rather - it has a ghosted value from the last one that loaded…
MAIN.LUA:
require "sqlite3"
require "region"
--HIDE THE STATUS BAR
display.setStatusBar(display.HiddenStatusBar)
--ICONS ON TABS
local MAX\_X = 10
local MAX\_Y = 1
regions = {}
--SHOW THE BACKGROUND
local background = display.newImage ("art/MapEurope1939.png", true)
--TABS AT BOTTOM
local tabReport = display.newImage ("art/tabReport.png", true)
local tabAction = display.newImage ("art/tabAction.png", true)
for arrayCounter = 0, MAX\_X do
arrayCounter = arrayCounter + 1
regions[arrayCounter] = {}
end
function loadRegions()
--load prototypes and vars
unitClass = require "unit"
regionClass = require "region"
local path = system.pathForFile("soi", system.ResourceDirectory )
--acquire data for the first X row
file = io.open( path, "r" )
db = sqlite3.open( path )
for arrayCounter = 0, MAX\_X do
--run for the number of Rows there are.
arrayCounter = arrayCounter + 1
--regions[arrayCounter] = {} -debug array
local sqlRegions = "SELECT \* FROM regions where mapLocX = "..arrayCounter.." order by mapLocY"
-- print (sqlRegions) --debug
arrayCounterY = 0
for recordRegion in db:nrows(sqlRegions) do
arrayCounterY = arrayCounterY + 1
local region = regionClass:new(recordRegion.control,
recordRegion.hasUnits,
recordRegion.infrastructure,
recordRegion.startingImage,
recordRegion.mapLocX,
recordRegion.mapLocY,
recordRegion.pk,
recordRegion.name,
recordRegion.terrain,
recordRegion.coastal
)
--load all units and add to region
local sqlUnits = "SELECT \* FROM units where units.fk = "..recordRegion.pk
for recordUnit in db:nrows(sqlUnits) do
if recordUnit ~= nil then
local unit = unitClass:new(recordUnit.pk,
recordUnit.fk,
recordUnit.name,
recordUnit.unitType,
recordUnit.supply,
recordUnit.experience,
recordUnit.morale,
recordUnit.strength
)
--add unit to region
region:addUnit(unit)
end
end
table.insert(regions[arrayCounter],region)
-- region = regions[arrayCounter][arrayCounterY] --debug
-- print("Array Location X: "..arrayCounter.." | Y: "..arrayCounterY.." | PK: "..region.pk) --debug --debug
--
-- --print(#regions)
-- print(#regions[arrayCounter])
end
end
end
function printRegions()
for x = 0, MAX\_X do
x = x + 1
for y = 0, MAX\_Y do
y = y + 1
-- region = {}
-- region = regions[x][y] --debug
-- print("Array Location X: "..x.." | Y: "..y.." | PK: "..regionClass.pk) --debug --debug
-- --region:printUnitData()
end
end
end
for x = 0, MAX\_X do
x = x + 1
for y = 0, MAX\_Y do
y = y + 1
region = regions[x][y] --debug
print("Array Location X: "..x.." | Y: "..y.." | PK: "..region.pk) --debug --debug
end
end
loadRegions()
printRegions() --debug
Here is my region code, which contains ‘units’:
local Region = {
mt = {},
icon = display.newImage ("art/InvisibleIconWithX.png", true),
control = "None",
hasUnits = 0,
infrastructure = 0,
pk = 0,
name = "None",
terrain = "None",
coastal = 0,
units = {}
}
function Region:new(p\_control, p\_hasUnits, p\_infrastructure, p\_startingImage, p\_x, p\_y,
p\_pk, p\_name, p\_terrain, p\_coastal)
self.control = p\_control
self.hasUnits = p\_hasUnits
self.infrastructure = p\_infrastructure
self.startingImage = p\_startingImage
self.pk = p\_pk
self.name = p\_name
self.terrain = p\_terrain
self.coastal = p\_coastal
self.icon = display.newImage ("art/"..p\_startingImage, true)
self.icon.x = (p\_x \* 64) - 32
self.icon.y = (p\_y \* 64) - 32
self.icon.isVisible = true
units = {}
return setmetatable({}, self.mt)
end
function Region:addUnit(unit)
table.insert(units, unit)
end
function Region:printUnitData()
--used for troubleshooting or dump
print("Region: "..self.pk.."| Number of Units: "..#units)
--walk through all units and output all their fields.
for i = 0, #units do
unit = units[i]
if unit ~= nil then
print("--------------------------")
print("Unit Data for pk: "..unit.pk)
print("fk: "..unit.fk)
print("name: "..unit.name)
print("unitType: "..unit.unitType)
print("supply: "..unit.supply)
print("experience: "..unit.morale)
print("strength: "..unit.strength)
print("--------------------------")
end
end
end
function Region:printRegionData()
--used for troubleshooting or dump
print("--------------------------")
print("Region: "..self.pk)
print("--------------------------")
end
Region.mt.\_\_index = Region
return Region
Here is my unit code, which is really just a struct:
local Unit = {
mt = {},
pk = 0,
fk = 0,
name = "None",
unitType = "None",
supply = 0,
experience = 0,
morale = 0,
strength = 0
}
function Unit:new(p\_pk, p\_fk, p\_name, p\_unitType, p\_supply, p\_experience, p\_morale, p\_strength)
self.pk = p\_pk
self.fk = p\_fk
self.name = p\_name
self.unitType = p\_unitType
self.supply = p\_supply
self.experience = p\_experience
self.morale = p\_morale
self.strength = p\_strength
-- print(self.name) --debug
return setmetatable({}, self.mt)
end
Unit.mt.\_\_index = Unit
return Unit
my goal is just to be able to review and output region member variable values in my main.lua code…is that too much to ask!!!
Thanks in advance…
Richard [import]uid: 117222 topic_id: 21237 reply_id: 321237[/import]