Hi community,
is it possible to use dofile() to load AND run .lua files in LUA, or is there another possibility to load and run lua files?
Thanks in advance
Using dofile() ?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Using dofile() ?
Yes, but depending on what you want require is better.
Help us help you: attach a .love.
Re: Using dofile() ?
is it possible that i have to give the full path, when i want to load a data with dofile().
For me, dofile("file.lua") doesn´t work, it says: "no such file or directory".
Here´s my code:
For me, dofile("file.lua") doesn´t work, it says: "no such file or directory".
Here´s my code:
Code: Select all
function load.love()
love.graphics.setMode(800, 600, false, false, 0)
end
function love.update(dt)
if love.keyboard.isDown("a") then
dofile("main2.lua")
end
end
Re: Using dofile() ?
running a file every time a key is pressed is a bad idea. Have the file return a function, then require the file and run iits function on key press.Wombat wrote:is it possible that i have to give the full path, when i want to load a data with dofile().
For me, dofile("file.lua") doesn´t work, it says: "no such file or directory".
Here´s my code:Code: Select all
function load.love() love.graphics.setMode(800, 600, false, false, 0) end function love.update(dt) if love.keyboard.isDown("a") then dofile("main2.lua") end end
Code: Select all
local importedfunc = require "myfile"
importedfunc()
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Using dofile() ?
Oh yeah, you shouldn't use dofile().
While require is usually what you need, in exceptional cases you will want to use love.filesystem.load, like this:
While require is usually what you need, in exceptional cases you will want to use love.filesystem.load, like this:
Code: Select all
local func = love.filesystem.load("somefile.lua")
if func then -- no syntax errors
func()
end
Help us help you: attach a .love.
Re: Using dofile() ?
I did like you´ve said but it don´t work. Nothing happens .Oh yeah, you shouldn't use dofile().
While require is usually what you need, in exceptional cases you will want to use love.filesystem.load, like this:
Code: Select all
local func = love.filesystem.load("somefile.lua") if func then -- no syntax errors func() end
Re: Using dofile() ?
That should work. Where are the files placed? If you're running a folder, the file has to be either in the folder or in the write directory. If you're running a .love-file, it has to be inside the .love file or in the write directory.Wombat wrote:I did like you´ve said but it don´t work. Nothing happens .Oh yeah, you shouldn't use dofile().
While require is usually what you need, in exceptional cases you will want to use love.filesystem.load, like this:
Code: Select all
local func = love.filesystem.load("somefile.lua") if func then -- no syntax errors func() end
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: Using dofile() ?
the file is in the write directory, where the main.lua is, too.
But finally it doesn´t work.
Sorry for getting on your nervs
But finally it doesn´t work.
Sorry for getting on your nervs
Re: Using dofile() ?
Time for a .love then (see the red box above for details).
Re: Using dofile() ?
I find that placing main.lua in the write directory doesn't work for me when I run it as a folder. Why would you want to place main.lua there?
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests