How to use LDtk and :setImageTable for a map visual?

,

Alright, I don't know if I'm just missing something obvious or what, but here's what's wrong

(System: Windows Playdate SDK)

So, I was fiddling around with the LDtk library (GitHub - NicMagnier/PlaydateLDtkImporter: Load tilemaps created with LDtk in your playdate games) and trying to get a map to load with a visual using the map data, but couldn't get it working for some reason. I feel like I've tried everything, from using different functions, the level system, and more, but couldn't get it working. Right now, I've just put in the data manually, but this isn't feasible for bigger maps.
Am I missing something obvious?

import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"

import "player"

local pd <const> = playdate
local gfx <const> = playdate.graphics

import '../toyboxes/toyboxes'

import "LDtk"

local world
local tilemap = gfx.tilemap:new()
local tilesheet = gfx.imagetable.new("Images/tiles")

local function game_initialize()
    playerInstance = Player(100,100)
    playerInstance:add()
    
    LDtk.load( "MyWorld.ldtk" )
    assert("MyWorld.ldtk", "myw dne")
    world = LDtk.create_tilemap("Level_0")
    assert(world, "does not exist")
    
    demoinfo = {0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1}
    tilemap:setImageTable(tilesheet)
    tilemap:setTiles(demoinfo, 8)  -- Assuming 32x32 tiles; adjust based on your setup
    
    -- Add wall sprites using LDtk data
    local emptyTileIDs = LDtk.get_empty_tileIDs("MyWorld", "Walls")
    gfx.sprite.addWallSprites(world, emptyTileIDs)
    print(world)
    -- Set the tilemap data using the world loaded from LDtk


   
end

game_initialize()

function pd.update()
    gfx.sprite.update()
    pd.timer.updateTimers()
    tilemap:draw(0,0)
end

As you can see, I'm manually loading the data using demoinfo, and I'm pretty sure that's not the way to do it. I tried using LDtk.create_tilemap, but couldn't get it working considering it just outputted a string that couldn't be used when I tried it.

Sorry if this is a "let me google it for you" question, as I couldn't find anything and it's been driving me crazy

Hi,
I would look at some of SquidGod`s tutorials on YouTube and GitHub.

For example I`m using something like this:

ldtk.load("levels/oxydian.ldtk", false)

function GameScene:goToLevel(levelName)
   
    for layerName, layer in pairs(ldtk.get_layers(levelName)) do
        if layer.tiles then
            local tilemap = ldtk.create_tilemap(levelName, layerName)

            local layerSprite = gfx.sprite.new()
                
            layerSprite:setTilemap(tilemap)
            layerSprite:setCenter(0, 0)
            layerSprite:moveTo(0, 0)
            layerSprite:setZIndex(layer.zIndex)
            layerSprite:add()

			for enum, tag in pairs(TAGS) do
				local emptyTiles = ldtk.get_empty_tileIDs(levelName, enum, layerName)
				if emptyTiles then
					local tileSprites = gfx.sprite.addWallSprites(tilemap, emptyTiles)
					for i=1,#tileSprites do
						local tileSprite = tileSprites[i]
						tileSprite:setTag(tag)
					end
				end
			end
        end
    end
end
1 Like