how do i get files outside folder?

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
VXDev
Prole
Posts: 6
Joined: Sun May 28, 2023 6:12 pm

how do i get files outside folder?

Post 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
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: how do i get files outside folder?

Post 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"
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: how do i get files outside folder?

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: how do i get files outside folder?

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 12 guests