How to change value in map table?

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

How to change value in map table?

Post by owowow »

Recently I started learning love2d and now I am making a collision map in tiles.

I want to change only index where some random sized boxes generated in the map table by loop. But somehow I can't get them changed correctly. I am quite a beginner and have no clear understang how tables work.. Could someone give me correct way to do it? currently I set mapsize width 480, height 320, and tilesize is 16.

Code: Select all

function generateBoxes()
    for i=1,5 do
        local x=math.random (0, mapsizex*tilesize)
        local y=math.random (0, mapsizey*tilesize)
        local width=math.random(1,6)
        local height=math.random(1,4)
        
        ChangeMap(math.floor(x/tilesize),math.floor(y/tilesize),width,height,1)
    end
    PrintMap()
end


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

function ChangeMap(x,y,w,h,num)
    print("x"..x.." y"..y.." w"..w.." h"..h)
    
    for i=y, h+1 do
        for j=x,w do 
            map[i][j]=num
        end
    end
    
end

function PrintMap()
    for i = 1, #map do
        local s = ''
        for j= 1, #map[i] do
            s = (s .. ' ' .. map[i][j])
        end
        print(s)
    end
end

User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: How to change value in map table?

Post by BrotSagtMist »

You forgot to include the relative position in the changing loop:
for i=y, h+1+y do
for j=x,w+x do
map[j]=num
end
obey
owowow
Prole
Posts: 7
Joined: Mon Aug 01, 2022 10:18 pm

Re: How to change value in map table?

Post by owowow »

Thank you! you are right,
such a dumb I was.. thank you very much.

So i went like this, it works as I wanted but it looks bit ugly..

Code: Select all

 for i=y, h+1+y-2 do
        for j=x,w+x-1 do
            map[i][j]=num
        end
   end
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to change value in map table?

Post by darkfrei »

owowow wrote: Tue Aug 02, 2022 5:51 am Thank you! you are right,
such a dumb I was.. thank you very much.

So i went like this, it works as I wanted but it looks bit ugly..

Code: Select all

 for i=y, h+1+y-2 do
        for j=x,w+x-1 do
            map[i][j]=num
        end
   end
Better?

Code: Select all

 for i=1, h do
        for j=1,w do
            map[i+y-1][j+x-1]=num
        end
   end
Actually you can have the first position as map[1][1] and not as map[0][0] and you don't need the "-1" _here_
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
owowow
Prole
Posts: 7
Joined: Mon Aug 01, 2022 10:18 pm

Re: How to change value in map table?

Post by owowow »

Thanks! truely thats better!

But now I ended like this,

Code: Select all

    for i=0, h-1 do
        for j=0,w-1 do
            if  (i+y)>0 and (j+x)>0 and (i+y)<=mapsizey and (j+x)<=mapsizex  then
                map[i+y][j+x]=num
            end
        end
   end


I also post here my whole sample text code(pls make conf to turn the console window on).
It would be appreciated if anyone can write to me a better code to do same thing.

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
    MakeNewMap()
    --generateBoxes()
end

function love.update(dt)

end






function generateBoxes()
     
     for i=1,5 do
            local x=math.random (0, mapsizex*tilesize)
            local y=math.random (0, mapsizey*tilesize)
            local width=math.random(1,6)
            local height=math.random(1,4)
            
            ChangeMap(math.floor(x/tilesize),math.floor(y/tilesize),width,height,1)
    end
    --PrintMap()
end


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

function ChangeMap(x,y,w,h,num)
    print("x"..x.." y"..y.." w"..w.." h"..h)

    for i=0, h-1 do
        for j=0,w-1 do
            if  (i+y)>0 and (j+x)>0 and (i+y)<=mapsizey and (j+x)<=mapsizex  then
                map[i+y][j+x]=num
            end
        end
   end

end

function PrintMap()
    for i = 1, #map do
        --print("#map:"..#map)
        local s = ""
        for j= 1, #map[i] do
            s = (s .. " " .. map[i][j])
        end
        print(s)
    end
    print(" ")
end

function love.keypressed(key)
    if key=="space" then
        PrintMap()
    end
    if key=="g" then
        generateBoxes()
        PrintMap()
    end
    if key=="c" then
        ChangeMap(3,2,3,2,1)
    end
    if key=="n" then
        MakeNewMap()
    end
    
end
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], eaaakb, Google [Bot], Semrush [Bot] and 5 guests