Page 1 of 1

how do i get files outside folder?

Posted: Thu Jul 04, 2024 8:13 pm
by VXDev
i need help with this so i have a script in a folder named "scripts" and i have another folder named "assets"
heres how my game folders work:
so inside the scripts folder the script named "player.lua" and from there im trying to get the assets from the scripts folder in my scripts please help im a newbie
|game/
assets/
images/
myfile.png
scripts/
player.lua
------------
main.lua

Re: how do i get files outside folder?

Posted: Thu Jul 04, 2024 8:18 pm
by NoreoAlles
you have to put this line on the top of your main.lua, or whatever file should get acces to the player assets:

Code: Select all

require "yourFolderNameHere/filename.lua
replace your folder namer with the folder you want to open up a file in or just the filename without folder and slash if your file is in the same folder as your main.lua
In your case propably

Code: Select all

require "scripts/player.lua"

Re: how do i get files outside folder?

Posted: Fri Jul 05, 2024 5:49 am
by togFox

Code: Select all

require "scripts.player"
Use a dot/period. From the wiki: https://www.love2d.org/wiki/require
It's strongly recommended to use period as directory separator! Forward slashes accidently work, hence its usage is discouraged! One of the reasons is that loading modules that are not Lua code files is only possible with dots as separator, and using dots and slashes at the same time might lead to mixing separators, and due to mixing require might load the same code multiple times.
Also from the wiki:
If the module is a lua file, don't use the .lua extension
Here is a snippet of my current project for reference:

Code: Select all

require 'lib.buttons'
require 'enums'
require 'constants'
fun = require 'functions'
cf = require 'lib.commonfunctions'
lists = require 'lib.lists'
animate = require 'lib.animate'
For your images, you can use this:

Code: Select all

IMAGE = {}
IMAGE[1] = love.graphics.newImage("assets/images/myfile.png")
newImage requires a slash and not a period for some reason.

Re: how do i get files outside folder?

Posted: Fri Jul 05, 2024 4:10 pm
by pgimeno
togFox wrote: Fri Jul 05, 2024 5:49 am newImage requires a slash and not a period for some reason.
Because it is not a module. `require` is Lua's module manager and that works with periods; `newImage` works with love.filesystem and therefore requires slashes.