Below is embedded the code I am querying about. So overall the code design in “upgrade.lua” is designed so I am able to easily create new images in “upgrade.lua” without making any more functions. Its also done this way so I don’t have to create another database.
I have likely made a ‘logic’ error related to the SQLite database and a change would make everything much easier. However, I would greatly appreciate any feedback on how to improve this code.
utils.lua (settings) -
--tables utils.settings = {--id, name, value, cost, + table num, image, x offset, y offset { 1, "balance" , 0, 0 , 0, "", }, { 2, "gamercoin" , 0, 0 , 0, "", }, { 3, "clickervalue" , 1, 0 , 0, "", }, { 4, "autoclicker" , 1, 0 , 0, "", }, { 5, "monitor\_level", 4, 0 , 0, "", }, { 6, "buymouse" , 1, -20, 1, "mouse1", 100, 75 }, --make sure the value increases by two each time }
utils.lua (database) -
--updating stuff for row in db:nrows( "SELECT \* FROM database" ) do if ( row.variable\_name == utils.settings[row.id][2] ) then utils.settings[row.id][3] = row.value utils.settings[row.id][4] = row.upgrade end end --the ONLY PUBLIC FUNCTION TO GO here function utils.update\_database( id, variable\_name, value, upgrade ) local temp = [[DELETE FROM database WHERE id =]] .. id .. [[;]] db:exec( temp ) local temp = [[INSERT INTO database VALUES (]] .. id .. [[, "]] .. variable\_name .. [[",]] .. value .. [[,]] .. upgrade .. [[);]] db:exec ( temp ) end
upgrade.lua -
local objects = {} local function increase\_ugrades( event ) if ( event.phase == "ended" ) then for i = 1, #utils.settings do if ( event.target.string == utils.settings[i][2] ) then if ( utils.settings[1][3] + utils.settings[i][4] \>= 0 ) then if ( objects[utils.settings[i][5]].alpha == 1 ) then utils.update\_balance( utils.settings[i][4] ) if ( utils.settings[i][6] ~= "" and utils.settings[i][3] \< 2 ) then utils.settings[i][3] = 2 elseif ( utils.settings[i][6] == "" ) then utils.settings[i][3] = utils.settings[i][3] + 1 end if ( utils.settings[i][6] == "" ) then utils.settings[i][4] = utils.settings[i][4] \* 2 end if ( utils.settings[i][6] ~= "" ) then objects[utils.settings[i][5]].alpha = 0.5 objects[utils.settings[i][5] + 1].alpha = 0.5 end objects[utils.settings[i][5] + 1].text = utils.settings[i][2] .. ": " .. ( -1 \* utils.settings[i][4] ) utils.update\_database( utils.settings[i][1], utils.settings[i][2], utils.settings[i][3], utils.settings[i][4] ) end end end end end end for i = 1, #utils.settings do if ( utils.settings[i][4] ~= 0 ) then objects[utils.settings[i][5] + 1] = display.newText( utils.settings[i][2] .. ": " .. ( -1 \* utils.settings[i][4] ), center\_x, center\_y + 50, font, 17) sceneGroup:insert( objects[utils.settings[i][5] + 1] ) objects[utils.settings[i][5]] = display.newImage( utils.settings[i][2] .. ".png", center\_x, center\_y ) sceneGroup:insert( objects[utils.settings[i][5]] ) objects[utils.settings[i][5]]:scale( 0.4, 0.4 ) if ( utils.settings[i][3] == 2 ) then objects[utils.settings[i][5]].alpha = 0.5 objects[utils.settings[i][5] + 1].alpha = 0.5 end objects[utils.settings[i][5]].string = utils.settings[i][2] objects[utils.settings[i][5]]:addEventListener( "touch", increase\_ugrades ) end end