Page 1 of 1

Drawing images in another script?

Posted: Thu Jun 20, 2013 11:23 pm
by Calahagus
So I have a bit of a problem when I use "function love.draw ()" in a script other than my main one. I use "require("scriptname")" to load that script but when I use the draw function it draws the things from that script only, with a black screen covering up everything from my main.lua file. How can I draw from another script without it messing up everything else?

Re: Drawing images in another script?

Posted: Thu Jun 20, 2013 11:45 pm
by chezrom
If I understand, you want to define several 'love.draw' functions : it can't work, the last defined override the others.
You must have only one 'love.draw' (in main.lua usually) that call other functions defined in your modules.

Re: Drawing images in another script?

Posted: Fri Jun 21, 2013 1:02 am
by substitute541
I think he needs gamestates instead of multiple love functions.

Try having a different "draw" function instead of using multiple love.draw functions in each file you're requiring. For example:

Code: Select all

-- file1
function file1_draw()

end

-- file2
function file2_draw()

end

-- file3
function file3_draw()

end
And if you want to call one of the draw functions, you can have a global gamestate variable:

Code: Select all

-- in love.load()
gamestate = "n/a"
To use the gamestate variable in love.draw to call one of the draw functions, you can do:

Code: Select all

-- in love.draw
if gamestate == "file1" then
    file1_draw()
elseif gamestate == "file2" then
    file2_draw()
elseif gamestate == "file3" then
    file3_draw()
end
That concept can be used for the love.update(dt) (file1_update(dt), file2_update(dt), file3_update(dt)) as well.

Hope it helps!

Edit: Fixed a typo in my code.