[SOLVED] Mapping

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.
Todespreis
Prole
Posts: 40
Joined: Sat Apr 01, 2023 9:30 pm

[SOLVED] Mapping

Post by Todespreis »

Hey there! I want to make my code a bit smaller? I'm trying to map the first dungeon in my game or just to map anything for the first time.
So the code

Code: Select all

function love.load()

tilemap = {
        
        {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
        {16, 16, 16, 16, 16, 16, 11, 12, 13, 12, 13, 12, 13, 12, 13, 15},
        {16, 16, 16, 16, 16, 16, 21, 22, 23, 22, 23, 22, 23, 22, 23, 25},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {11, 12, 13, 12, 13, 12, 25, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {21, 22, 23, 22, 23, 22, 25, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
        {31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
        {31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
        {51, 52, 53, 52, 53, 15, 32, 33, 32, 33, 32, 33, 11, 52, 53, 55},
        {16, 16, 16, 16, 16, 31, 32, 33, 32, 33, 32, 33, 35, 16, 16, 16},
        {16, 16, 16, 16, 16, 31, 32, 33, 32, 33, 32, 33, 35, 16, 16, 16},
        {16, 16, 16, 16, 16, 51, 52, 53, 54, 52, 53, 54, 55, 16, 16, 16}
        
        
    }

    tile_11 = love.graphics.newImage("/DungeonTiles01/11.png")
    tile_12 = love.graphics.newImage("/DungeonTiles01/12.png")
    tile_13 = love.graphics.newImage("/DungeonTiles01/13.png")
    tile_15 = love.graphics.newImage("/DungeonTiles01/15.png")
    tile_21 = love.graphics.newImage("/DungeonTiles01/21.png")
    tile_22 = love.graphics.newImage("/DungeonTiles01/22.png")
    tile_23 = love.graphics.newImage("/DungeonTiles01/23.png")
    
function love.draw()

     for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            
            if tile == 11 then
             
                love.graphics.draw( tile_11, j * width, i * height)   
               
            
            elseif tile == 12 then 

                love.graphics.draw( tile_12, j * width, i * height) 


            elseif tile == 13 then

                love.graphics.draw( tile_13, j * width, i * height)

            elseif tile == 15 then

                love.graphics.draw( tile_15, j * width, i * height)                    

            elseif tile == 21 then

                love.graphics.draw( tile_21, j * width, i * height)

            elseif tile == 22 then

                love.graphics.draw( tile_22, j * width, i * height)
            

            elseif tile == 23 then

                love.graphics.draw( tile_23, j * width, i * height)

            end
        end
     end
end
is working. The problem is, i have no desire to assign every single integer in my array to a .png. It is taking like forever :death:

So i wrote that:

Code: Select all

 tiles = { }

    for i = 1, num_tiles do

        tiles[i] = love.graphics.newImage("/DungeonTiles01/" .. string.format("%02d", i) .. ".png")

 end
 
function love.draw()

     for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            
            love.graphics.draw(tiles[tile], j * width, i * height)           

        end
     end
end 
The problem is, the interpreter starts counting by 01. And the first integer in my array is 11 (first line, first row), then a 12 (first line, second row) and so on. How do i tell the interpreter: "just look up in the damn tilemap - array and if there is no 01, so stop searching for it!" ? :neko: :ultraglee: :rofl:
Last edited by Todespreis on Sun Apr 21, 2024 5:54 pm, edited 1 time in total.
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Mapping

Post by keharriso »

If I understand correctly, you're trying to load images for tiles that don't exist, like 01.png

To solve this, you can iterate over your tilemap to pick out the tiles that are actually used:

Code: Select all

    tiles = {}
    for _, row in ipairs(tilemap) do
        for _, tile in ipairs(row) do
            if tiles[tile] == nil then
                tiles[tile] = love.graphics.newImage("/DungeonTiles01/" .. string.format("%02d", tile) .. ".png")
            end
        end
    end
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
Todespreis
Prole
Posts: 40
Joined: Sat Apr 01, 2023 9:30 pm

Re: Mapping

Post by Todespreis »

Exactly! But your code is'nt working. I get the same error ^^
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Mapping

Post by keharriso »

Todespreis wrote: Sun Apr 21, 2024 4:25 pm Exactly! But your code is'nt working. I get the same error ^^
Hello, would you mind sharing the error you get?
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
Todespreis
Prole
Posts: 40
Joined: Sat Apr 01, 2023 9:30 pm

Re: Mapping

Post by Todespreis »

"Could not open file /DungeonTiles01/01.png does not exist." Like i mentioned before, my tiles starting at 11.png
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Mapping

Post by keharriso »

If you share your code as a .love file I can hopefully find exactly where your problem is.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
Todespreis
Prole
Posts: 40
Joined: Sat Apr 01, 2023 9:30 pm

Re: Mapping

Post by Todespreis »

Sure, here is it:

Code: Select all

local player

local direction

local ground

local tilemap

local num_tiles

local tiles



function love.load()
    
    FrameDuration = 0.08
    FrameTimer = 0
    FrameIndex = 1
    FramesWalkingDown =    {
       love.graphics.newImage("/Character_Sprites/going_down_01.png"),
       love.graphics.newImage("/Character_Sprites/going_down_02.png"),
       love.graphics.newImage("/Character_Sprites/going_down_03.png"),
       love.graphics.newImage("/Character_Sprites/going_down_04.png"),
       love.graphics.newImage("/Character_Sprites/going_down_05.png"),
       love.graphics.newImage("/Character_Sprites/going_down_06.png")
    }
    FramesWalkingUp = {
       love.graphics.newImage("/Character_Sprites/going_up01.png"),
       love.graphics.newImage("/Character_Sprites/going_up02.png"),
       love.graphics.newImage("/Character_Sprites/going_up03.png"),
       love.graphics.newImage("/Character_Sprites/going_up04.png"),
       love.graphics.newImage("/Character_Sprites/going_up05.png"),
       love.graphics.newImage("/Character_Sprites/going_up06.png")
    }
    FramesWalkingRight = {
      love.graphics.newImage("/Character_Sprites/going_right01.png"),
      love.graphics.newImage("/Character_Sprites/going_right02.png"),
      love.graphics.newImage("/Character_Sprites/going_right03.png"),
      love.graphics.newImage("/Character_Sprites/going_right04.png"),
      love.graphics.newImage("/Character_Sprites/going_right05.png"),
      love.graphics.newImage("/Character_Sprites/going_right06.png")
    }

    FramesWalkingLeft = {
      love.graphics.newImage("/Character_Sprites/going_left01.png"),
      love.graphics.newImage("/Character_Sprites/going_left02.png"),
      love.graphics.newImage("/Character_Sprites/going_left03.png"),
      love.graphics.newImage("/Character_Sprites/going_left04.png"),
      love.graphics.newImage("/Character_Sprites/going_left05.png"),
      love.graphics.newImage("/Character_Sprites/going_left06.png")
    }
  

    FrameEnabledDown = FramesWalkingDown[FrameIndex]
    FrameEnabledUp = FramesWalkingUp[FrameIndex]
    FrameEnabledRight = FramesWalkingRight[FrameIndex]
    FrameEnabledLeft = FramesWalkingLeft[FrameIndex]


    tilemap = {
        
        {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
        {16, 16, 16, 16, 16, 16, 11, 12, 13, 12, 13, 12, 13, 12, 13, 15},
        {16, 16, 16, 16, 16, 16, 21, 22, 23, 22, 23, 22, 23, 22, 23, 25},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {11, 12, 13, 12, 13, 12, 25, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {21, 22, 23, 22, 23, 22, 25, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
        {31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
        {31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
        {51, 52, 53, 52, 53, 15, 32, 33, 32, 33, 32, 33, 11, 52, 53, 55},
        {16, 16, 16, 16, 16, 31, 32, 33, 32, 33, 32, 33, 35, 16, 16, 16},
        {16, 16, 16, 16, 16, 31, 32, 33, 32, 33, 32, 33, 35, 16, 16, 16},
        {16, 16, 16, 16, 16, 51, 52, 53, 54, 52, 53, 54, 55, 16, 16, 16}
        
        
    }

    num_tiles = 60

    tiles = {}

    for i = 1, num_tiles do

        tiles[i] = love.graphics.newImage("/DungeonTiles01/" .. string.format("%02d", i) .. ".png")

    end

    
   

    width = 16

    height = 16

    player = {}
    player.x = 70
    player.y = 70

    player.image = love.graphics.newImage("/Character_Sprites/standing_down.png" )

    


end


Down={}

function love.joystickpressed( joystick, button )

  Down[button]=true

end

function love.joystickreleased( joystick, button )

  Down[button]=nil

end

love.update=function (dt)

    FrameTimer = FrameTimer + dt
    if FrameTimer >= FrameDuration then
        FrameTimer = 0
        FrameIndex = FrameIndex + 1
        
        if FrameIndex > #FramesWalkingDown then
            FrameIndex = 1
        end

        if FrameIndex > #FramesWalkingUp then
            FrameIndex = 1
        end

        if FrameIndex > #FramesWalkingLeft then
            FrameIndex = 1
        end

        if FrameIndex > #FramesWalkingRight then
            FrameIndex = 1
        end
        
        if Down[06] then 
            player.x = player.x + 4
            FrameEnabledRight = FramesWalkingRight[FrameIndex]
        end

        if Down[04] then 
            player.y = player.y + 4
            FrameEnabledDown = FramesWalkingDown[FrameIndex]
        end

        if Down[02] then 
            player.x = player.x - 4
            FrameEnabledLeft = FramesWalkingLeft[FrameIndex]
        end

        if Down[00] then 
            player.y = player.y - 4
            FrameEnabledUp = FramesWalkingUp[FrameIndex]
        end
    end

    

end



function love.draw()

     for _, row in ipairs(tilemap) do
        for _, tile in ipairs(row) do
            if tiles[tile] ~= nil then
                tiles[tile] = love.graphics.newImage("/DungeonTiles01/" .. string.format("%02d", tile) .. ".png")
            end
        end
    end

    if Down[06] then
    
      love.graphics.draw( FrameEnabledRight, player.x, player.y  )
    
    elseif Down[04] then 
   
      love.graphics.draw( FrameEnabledDown, player.x, player.y  ) 
    
    elseif Down[02] then
    
      love.graphics.draw( FrameEnabledLeft, player.x, player.y  )
    
    elseif Down[00] then
      
      love.graphics.draw( FrameEnabledUp, player.x, player.y  )

    else 
            love.graphics.draw( player.image, player.x, player.y  )
    end
end
   
Todespreis
Prole
Posts: 40
Joined: Sat Apr 01, 2023 9:30 pm

Re: Mapping

Post by Todespreis »

Oh, love file, sorry.
Spiel1_mitTiles.love
(121.49 KiB) Downloaded 40 times
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Mapping

Post by keharriso »

This code here, in love.load:

Code: Select all

    num_tiles = 60
    tiles = {}
    for i = 1, num_tiles do
        tiles[i] = love.graphics.newImage("/DungeonTiles01/" .. string.format("%02d", i) .. ".png")
    end
should be replaced with this code (which you currently have in love.draw):

Code: Select all

     tiles = {}
     for _, row in ipairs(tilemap) do
        for _, tile in ipairs(row) do
            if tiles[tile] == nil then
                tiles[tile] = love.graphics.newImage("/DungeonTiles01/" .. string.format("%02d", tile) .. ".png")
            end
        end
    end
Note the "if tiles[tile] == nil" instead of "if tiles[tile] ~= nil".
Last edited by keharriso on Sun Apr 21, 2024 5:53 pm, edited 1 time in total.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
Todespreis
Prole
Posts: 40
Joined: Sat Apr 01, 2023 9:30 pm

Re: Mapping

Post by Todespreis »

Oh, sorry. I thought, you want me to change it in the love.draw ^^
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 3 guests