Page 1 of 2

Random Generation map error on Mobile

Posted: Tue Aug 16, 2022 4:47 pm
by owowow
I was making trees that generated in the tiled map randomly by using table.
My code works fine on my PC but when I run it on my Android it crashes when its checking tiles to place new tree.
I suspected the problem was because of difference of screen ratio or something and tried some size changes but didn't help any.

I attach my example code and error appears like this on mobile.
Please let me know if someone know what the problem is!


Code: Select all

function love.load()
    math.randomseed (os.clock ())
    screensizex=love.graphics.getWidth()
    screensizey=love.graphics.getHeight()
    tilesize=16
    map={}
    mapsizex = screensizex/tilesize     --30 tiles
    mapsizey = screensizey/tilesize     --20tiles
    TreeNums=50
    objects={}
    trees={}
    MakeNewMap()

    generateTrees()
end


function love.draw()

    if trees then
        for i,v in ipairs(trees) do
        love.graphics.rectangle("fill",v.x,v.y,v.w,v.h)
        end
    end

end



function generateTrees()
  
    for i=1,TreeNums do
        local tilex=math.random (1, mapsizex)
        local tiley=math.random (1, mapsizey)
        --x=clamp(0,x,screensizex)
        --y=clamp(0,y,screensizey)
        print("random x:"..tilex.." random y:"..tiley)
        if CheckTileByTilesize(tilex,tiley,0) then
            trees[i]={
                x=(tilex*tilesize)-tilesize,
                y=(tiley*tilesize)-tilesize,
                w=tilesize,
                h=tilesize
            }
            --tree[i]:SetCollision(true) --set circle collision
            table.insert(objects, #objects + 1, tree[i])
            map[tiley][tilex]=2
            
        end
    end
end




function MakeNewMap()
    for y = 1,mapsizey do
        map[y] = {}
        for x = 1,mapsizex do
            map[y][x]= 0
        end
        
    end
end



function CheckTileByTilesize(x,y,num)
    if map[y][x] == num then
        return true
    else
        return false
    end
end


function GetTilePos(x,y)

    local tilex=math.floor((x+tilesize)/tilesize)
    local tiley=math.floor((y+tilesize)/tilesize)
    tilex=clamp(1,tilex,mapsizex)
    tiley=clamp(1,tiley,mapsizey)
    local pos={x=tilex,y=tiley}
    return pos
end


function PrintMap()
    for i = 1, #map do
      
        local s = ""
        for j= 1, #map[i] do
            s = (s .. " " .. map[i][j])
        end
        print(s.." "..i)
    end
    for k= 1, #map do
        local m = ""
        for l= 1, #map[k] do
             m = (m.. " "..l)
        end
        if k==#map then print(m) end
    end
    
    print(" ")
end

function love.keypressed(key)
    if key=="space" then
        PrintMap()
    end
    if key=="g" then
        MakeNewMap()
        generateTrees()
        PrintMap()
    end

    if key=="n" then
        MakeNewMap()
    end
    
end

Code: Select all

Error

main.lua:69: attempt to index a nil value


Traceback

main.lua:69: in function 'CheckTileByTilesize'
main.lua:38: in function 'generateTrees'
main.lua:14: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'

Re: Random Generation map error on Mobile

Posted: Tue Aug 16, 2022 4:52 pm
by darkfrei
Please make the resolution independent code:

Code: Select all

screensizex=love.graphics.getWidth()
screensizey=love.graphics.getHeight()
mapsizex = 30
mapsizey = 20
tilesize = math.floor(math.min (screensizex/mapsizex, screensizey/mapsizey))
Also use table.insert (trees, tree) instead of trees[ i ] = tree: it finds the last i of the sequence and make +1 without errors.

Re: Random Generation map error on Mobile

Posted: Tue Aug 16, 2022 4:54 pm
by GVovkiv
Also, it's, probably, better to use love.math.random, and not lua's math.random

Re: Random Generation map error on Mobile

Posted: Tue Aug 16, 2022 4:58 pm
by 1414codeforge
Hi, in these lines:

Code: Select all

    mapsizex = screensizex/tilesize     --30 tiles
    mapsizey = screensizey/tilesize     --20tiles
You are not using integer division.
I wonder if you're experiencing some weird floating point rounding approximation error in your code that cascades all the way to those for loops, and, in turn, create a mess when you index the map table.
Have you tried using math.floor() on that?

Re: Random Generation map error on Mobile

Posted: Tue Aug 16, 2022 5:00 pm
by darkfrei
GVovkiv wrote: Tue Aug 16, 2022 4:54 pm Also, it's, probably, better to use love.math.random, and not lua's math.random
No, math.random is better, but by the testing use the same seed, you don't need to get the error sometimes.

Re: Random Generation map error on Mobile

Posted: Tue Aug 16, 2022 6:30 pm
by milon
darkfrei wrote: Tue Aug 16, 2022 5:00 pm No, math.random is better ...
How so? My impression is the exact opposite (although they're both generally fine depending on application).

Re: Random Generation map error on Mobile

Posted: Tue Aug 16, 2022 6:30 pm
by owowow
@darkfrei, Omg it was so quick and true!! It worked!! so it looks like the problem was the fixed tilesize! Thank you so much!

btw, i changed it like this. sorry that previously I forgot to change the part of trees and tree..

and i will change and try with math.random to love.math.random!

Code: Select all

function love.load()
    math.randomseed (os.clock ())
    screensizex=love.graphics.getWidth()
    screensizey=love.graphics.getHeight()
    mapsizex = math.floor(screensizex/16)
    mapsizey = math.floor(screensizey/16)
    tilesize = math.floor(math.min (screensizex/mapsizex, screensizey/mapsizey))
    map={}
    
    TreeNums=50
    --objects={} didnt need
    --trees={} moved in the generate function
    MakeNewMap()

    generateTrees()
end


function love.draw()

    if trees then
        for i,v in ipairs(trees) do
        love.graphics.rectangle("fill",v.x,v.y,v.w,v.h)
        end
    end

end



function generateTrees()
    tree={}
    trees={}
    for i=1,TreeNums do
        local tilex=math.random (1, mapsizex)
        local tiley=math.random (1, mapsizey)
        --x=clamp(0,x,screensizex)
        --y=clamp(0,y,screensizey)
        print("random x:"..tilex.." random y:"..tiley)
        if CheckTileByTilesize(tilex,tiley,0) then
            tree={
                x=(tilex*tilesize)-tilesize,
                y=(tiley*tilesize)-tilesize,
                w=tilesize,
                h=tilesize
            }

            table.insert(trees,tree)
            map[tiley][tilex]=2
            
        end
    end
end




function MakeNewMap()
    love.graphics.setColor(0,0,0,1)
        love.graphics.rectangle("fill",0,0,screensizex,screensizey)
        love.graphics.setColor(0,1,0,1)

    for y = 1,mapsizey do
        map[y] = {}
        for x = 1,mapsizex do
            map[y][x]= 0
        end
        
    end
end



function CheckTileByTilesize(x,y,num)
    if map[y][x] == num then
        return true
    else
        return false
    end
end


function GetTilePos(x,y)

    local tilex=math.floor((x+tilesize)/tilesize)
    local tiley=math.floor((y+tilesize)/tilesize)
    tilex=clamp(1,tilex,mapsizex)
    tiley=clamp(1,tiley,mapsizey)
    local pos={x=tilex,y=tiley}
    return pos
end


function PrintMap()
    for i = 1, #map do
      
        local s = ""
        for j= 1, #map[i] do
            s = (s .. " " .. map[i][j])
        end
        print(s.." "..i)
    end
    for k= 1, #map do
        local m = ""
        for l= 1, #map[k] do
             m = (m.. " "..l)
        end
        if k==#map then print(m) end
    end
    
    print(" ")
end

function love.keypressed(key)
    if key=="space" then
        PrintMap()
    end
    if key=="g" then
        
        MakeNewMap()
        generateTrees()
        PrintMap()
    end

    if key=="n" then
        MakeNewMap()
    end
    
end

Re: Random Generation map error on Mobile

Posted: Tue Aug 16, 2022 6:47 pm
by owowow
And anyone knows if thats better use love.window.getSafeArea( ) instead of love.graphics.getWidth()/Height()?

Code: Select all

	safex, safey, safew, safeh = love.window.getSafeArea( )
    screensizex=safew
    screensizey=safeh

Re: Random Generation map error on Mobile

Posted: Tue Aug 16, 2022 7:01 pm
by darkfrei
It must be as easy as

Code: Select all

-- License CC0 (Creative Commons license) (c) darkfrei, 2022

--love.window.setMode(1920, 1080, {resizable=true, borderless=false})
love.window.setMode(240, 320) -- QVGA, but vertical

function love.load()
	safeX, safeY, safeW, safeH = love.window.getSafeArea( )
end

function love.draw()
	love.graphics.translate(safeX, safeY)
	love.graphics.rectangle("line", 0, 0, safeW, safeH)
	love.graphics.line(0, 0, safeW, safeH)
	love.graphics.line(0, safeH, safeW, 0)
	love.graphics.circle ('line', safeW/2, safeH/2, safeW/2)
	love.graphics.circle ('line', safeW/2, safeH/2, safeH/2)
end


function love.mousepressed( x, y, button, istouch, presses )
	love.window.setMode(240, 320) -- QVGA, but vertical

	safeX, safeY, safeW, safeH = love.window.getSafeArea( )
end
safe-area-03.love
(482 Bytes) Downloaded 89 times
OR double the setMode

Code: Select all

function love.load()
	--love.window.setMode(1920, 1080, {resizable=true, borderless=false})
	love.window.setMode(240, 320) -- QVGA, but vertical
	love.window.setMode(240, 320) -- double it!
--	love.timer.sleep(1)
	safeX, safeY, safeW, safeH = love.window.getSafeArea( )
end

function love.draw()
	love.graphics.translate(safeX, safeY) -- or minus, not tested
	love.graphics.rectangle("line", 0, 0, safeW, safeH)
	love.graphics.line(0, 0, safeW, safeH)
	love.graphics.line(0, safeH, safeW, 0)
	love.graphics.circle ('line', safeW/2, safeH/2, safeW/2)
	love.graphics.circle ('line', safeW/2, safeH/2, safeH/2)
end
safe-area-06.love
(459 Bytes) Downloaded 88 times

Re: Random Generation map error on Mobile

Posted: Wed Aug 17, 2022 9:42 am
by Gunroar:Cannon()
darkfrei wrote: Tue Aug 16, 2022 5:00 pm
GVovkiv wrote: Tue Aug 16, 2022 4:54 pm Also, it's, probably, better to use love.math.random, and not lua's math.random
No, math.random is better, but by the testing use the same seed, you don't need to get the error sometimes.
Woah, why?