Can't Get Files of Desktop with LOVE2D

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
BukkitmanMC
Prole
Posts: 10
Joined: Sat Apr 29, 2017 4:06 am

Can't Get Files of Desktop with LOVE2D

Post by BukkitmanMC »

Hey There! I am new to the LOVE Forums even though I have programmed with this Library before, so I am pretty used to it :crazy: ! However, Right now, I am working on a game using this framework, and it supposed to access the files of your Desktop! however every string says "nil" in the files!. Also, when I ask it to print out an entire table, it only shows some stuff I don't understand like: "table: 0x03059638" :x ! can you figure out why it does that? if yes, thanks!

The Code of the only file that really has something, since I can't figure out how to add a file:

Code: Select all

function love.load()
  require "player" -- Not Really Anything Right Now

  -- 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(10, 10, 10, 10, "gfx/stickman.png", true)}

  print(DesktopFiles)
end

function love.update(dt)

end

function love.draw()
  love.graphics.draw(images.background)
end
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Can't Get Files of Desktop with LOVE2D

Post by pedrosgali »

You are trying to print a table, that's why it says 'tableX0...'. Try

Code: Select all

for k in pairs(desktopFiles) do
  print(desktopFiles[k])
end
Don't think love can read from your desktop though without using FFI. I've not used it before, maybe someone else can help with that. :)

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Can't Get Files of Desktop with LOVE2D

Post by zorg »

Löve has three ways to do filesystem stuff:
- love.filesystem functions, which are the recommended way, support utf-8 encoding, work the same on all OS-es, and can only access the game's own directory and the save directory that's set with your project's identity.
- the lua io functions, which can access any folder and file anywhere on your computer, but it doesn't really guarantee utf-8 support on all OS-es. (windows)
- Advanced stuff: Using the FFI to call the PhysFS functions löve uses directly, in a way to circumvent their "allowed folder" limitations, akin to how file and folder dropping works.

For starters, i'd recommend forgetting every other folder apart from the save folder and the game's own, and using the love.filedropped and love.directorydropped functions to "support" files elsewhere, like on the desktop.

As for tables, you need to print their contents, as pedrosgali said above, otherwise, you'll print the reference's memory location, which is pretty much useless unless you want to compare if two variables point to the same table.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
BukkitmanMC
Prole
Posts: 10
Joined: Sat Apr 29, 2017 4:06 am

Re: Can't Get Files of Desktop with LOVE2D

Post by BukkitmanMC »

zorg wrote: Sat Apr 29, 2017 12:32 pm Löve has three ways to do filesystem stuff:
- love.filesystem functions, which are the recommended way, support utf-8 encoding, work the same on all OS-es, and can only access the game's own directory and the save directory that's set with your project's identity.
- the lua io functions, which can access any folder and file anywhere on your computer, but it doesn't really guarantee utf-8 support on all OS-es. (windows)
- Advanced stuff: Using the FFI to call the PhysFS functions löve uses directly, in a way to circumvent their "allowed folder" limitations, akin to how file and folder dropping works.

For starters, i'd recommend forgetting every other folder apart from the save folder and the game's own, and using the love.filedropped and love.directorydropped functions to "support" files elsewhere, like on the desktop.

As for tables, you need to print their contents, as pedrosgali said above, otherwise, you'll print the reference's memory location, which is pretty much useless unless you want to compare if two variables point to the same table.
Thanks! I didn't know about that before! so this will definitely be useful! guess I'll be trying to figure out how to use lua's io functions later!

Also,
pedrosgali wrote: Sat Apr 29, 2017 10:08 am You are trying to print a table, that's why it says 'tableX0...'. Try

Code: Select all

for k in pairs(desktopFiles) do
  print(desktopFiles[k])
end
Don't think love can read from your desktop though without using FFI. I've not used it before, maybe someone else can help with that. :)

Thanks for the help, but for some reason, the file names won't show up. does it have to do with the directory it is looking at?

EDIT: Thanks For The Info! :ultraglee:
BukkitmanMC
Prole
Posts: 10
Joined: Sat Apr 29, 2017 4:06 am

Re: Can't Get Files of Desktop with LOVE2D

Post by BukkitmanMC »

zorg wrote: Sat Apr 29, 2017 12:32 pm Löve has three ways to do filesystem stuff:
- love.filesystem functions, which are the recommended way, support utf-8 encoding, work the same on all OS-es, and can only access the game's own directory and the save directory that's set with your project's identity.
- the lua io functions, which can access any folder and file anywhere on your computer, but it doesn't really guarantee utf-8 support on all OS-es. (windows)
- Advanced stuff: Using the FFI to call the PhysFS functions löve uses directly, in a way to circumvent their "allowed folder" limitations, akin to how file and folder dropping works.

For starters, i'd recommend forgetting every other folder apart from the save folder and the game's own, and using the love.filedropped and love.directorydropped functions to "support" files elsewhere, like on the desktop.

As for tables, you need to print their contents, as pedrosgali said above, otherwise, you'll print the reference's memory location, which is pretty much useless unless you want to compare if two variables point to the same table.
Last of all, if I were to use love.directoryDropped(), how would I use it to get the file names? I am guessing it uses love.filesystem.mount, but how would I convert it into file names in an array?
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Can't Get Files of Desktop with LOVE2D

Post by zorg »

'UniqueName' is whatever you want to call the mounted directory's root inside löve.

Code: Select all

function love.directorydropped( path )
love.filesystem.mount(path, 'UniqueName')
files = love.filesystem.getDirectoryItems('UniqueName')
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
BukkitmanMC
Prole
Posts: 10
Joined: Sat Apr 29, 2017 4:06 am

Re: Can't Get Files of Desktop with LOVE2D

Post by BukkitmanMC »

zorg wrote: Tue May 23, 2017 9:11 am 'UniqueName' is whatever you want to call the mounted directory's root inside löve.

Code: Select all

function love.directorydropped( path )
love.filesystem.mount(path, 'UniqueName')
files = love.filesystem.getDirectoryItems('UniqueName')
end
Ah! Thanks! With this, I was able to get that part to work! When I get the Beta Build of the Game Complete, I might as well show the game to you all!
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests