Page 1 of 3

Using dofile() ?

Posted: Sat Oct 08, 2011 8:04 am
by Wombat
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 ;)

Re: Using dofile() ?

Posted: Sat Oct 08, 2011 8:08 am
by Robin
Yes, but depending on what you want require is better.

Re: Using dofile() ?

Posted: Sat Oct 08, 2011 8:28 am
by Wombat
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

Re: Using dofile() ?

Posted: Sat Oct 08, 2011 9:29 am
by kraftman
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
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.

Code: Select all

local importedfunc = require "myfile"

importedfunc()

Re: Using dofile() ?

Posted: Sat Oct 08, 2011 9:50 am
by Robin
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() ?

Posted: Sat Oct 08, 2011 2:50 pm
by Wombat
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
I did like you´ve said but it don´t work. Nothing happens :(.

Re: Using dofile() ?

Posted: Sat Oct 08, 2011 3:12 pm
by T-Bone
Wombat wrote:
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
I did like you´ve said but it don´t work. Nothing happens :(.
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.

Re: Using dofile() ?

Posted: Sat Oct 08, 2011 3:20 pm
by Wombat
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 :(

Re: Using dofile() ?

Posted: Sat Oct 08, 2011 5:25 pm
by vrld
Time for a .love then (see the red box above for details).

Re: Using dofile() ?

Posted: Sat Oct 08, 2011 6:30 pm
by T-Bone
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?