Run a different file *solved*

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
TeeJayz
Prole
Posts: 3
Joined: Tue Aug 14, 2012 1:27 pm

Run a different file *solved*

Post 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?
Last edited by TeeJayz on Tue Aug 14, 2012 8:25 pm, edited 1 time in total.
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Run a different file

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

Re: Run a different file

Post 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.
Help us help you: attach a .love.
TeeJayz
Prole
Posts: 3
Joined: Tue Aug 14, 2012 1:27 pm

Re: Run a different file

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

Re: Run a different file

Post by Robin »

It happens automatically. You don't need to worry about that.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests