Page 1 of 1
Run a different file *solved*
Posted: Tue Aug 14, 2012 3:36 pm
by TeeJayz
In main.lua, how do you run a different .lua file? I need this for a menu screen because I cannot undraw images. I dont think I need to post any code, so any answers?
Re: Run a different file
Posted: Tue Aug 14, 2012 3:48 pm
by Qcode
To load a different file you have to put require "filename" (IMPORTANT: Make sure you put the file NAME, but not the extension. So require "menu" not require "menu.lua".)
Now none of your code is going to be run, unless you call those functions inside of the main love. functions. Here's an example.
This is what your menu.lua could look like
Code: Select all
function menu_load()
--Menu loading stuff
end
function menu_draw()
-- Menu drawing stuff
end
function menu_update(dt)
-- Menu updating stuff
end
And now here is what your main.lua could look like
Code: Select all
require "menu"
function love.load()
menu_load()
-- Other loading stuff
end
function love.draw()
menu_draw()
-- Other drawing stuff
end
function love.update(dt)
menu_update(dt)
-- Other updating stuff
end
And that's all it takes to run stuff from other files.
One more little thing to note. When you have a parameter that you want to use inside one of your functions (For example, dt in menu_update) you must have that parameter written when you call it. So like this
Code: Select all
function menu_update(dt)
--Dt stuff
end
That is fine as long as you call it like this
Code: Select all
function love.update(dt) -- You need to have dt in this as well
menu_update(dt) --See the dt
end
Re: Run a different file
Posted: Tue Aug 14, 2012 4:05 pm
by Robin
TeeJayz wrote:I cannot undraw images
You don't need to.
Every frame, the screen is cleared. Every frame, love.update() and love.draw() are run. Use
if statements and
for loops and so on to decide what should be drawn.
Re: Run a different file
Posted: Tue Aug 14, 2012 4:11 pm
by TeeJayz
Robin wrote:TeeJayz wrote:I cannot undraw images
You don't need to.
Every frame, the screen is cleared. Every frame, love.update() and love.draw() are run. Use
if statements and
for loops and so on to decide what should be drawn.
I dont know how to go forward 1 frame.
Re: Run a different file
Posted: Tue Aug 14, 2012 4:14 pm
by Robin
It happens automatically. You don't need to worry about that.