Can you make a game load another lua file
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- keyspinner
- Prole
- Posts: 24
- Joined: Mon Sep 01, 2008 12:03 am
Can you make a game load another lua file
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.
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.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Can you make a game load another lua file
Use love.filesystem.require or love.filesystem.include. However, the callbacks aren't automatically overwritten, so instead of:
do:
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).
Code: Select all
function draw()
--some stuff
end
Code: Select all
draw = function ()
--some stuff
end
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).
Last edited by bartbes on Mon Sep 01, 2008 8:09 pm, edited 1 time in total.
- keyspinner
- Prole
- Posts: 24
- Joined: Mon Sep 01, 2008 12:03 am
Re: Can you make a game load another lua file
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,
What should I do?
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
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Can you make a game load another lua file
Code: Select all
function keyreleased(key)
if key == love.key_space then
love.filesystem.require("1.lua")
end
end
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
(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
Last edited by bartbes on Mon Sep 01, 2008 8:10 pm, edited 1 time in total.
- keyspinner
- Prole
- Posts: 24
- Joined: Mon Sep 01, 2008 12:03 am
Re: Can you make a game load another lua file
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.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Can you make a game load another lua file
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.
- keyspinner
- Prole
- Posts: 24
- Joined: Mon Sep 01, 2008 12:03 am
Re: Can you make a game load another lua file
Well, shouldn't there be something like, a type of erase function?
That erases old ones and loads the new ones?
That erases old ones and loads the new ones?
- keyspinner
- Prole
- Posts: 24
- Joined: Mon Sep 01, 2008 12:03 am
Re: Can you make a game load another lua file
Alright,
Here is my current code
(I changed 1.lua to first.lua)
main.lua (This is the title screen)
First.lua (This is "level 1")
When I run it, I get this error:
What does this mean?
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
Code: Select all
Stack Traceback:
[C]: in function 'draw'
main.lua:22: in function <main.lua:21>
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Can you make a game load another lua file
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...
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...
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.
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...
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...
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.
- keyspinner
- Prole
- Posts: 24
- Joined: Mon Sep 01, 2008 12:03 am
Re: Can you make a game load another lua file
So I have to define message in main.lua? What is the point of making first.lua?
Who is online
Users browsing this forum: Ahrefs [Bot] and 2 guests