Random Generation map error on Mobile

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
owowow
Prole
Posts: 7
Joined: Mon Aug 01, 2022 10:18 pm

Random Generation map error on Mobile

Post 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'
Attachments
main.lua
(2.42 KiB) Downloaded 18 times
conf.lua
(101 Bytes) Downloaded 15 times
User avatar
darkfrei
Party member
Posts: 737
Joined: Sat Feb 08, 2020 11:09 pm

Re: Random Generation map error on Mobile

Post 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.
Last edited by darkfrei on Tue Aug 16, 2022 4:56 pm, edited 3 times in total.
User avatar
GVovkiv
Party member
Posts: 399
Joined: Fri Jan 15, 2021 7:29 am
Contact:

Re: Random Generation map error on Mobile

Post by GVovkiv »

Also, it's, probably, better to use love.math.random, and not lua's math.random
User avatar
1414codeforge
Prole
Posts: 9
Joined: Fri Aug 05, 2022 1:14 pm
Location: Italy, Sardinia
Contact:

Re: Random Generation map error on Mobile

Post 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?
User avatar
darkfrei
Party member
Posts: 737
Joined: Sat Feb 08, 2020 11:09 pm

Re: Random Generation map error on Mobile

Post 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.
milon
Party member
Posts: 402
Joined: Thu Jan 18, 2018 9:14 pm

Re: Random Generation map error on Mobile

Post 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).
owowow
Prole
Posts: 7
Joined: Mon Aug 01, 2022 10:18 pm

Re: Random Generation map error on Mobile

Post 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
owowow
Prole
Posts: 7
Joined: Mon Aug 01, 2022 10:18 pm

Re: Random Generation map error on Mobile

Post 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
User avatar
darkfrei
Party member
Posts: 737
Joined: Sat Feb 08, 2020 11:09 pm

Re: Random Generation map error on Mobile

Post 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 16 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 17 times
Attachments
Click to adjust
Click to adjust
Screenshot_20220817-190617.jpg (101.94 KiB) Viewed 487 times
Last edited by darkfrei on Wed Aug 17, 2022 6:42 pm, edited 10 times in total.
User avatar
Gunroar:Cannon()
Party member
Posts: 954
Joined: Thu Dec 10, 2020 1:57 am

Re: Random Generation map error on Mobile

Post 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?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 15 guests