Page 2 of 2

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 7:41 pm
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).

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 7:47 pm
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. :(

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 7:51 pm
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.

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 7:53 pm
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.

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 7:55 pm
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.

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 8:06 pm
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

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 8:08 pm
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")

Re: Can you make a game load another lua file

Posted: Mon Sep 01, 2008 8:09 pm
by keyspinner
Thanks man, I'm just learning something new every minute,
let's hope this works.
Success! Thanks!