Can you make a game load another lua file

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.
User avatar
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

Post by bartbes »

Or you take the second draw from main.lua, which would seem a logical thing to do.
However, you posed the question why you should use first.lua, well honoustly, I don't know. If you want to use those as levels, it is possible to just use a lua file that defines a load of variables, which you then execute with the 'engine' (which is main.lua in my example).
User avatar
keyspinner
Prole
Posts: 24
Joined: Mon Sep 01, 2008 12:03 am

Re: Can you make a game load another lua file

Post by keyspinner »

Well I was mainly trying to do this to not confuse myself, because I do this all the time, for example in math class I write too much and can't find anything.
I am going to need you fix my code so I can see how it's supposed to look, if you know how.
Because I suck at understanding anything for some reason. :(
User avatar
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

Post by bartbes »

main.lua:

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(key)
    if key == love.key_space then
    love.filesystem.require(first.lua)
    load()
    end

    end


First.lua:

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
It should work now, only difference is a removed 2nd draw in main.lua, and fixed a tiny bug.
User avatar
keyspinner
Prole
Posts: 24
Joined: Mon Sep 01, 2008 12:03 am

Re: Can you make a game load another lua file

Post by keyspinner »

Oh, thanks I see what you did!
I'm so blind, I should have realized there should be only 1 draw function.
Thank you.
Last edited by keyspinner on Mon Sep 01, 2008 8:06 pm, edited 2 times in total.
User avatar
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

Post by bartbes »

As you can see in my first few posts, everyone makes mistakes.
If you need any more help, don't be afraid to ask, that's what these forums are for.
User avatar
keyspinner
Prole
Posts: 24
Joined: Mon Sep 01, 2008 12:03 am

Re: Can you make a game load another lua file

Post by keyspinner »

Now I'm getting a runtime error
Stack traceback:
line 15 in main.lua for function in line 13
it tells me 'first' is a 'nil' value
User avatar
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

Post by bartbes »

Yeah, missed that one, first.lua should have quotation marks around it as it's a string not a variable, so it should be

Code: Select all

love.filesystem.require("first.lua")
User avatar
keyspinner
Prole
Posts: 24
Joined: Mon Sep 01, 2008 12:03 am

Re: Can you make a game load another lua file

Post by keyspinner »

Thanks man, I'm just learning something new every minute,
let's hope this works.
Success! Thanks!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 50 guests