1

I am making a project with lua that gets a list of all the file names from your desktop in lua. however, I can't figure out how to do it, and I am also going to be using love2d for it, because it is going to be a game. can you tell me how to do it? thanks!

Here is the code

function love.load()
  require "player"

  -- Lets add Some Variables!
  -- Some Directory Suff first for Variables...
  DesktopDirectory = love.filesystem.getUserDirectory().."Desktop"
  DesktopFiles = love.filesystem.getDirectoryItems(DesktopDirectory)

  -- These are the Images!
  images = {
    background = love.graphics.newImage("gfx/desktop.png")
  }

  players = {Player.New(50, 300, 40, 40, "gfx/stickman.png", true)}

  love.graphics.setBackgroundColor(100, 220, 255)

  for k in pairs(DesktopFiles) do
    print(DesktopFiles[k])
  end
end

function love.keypressed(k)
  if k == "j" then
    players[1].jump()
  end
end

function love.update(dt)
  for i in pairs(players) do
    players[i].update()
  end
end

function love.draw()
  love.graphics.draw(images.background)

  for i in pairs(players) do
    players[i].draw()
  end
end
  • what do you mean with "access the files from your desktop"? do you just want a list of those files? – Piglet Apr 30 '17 at 09:47
  • pretty much yes... I meant to say "get a list of all the file names from your desktop"... let me rephrase that in the question! – BukkitmanPlays MCPE Apr 30 '17 at 19:53
  • there is a lot of information online on how to get file lists in lua. read this for example: http://stackoverflow.com/questions/5303174/how-to-get-list-of-directories-in-lua – Piglet Apr 30 '17 at 20:03

1 Answers1

3

Love2D (tries to) sandbox file system access, you're not supposed to touch anything outside your game's code or the "save directory" (where anything that you write goes). In particular, the Desktop folder is also out of reach. If you try to use love.filesystem.getDirectoryItems on that path, you'll just get an empty table. Same for other functions – they'll just refuse to work.

The easiest way to get that functionality is to include lfs and use its functions together with Lua's base io library for general file system access.

nobody
  • 4,074
  • 1
  • 23
  • 33