Using dofile() ?

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.
Wombat
Prole
Posts: 31
Joined: Mon Oct 03, 2011 8:38 pm

Using dofile() ?

Post 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 ;)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Using dofile() ?

Post by Robin »

Yes, but depending on what you want require is better.
Help us help you: attach a .love.
Wombat
Prole
Posts: 31
Joined: Mon Oct 03, 2011 8:38 pm

Re: Using dofile() ?

Post 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
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Using dofile() ?

Post 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()
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Using dofile() ?

Post 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
Help us help you: attach a .love.
Wombat
Prole
Posts: 31
Joined: Mon Oct 03, 2011 8:38 pm

Re: Using dofile() ?

Post 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 :(.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Using dofile() ?

Post 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.
Wombat
Prole
Posts: 31
Joined: Mon Oct 03, 2011 8:38 pm

Re: Using dofile() ?

Post 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 :(
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Using dofile() ?

Post by vrld »

Time for a .love then (see the red box above for details).
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Using dofile() ?

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

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests