main.lua can't find a json file, but only when run as .love file
Posted: Tue Apr 11, 2023 11:07 am
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:
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:
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:
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:
Changing the line to this will fix that issue:
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:
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:
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?
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 .
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'
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)
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
Code: Select all
local ldtkFile = io.open("snaketime/game-3.ldtk", "r")
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")
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)
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?