Create Object With Array Table - lua
I try to make several panel objects on my form in Cheat Engine using CE Lua script. How do this in the correct way?.
local bricks = {}
local brickWidth = 70
local brickHeight = 25
local brickRows = 6
local brickColumns = 6
local rleft = 5
local rtop = 5
local cleft = 5
local ctop = 10
for row = 0, brickRows do
for column = 0, brickColumns do
bricks[row] = createPanel(gameMain)
bricks[row].Width = brickWidth
bricks[row].Height = brickHeight
bricks[row].Top = rtop
bricks[row].Left = rleft
bricks[row].Color = math.random(10,65255)
rleft = rleft + brickWidth + 5
bricks[column] = createPanel(gameMain)
bricks[column].Width = brickWidth
bricks[column].Height = brickHeight
bricks[column].Left = cleft
bricks[column].Top = brickHeight + 5
bricks[column].Color = math.random(10,65255)
ctop = ctop + brickHeight + 5
end
end
But it fails. What I want is for each row and column will contain 6 panels.
How to write the correct script?. Thank you
Create a table that will contain all bricks.
Create 1 table per row
Create and add 1 brick per column into each row
Simply use the loop counters to calculate the offsets.
Maybe you should solve such problems with pen and paper first.
local rows, cols = 6, 6
local width, height = 70, 25
local gap = 5
local bricks = {}
for row = 1, rows do
bricks[row] = {}
for col = 1, cols do
local x = (col - 1) * (width + gap) -- x offset
local y = (row - 1) * (height + gap) -- y offset
local newBrick = createPanel(gameMain)
-- assign brick's properties
-- ...
bricks[row][col] = newBrick
end
end
Related
bmp aspect ratio issue
I've been trying to understand how bmp files work so I can render some Mandelbrot set pictures and output them as bmp files since that seems to be one of the easiest methods but for some reason when I use an aspect ratio that isn't 1:1 even though its something to the power of 4 (so no padding is needed) I get weird artifacts like these 200:100 48:100 what I'm trying to do is turning an array of pixels that has white for even numbers and black for odd numbers into a bmp, this (100:100) is what it looks like with 1:1 aspect ratio. I've tried reading through the wikipedia article to see if I can figure out what I'm doing wrong but I still don't get what I'm missing. This is the script I've written in Lua so far: ResolutionX = 100 ResolutionY = 100 local cos, atan, sin, atan2, sqrt, floor = math.cos, math.atan, math.sin, math.atan2, math.sqrt, math.floor local insert, concat = table.insert, table.concat local sub, char, rep = string.sub, string.char, string.rep io.output("Test.bmp") function Basen(n,b) n = floor(n) if not b or b == 10 then return tostring(n) end local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" local t = {} repeat local d = (n % b) + 1 n = floor(n / b) insert(t, 1, digits:sub(d,d)) until n == 0 return rep("0",32-#t)..concat(t,"") end FileSize = Basen(ResolutionY*ResolutionX*3 + 54,2) FileSize4 = tonumber(sub(FileSize,1,8),2) or 0 FileSize3 = tonumber(sub(FileSize,9,16),2) or 0 FileSize2 = tonumber(sub(FileSize,17,24),2) or 0 FileSize1 = tonumber(sub(FileSize,25,32),2) or 0 Width = Basen(ResolutionX,2) print("Width: ",Width) Width4 = tonumber(sub(Width,1,8),2) or 0 Width3 = tonumber(sub(Width,9,16),2) or 0 Width2 = tonumber(sub(Width,17,24),2) or 0 Width1 = tonumber(sub(Width,25,32),2) or 0 Height = Basen(ResolutionY,2) print("Height: ",Height) Height4 = tonumber(sub(Height,1,8),2) or 0 Height3 = tonumber(sub(Height,9,16),2) or 0 Height2 = tonumber(sub(Height,17,24),2) or 0 Height1 = tonumber(sub(Height,25,32),2) or 0 BMPSize = Basen(ResolutionY*ResolutionX*3,2) BMPSize4 = tonumber(sub(BMPSize,1,8),2) or 0 BMPSize3 = tonumber(sub(BMPSize,9,16),2) or 0 BMPSize2 = tonumber(sub(BMPSize,17,24),2) or 0 BMPSize1 = tonumber(sub(BMPSize,25,32),2) or 0 print("TotalSize: ",FileSize1,FileSize2,FileSize3,FileSize4,"\nWidth: ",Width1,Width2,Width3,Width4,"\nHeight: ",Height1,Height2,Height3,Height4,"\nImage data: ",BMPSize1,BMPSize2,BMPSize3,BMPSize4) Result = {"BM"..char( --File type FileSize1,FileSize2,FileSize3,FileSize4,--File size 0,0,0,0, --Reserved 54,0,0,0, --Where the pixel data starts 40,0,0,0, --DIB header Width1,Width2,Width3,Width4, --Width Height1,Height2,Height3,Height4, --Height 1,0, --Color planes 24,00, --Bit depth 0,0,0,0, --Compression BMPSize1,BMPSize2,BMPSize3,BMPSize4, --The amount of bytes pixel data will consume Width1,Width2,Width3,Width4, Height1,Height2,Height3,Height4, 0,0,0,0, --Number of colors in palatte 0,0,0,0 )} for X = 0, ResolutionX - 1 do for Y = 0, ResolutionY - 1 do insert(Result,rep(char(255 * ((X + 1) % 2) * ((Y + 1) % 2)),3)) end end io.write(table.concat(Result))
Ok, here's a BMP version. I've put things in a module so it might be easier to use. local writeBMP = {} local floor = math.floor local insert, concat = table.insert, table.concat local sub, char, rep = string.sub, string.char, string.rep local function Basen(n,b) n = floor(n) if not b or b == 10 then return tostring(n) end local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" local t = {} repeat local d = (n % b) + 1 n = floor(n / b) insert(t, 1, digits:sub(d,d)) until n == 0 return rep("0",32-#t)..concat(t,"") end local function nextMul4(x) if ( x % 4 == 0 ) then return x else return x+4-(x%4) end end local function clamp(x) local y = x if ( x > 255 ) then y = 255 elseif ( x < 0 ) then y = 0 end return floor(y) end -- Accepts array of type pixelsXYC[X][Y][C] of numbers 0-255 -- C=1,2,3 are the red, green and blue channels respectively -- X increases left to right, and Y increases top to bottom function writeBMP.data(pixelsXYC, resolutionX, resolutionY) local Pixels = pixelsXYC local ResolutionX = resolutionX local ResolutionY = resolutionY assert(#Pixels == ResolutionX, "Table size and X resolution mismatch") assert(#Pixels[1] == ResolutionY, "Table size and Y resolution mismatch") local FileSize = Basen(ResolutionY*nextMul4(3*ResolutionX) + 54,2) local FileSize4 = tonumber(sub(FileSize,1,8),2) or 0 local FileSize3 = tonumber(sub(FileSize,9,16),2) or 0 local FileSize2 = tonumber(sub(FileSize,17,24),2) or 0 local FileSize1 = tonumber(sub(FileSize,25,32),2) or 0 local Width = Basen(ResolutionX,2) local Width4 = tonumber(sub(Width,1,8),2) or 0 local Width3 = tonumber(sub(Width,9,16),2) or 0 local Width2 = tonumber(sub(Width,17,24),2) or 0 local Width1 = tonumber(sub(Width,25,32),2) or 0 local Height = Basen(ResolutionY,2) local Height4 = tonumber(sub(Height,1,8),2) or 0 local Height3 = tonumber(sub(Height,9,16),2) or 0 local Height2 = tonumber(sub(Height,17,24),2) or 0 local Height1 = tonumber(sub(Height,25,32),2) or 0 local BMPSize = Basen(ResolutionY*nextMul4(3*ResolutionX),2) local BMPSize4 = tonumber(sub(BMPSize,1,8),2) or 0 local BMPSize3 = tonumber(sub(BMPSize,9,16),2) or 0 local BMPSize2 = tonumber(sub(BMPSize,17,24),2) or 0 local BMPSize1 = tonumber(sub(BMPSize,25,32),2) or 0 local Result = {} Result[1] = "BM" .. char( --File type FileSize1,FileSize2,FileSize3,FileSize4,--File size 0,0, --Reserved 0,0, --Reserved 54,0,0,0, --Where the pixel data starts 40,0,0,0, --DIB header Width1,Width2,Width3,Width4, --Width Height1,Height2,Height3,Height4, --Height 1,0, --Color planes 24,0, --Bit depth 0,0,0,0, --Compression BMPSize1,BMPSize2,BMPSize3,BMPSize4, --The amount of bytes pixel data will consume 37,22,0,0, --Pixels per meter horizontal 37,22,0,0, --Pixels per meter vertical 0,0,0,0, --Number of colors in palatte 0,0,0,0 ) local Y = ResolutionY while( Y >= 1 ) do for X = 1, ResolutionX do local r = clamp( Pixels[X][Y][1] ) local g = clamp( Pixels[X][Y][2] ) local b = clamp( Pixels[X][Y][3] ) Result[#Result+1] = char(b) Result[#Result+1] = char(g) Result[#Result+1] = char(r) end -- byte alignment if ( ( (3*ResolutionX) % 4 ) ~= 0 ) then local Padding = 4 - ((3*ResolutionX) % 4) Result[#Result+1] = rep(char(0),Padding) end Y = Y - 1 end return table.concat(Result) end function writeBMP.write(pixelsXYC, resolutionX, resolutionY, filename) local file = io.open(filename,"wb") local data = writeBMP.data(pixelsXYC, resolutionX, resolutionY) file:write(data) end return writeBMP A simple test: -- writeBMP example local writeBMP = require "writeBMP" local resolutionX = 100 local resolutionY = 100 -- Pixel data local pixels = {} for x=1,resolutionX do pixels[x] = {} for y=1, resolutionY do pixels[x][y] = {} local red = 255*(resolutionX-x+resolutionY-y)/(resolutionX+resolutionY) local green = 255*y/resolutionY local blue = 255*x/resolutionX pixels[x][y][1] = red pixels[x][y][2] = green pixels[x][y][3] = blue end end writeBMP.write(pixels,resolutionX,resolutionY,"testwritebmp.bmp") return Note: In BMP, the Y axis starts on the bottom. I am more used to Y axis going from top down in computer graphics (so I wrote it that way). Thanks HAX for the code.
Welcome to Stack Exchange :) I suggest having a look at PPM files, they are easy. They can be converted with other tools to png or bmp with other tools. Wikipedia PPM Specification Here is a PPM solution: ResolutionX = 48 ResolutionY = 100 local cos, atan, sin, atan2, sqrt, floor = math.cos, math.atan, math.sin, math.atan2, math.sqrt, math.floor local insert, concat = table.insert, table.concat local sub, char, rep = string.sub, string.char, string.rep local file = io.open("Test.ppm","w") -- PPM File headers local Output = { } Output[1] = "P3" Output[2] = tostring(ResolutionX) .. " " .. tostring(ResolutionY) Output[3] = "255" -- Image body for Y = 0, ResolutionY - 1 do for X = 0, ResolutionX - 1 do local value = 255 * ((X + 1) % 2) * ((Y + 1) % 2) Output[#Output+1] = rep(tostring(floor(value)),3," ") end end -- Join lines together local Result = table.concat(Output,"\n") file:write(Result) Notes: I could not get your code to write to file (see my usage of file). The inner loop must be X (columns) and the outer loop must be Y (rows) if the write order is English reading order (left-right down-up).
Check Collision Between Ball And Array Of Bricks
Since I have a collision check function in Lua: function onCollision(obj1,obj2) obj1x = obj1.left obj1y = obj1.top obj1w = obj1.width obj1h = obj1.height obj2x = obj2.left obj2y = obj2.top obj2w = obj2.width obj2h = obj2.height if obj2x + obj2w >= obj1x and obj2y + obj2h >= obj1y and obj2y <= obj1y + obj1h and obj2x <= obj1x + obj1w then return true end end And I did make some panels as bricks using array table on my form, with this function, I did collision check between ball with each form sides without problems. function createBricks() for row = 1, brickRows do bricks[row] = {} for col = 1, brickColumns do local x = (col - 1) * (width + gap) -- x offset local y = (row - 1) * (height + gap) -- y offset local newBrick = createPanel(gamePanel) newBrick.width = brickWidth newBrick.height = brickHeight newBrick.top = y + 15 newBrick.left = x + 15 newBrick.BorderStyle = 'bsNone' if level == 1 then newBrick.color = '65407' -- green elseif level == 2 then newBrick.color = '858083' -- red elseif level == 3 then newBrick.color = '9125192' -- brown elseif level == 4 then newBrick.color = math.random(8,65255) end bricks[row][col] = newBrick end end end Next how to detect if the ball collided with the bricks?. So far I did: for row = 1, brickRows do bricks[row] = {} for col = 1, brickColumns do dBrick = bricks[row][col] if onCollision(gameBall,dBrick) then dBrick.destroy() -- destroy the collided brick end end end I want to learn how to implement this collision logic in VB Net script which VB script easier for me, I did the whole game project using VB Net, now I try to re-write the project using CE Lua. Private brickArray(brickRows, brickColumns) As Rectangle Private isBrickEnabled(brickRows, brickColumns) As Boolean For rows As Integer = 0 To brickRows For columns As Integer = 0 To brickColumns If Not isBrickEnabled(rows, columns) Then Continue For If gameBall.IntersectsWith(brickArray(rows, columns)) Then isBrickEnabled(rows, columns) = False If gameBall.X + 10 < brickArray(rows, columns).X Or _ gameBall.X > brickArray(rows, columns).X + brickArray(rows, columns).Width _ Then xVel = -xVel Else yVel = -yVel End If End If Next Next And also this private sub, how to write it CE Lua? Sub loadBricks() Dim xOffset As Integer = 75, yOffset As Integer = 100 For row As Integer = 0 To brickRows For column As Integer = 0 To brickColumns brickArray(row, column) = New Rectangle(xOffset, yOffset, brickWidth, brickHeight) xOffset += brickWidth + 10 isBrickEnabled(row, column) = True Next yOffset += brickHeight + 10 xOffset = 75 Next End Sub Function getBrickCount() As Integer Dim Count As Integer = 0 For Each brick As Boolean In isBrickEnabled If brick = True Then Count += 1 Next Return Count End Function
function onCollision(obj1,obj2) obj1x = obj1.left obj1y = obj1.top obj1w = obj1.width obj1h = obj1.height obj2x = obj2.left obj2y = obj2.top obj2w = obj2.width obj2h = obj2.height if obj2x + obj2w >= obj1x and obj2y + obj2h >= obj1y and obj2y <= obj1y + obj1h and obj2x <= obj1x + obj1w then return true end end Then to detect collision the collision and remove from the table: -- Drawback table local function tcount( t ) local c = 0 for k,v in pairs(t) do c = c + 1 end return c end local count = #brickArray for x = 1, count do if onCollision(gameBall, brickArray[x]) then if gameBall.Left + 10 < brickArray[x].Left or gameBall.Left > brickArray[x].Left + brickArray[x].Width then xVel = -xVel else yVel = -yVel end playSound(findTableFile('strikeball.wav')) brickArray[x] = brickArray[count] brickArray[x] = x brickArray[count] = nil brickArray[x].Visible = false tcount(brickArray) end end The code above detected the collision and remove the object from the table, but that is not removed from display. How to remove the bricks from the table and display, using Cheat Engine Lua script?.
Add score using score module
I'm working on a game, collecting points by pressing on images. Pressing images and respawning new ones is no problem but adding points to the score causes some problems. When I press on the images the score doesn't get updated with the amount of points. I'm using this score module http://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/ local score_mod = require( "score" ) math.randomseed( os.time() ) local scoreText = score_mod.init({ fontSize = 70, font = native.systemFont, x = display.contentCenterX + 50, y = display.contentCenterY + 170, filename = "scorefile.txt" }) scoreText:setTextColor( (232/255), (216/255), (32/255) ) local function Game () local images ={ {name = "Icon.png", points = 1}, {name = "Icon-mdpi.png", points = 2}, {name = "Icon-xhdpi.png", points = 3}, {name = "Icon-ldpi.png", points = 4}, {name = "Icon-Small-50.png", points = 5} } local numRows = 3 local numCols = 2 local blockWidth = display.contentCenterX / 2.2 local blockHeight = display.contentCenterY / 2 local row local col local imgDataArray = {} function imagePress (event) if event.phase == "began" then local x = event.target.x local y = event.target.y event.target:removeSelf() score_mod.score = score_mod.score + images[event.target.imgNumber].points function nextImages(x, y) local nextRandomImg = math.random(1,5) local nextImage = display.newImage(images[nextRandomImg].name, x, y) nextImage:addEventListener( "touch", imagePress ) nextImage.imgNumber = nextRandomImg table.insert(imgDataArray, image) end local nextDelay = function() return nextImages(x, y) end timer.performWithDelay( 2000, nextDelay, 1 ) end return true end function makeImage() for row = 1, numRows do for col = 1, numCols do local x = (col - 1) * blockWidth + 120 local y = (row + 1) * blockHeight - 160 local randomImage = math.random(1, 5) local image = display.newImage(images[randomImage].name, x, y) image.imgNumber = randomImage image.imgX = x image.imgY = y image:addEventListener( "touch", imagePress ) table.insert(imgDataArray, image) end end end makeImage() end Game() Many thanks!
Use score_mod.add(images[event.target.imgNumber].points) instead of score_mod.score = score_mod.score + images[event.target.imgNumber].points
attempt to index upvalue 'letter' error because or arr_str[i]
Im trying to create a random letter answer for my game but the problem is that it wont detect the images please help... local s = answer --answer local len = string.len(answer) local str local j for j=1,len do str = s:sub(j,j) arr_str[j]= str end for i = 1, #arr_str do local j = math.random( 1,#arr_str) arr_str[i], arr_str[j] = arr_str[j], arr_str[i] end local max = #arr_str local rowMax = max --if max > 8 then rowMax = 8 end local gap = 35 local xPos = halfW - (rowMax/2*35) + 17.5 local yPos = bottom - 20 local startpos = xPos for i= 1, #arr_str do slot = display.newImageRect( "images/bg_slot.png", 43, 43 ) slot.x = xPos; slot.y = yPos -40 slotgroup:insert( slot ) slot1 = display.newImageRect( "images/ans_slot.png", 43, 43 ) slot1.x = xPos; slot1.y = yPos slotgroup:insert( slot1) print("ERROR",arr_str[i]) letter = display.newImageRect("letters/uc/".. arr_str[i] ..".png",50,50) letter.name = arr_str[i] letter.x = xPos; letter.y = yPos letter.idBg = i lettergroup:insert( letter) xPos = xPos + gap
Probably the image that you are trying to open does not exist. Print the whole path of the image (code below) and look for it on your project folder to make sure you really have that image (pay attention to the file extension as well. print("letters/uc/".. arr_str[i] ..".png")
Cannot change value of object in lua
i have a table(array) where i keep the references of some images. Here is the code: local rowcount = 8 local colcount = 4 local blockWidth = display.contentWidth / (colcount*4) local blockHeight = display.contentWidth / (rowcount*2) local row local col local pan = 3 local i=0 for row = 1, rowcount do for col = 1, colcount do local x = (col - 1) * blockWidth + pan + 30 local y = (row + 1) * blockHeight + pan local random= math.random(1,6) random = revisarTres(i, random) local image = display.newImage(images[random], 0, 0) image.x = x image.y = y print (x) print (y) image.value = random image:addEventListener("touch", blockTouch) block[i] = image i=i+1 end end I keep the references in block of my 31 images. Then i make a transition for one image to another, conversely. And i want to change the value, the coordenates and the reference of this two. I'm making this: block[old].value, block[new].value = block[new].value, block[old].value block[old].x, block[new].x = block[new].x, block[old].x block[old].y, block[new].y = block[new].y, block[old].y block[old], block[new] = block[new], block[old] Old is the position of one of the images i want to change and new of the other one. The reference is changing but the value doesn't. Please can someone help me, Thanks!
One small note (addition?): if You ever find a table, where You can't directly change values - check if it's not readonly one. More about this kind of technique using Lua metatables: lua-users Wiki - Read Only Tables