Page 1 of 2

Can you make a game load another lua file

Posted: Mon Sep 01, 2008 5:33 pm
by keyspinner
I'm confused and need a little help,
how would you make a .lua just for the title screen, then say, press space and load a .lua, for say level 1.
Can you do that?
I hope you can because I will get awfully confused if you can't.

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 6:11 pm
by bartbes
Use love.filesystem.require or love.filesystem.include. However, the callbacks aren't automatically overwritten, so instead of:

Code: Select all

function draw()
--some stuff
end
do:

Code: Select all

draw = function ()
--some stuff
end
EDIT: I was WRONG please ignore code snippets above
And, even more important, you should call load yourself, cause it wont be called twice. (or you could do the load things outside of all function, in which case it will always run on first run of the script).

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 6:28 pm
by keyspinner
Sorry, I'm a bit new could you better explain that?
I sort of understand but I'm a bit confused on why I put
draw = function()

I have been trying to use the love.filesystem.include
but it tells me malformed number when I try to include 1.lua. (This is my first "level")

Alright I re-read your post and kind of understand,
I would do draw = function() so the 1.lua will overwrite the current one or what?


Here's my code,

Code: Select all

function load()
font = love.graphics.newFont(love.default_font, 12)
love.graphics.setFont(font)
title = "lua demo"
play = "press space to play"
end

function draw()
love.graphics.draw(title, 50, 50)
love.graphics.draw(play, 400, 300)
end

function keyreleased(space)
               //this is where I want it to make it open 1.lua
end
What should I do?

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 6:44 pm
by bartbes

Code: Select all

function keyreleased(key)
               if key == love.key_space then
                          love.filesystem.require("1.lua")
               end
end
This would work.
I will try to explain what I meant about the callbacks.
In the loader you create callbacks (load, draw and keyreleased in this case), however if you load the new file it won't overwrite those callbacks with it's own. So you reassign them by overwriting the variable (as functions are basically the same as numbers and strings). So we put a new function in place of the old, which is done by the following code:

Code: Select all

draw = function ()
--your draw code in the loaded lua file
end
EDIT: Also wrong, ofcourse.
(in this example I change draw, but I suppose you see that too)
After doing that it will use the callbacks of the loaded file.
However as I stated before the load callbacks doesn't get called twice, so you have to call it manually, which would produce the following in the loader:

Code: Select all

function keyreleased(key)
               if key == love.key_space then
                         love.filesystem.require("1.lua")
                         load()
               end
end
I hope this is clear enough.

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 6:47 pm
by keyspinner
I think I got it, but I will have to do it later, I am in a noisy room and have a headache, thank you for helping me.

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 7:07 pm
by bartbes
That's what I'm here for, however I just miserably failed to do the same thing.. So if it really doesnt work you might have to do some coding to enable multiple callbacks, there's already a topic around, however if you need help with that, just call.

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 7:09 pm
by keyspinner
Well, shouldn't there be something like, a type of erase function?
That erases old ones and loads the new ones?

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 7:17 pm
by keyspinner
Alright,
Here is my current code
(I changed 1.lua to first.lua)

main.lua (This is the title screen)

Code: Select all

function load()
font = love.graphics.newFont(love.default_font, 12)
love.graphics.setFont(font)
title = "lua demo"
play = "press space to play"
end

function draw()
love.graphics.draw(title, 50, 50)
love.graphics.draw(play, 400, 300)
end

function keyreleased(space)
if key == love.key_space then
love.filesystem.require(first.lua)
load()
end

end

draw = function()
love.graphics.draw(message, 200, 200)
end

First.lua (This is "level 1")

Code: Select all

function load()
font = love.graphics.newFont(love.default_font, 12)
love.graphics.setFont(font)
message = "Level 1"
end

function draw()
love.graphics.draw(message, 400, 300)
end
When I run it, I get this error:

Code: Select all

Stack Traceback:
[C]: in function 'draw'
main.lua:22: in function <main.lua:21>
What does this mean?

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 7:24 pm
by bartbes
That I failed was because I actually first included before I created the callbacks.

Well erase and create vs overwrite.. not really different.

I should actually thank you because of the testing I did for you I found out that you don't even have to do the draw = function() way, but you can just do function draw()... so... :oops:
Well, I will write add that in my previous posts.

SO the only thing you have to do is love.filesystem.require.

I'll go hide in a corner now... :oops: :oops:

EDIT: You posted while I was typing
In the second draw you define in main.lua you use message, however message doesn't exist and thus equals nil, however there is no draw function that excepts nil, so it crashes.

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 7:29 pm
by keyspinner
So I have to define message in main.lua? What is the point of making first.lua?