Page 1 of 1

main.lua can't find a json file, but only when run as .love file

Posted: Tue Apr 11, 2023 11:07 am
by trashtiergames
Hi all!

I have a finished game called snaketime. It's nothing special. It works when I run it on my Macbook from the command line using this:

Code: Select all

love .
But there is an error when I drag the folder on top of the love2d, and when I try to make a zip file as per these instructions: https://love2d.org/forums/viewtopic.php?f=4&t=451

This is the error:

Code: Select all

Error

main.lua:91: attempt to index local 'ldtkFile' (a nil value)


Traceback

[love "callbacks.lua"]:228: in function 'handler'
main.lua:91: in function 'load'
[love "callbacks.lua"]:136: in function <[love "callbacks.lua"]:135>
[C]: in function 'xpcall'
[C]: in function 'xpcall'
This "ldtk file" is a .json file that is created by LDtk, a tile editor like Tiled.

These are the relevant parts of my main.lua:

Code: Select all

Class = require "libraries/class"
push = require "libraries/push"
json = require "libraries/json"
bump = require "libraries/bump"
anim8 = require "libraries/anim8"

function love.load()
  math.randomseed(os.time())

  local ldtkFile = io.open("game-3.ldtk", "r") [this is line 91]
  local ldtkJson = ldtkFile:read("a")
  ldtkFile:close()
  ldtk = json.decode(ldtkJson)
The full source code can be found on my github repo:
https://github.com/trashtiergames/snaketime

What I've figured out is that the same error occurs when I run the following from the parent directory:

Code: Select all

love snaketime
Changing the line to this will fix that issue:

Code: Select all

local ldtkFile = io.open("snaketime/game-3.ldtk", "r")
However, the issue still persists with the .love file. So somehow io.open doesn't get the right path to the .ldtk file that is in the same folder.

I have tried to get the full path to the directory with this method:

Code: Select all

local current_dir = os.getenv("PWD")
local ldtkPath = current_dir .. "/snaketime/game-3.ldtk"
local ldtkFile = io.open(ldtkPath, "r")
This worked when calling via command line using "love snaketime" or "love ." from within the folder (with the string that's added in the second line above is changed accordingly). It still did not work when making a .love file or when dragging the folder on top of the love2d app.

I have also tried the following to get the path:

Code: Select all

current_dir = io.popen"cd":read'*l'
(returns nil)

Code: Select all

current_dir = os.execute("cd")
(returns 0)

Code: Select all

current_dir = debug.getinfo(1).short_src
(returns "main.lua")

Code: Select all

current_dir = debug.getinfo(1).source
(returns "@main.lua")

Code: Select all

current_dir = io.popen("cd"):read()
(returns nil)
None of these worked. Either they resulted in the same error or caused new ones. I put the return values in brackets.

These are from this stackoverflow thread:
https://stackoverflow.com/questions/603 ... ory-in-lua

Someone there also recommends installing a library, but I'm not sure if that means it won't work if I send the finished file to friends or let people download it.

So basically I only want to package my game and let other people play it who don't know what lua and love2d is and who can't use the command line. But this error is preventing me from doing that. Can anyone help me? :)

Re: main.lua can't find a json file, but only when run as .love file

Posted: Tue Apr 11, 2023 11:15 am
by slime
io.open doesn't know how to look inside zip files and also has platform-specific paths, you should use love.filesystem.read instead.

Re: main.lua can't find a json file, but only when run as .love file

Posted: Wed Apr 12, 2023 5:45 pm
by zorg
Let me add that you should also use dots in the require function instead of slashes, since those are module names, not paths. It works for the moment, but it's accidental and will be "fixed" come the next major löve release.

(I'd also mention to make sure the casing of paths and files is correct, but that usually only happens with windows, not with any other OS due to windows not caring by default about whether something is lower or uppercase.)

Re: main.lua can't find a json file, but only when run as .love file

Posted: Tue Apr 18, 2023 5:10 am
by knorke
zorg wrote: Wed Apr 12, 2023 5:45 pm Let me add that you should also use dots in the require function instead of slashes, since those are module names, not paths. It works for the moment, but it's accidental and will be "fixed" come the next major löve release.
Can you please explain this more?

Code: Select all

bump = require "libraries/bump"
This requires a file named "bump.lua" which is in a subfolder "libraries", yes?
Why is that not a path? What should it look like instead?

Re: main.lua can't find a json file, but only when run as .love file

Posted: Tue Apr 18, 2023 9:56 am
by darkfrei
Use the point as "folder.subfolder.filename"
http://lua-users.org/wiki/SampleCode

Re: main.lua can't find a json file, but only when run as .love file

Posted: Thu Apr 20, 2023 11:59 am
by zorg
knorke wrote: Tue Apr 18, 2023 5:10 am
zorg wrote: Wed Apr 12, 2023 5:45 pm Let me add that you should also use dots in the require function instead of slashes, since those are module names, not paths. It works for the moment, but it's accidental and will be "fixed" come the next major löve release.
Can you please explain this more?

Code: Select all

bump = require "libraries/bump"
This requires a file named "bump.lua" which is in a subfolder "libraries", yes?
Why is that not a path? What should it look like instead?
Sorry for the late reply, wanted to do it a few days ago, i just forgot.

No, it will match "libraries/bump" to the package.path contents, which is full of predefined patterns; if there's a match, it'll use it.
but what you give the function is not a path (unlike with most löve constructors and love.filesystem stuff), so you would use dots as separators, not slashes.