Drawing images in another script?

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.
Post Reply
User avatar
Calahagus
Prole
Posts: 6
Joined: Thu Jun 06, 2013 1:16 am

Drawing images in another script?

Post 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?
User avatar
chezrom
Citizen
Posts: 59
Joined: Tue May 28, 2013 11:03 pm
Location: France

Re: Drawing images in another script?

Post 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.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Drawing images in another script?

Post 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.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
Post Reply

Who is online

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