Help in using draw function in other scripts.

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
Midhun
Prole
Posts: 5
Joined: Sun Oct 06, 2024 1:33 pm

Help in using draw function in other scripts.

Post by Midhun »

So in my game, there are three scripts the main.lua script draws the player which works absolutely fine but the other script, in pauseMenu.lua the draw function is not working, it does not draw anything if we create any script other than main.lua the same happens the draw function is not working. Can anyone help me???
MrFariator
Party member
Posts: 559
Joined: Wed Oct 05, 2016 11:53 am

Re: Help in using draw function in other scripts.

Post by MrFariator »

You need to return a function (or other data) from your other scripts, that you can then use in the script that has love.draw defined. Like lets consider the following, simplistic example:

Code: Select all

-- otherScript.lua
local function myDraw ()
  love.graphics.rectangle ( "fill", 10, 10, 100, 100 )
end
return myDraw

-- main.lua
local drawFunctionFromOtherScript = require ( "otherScript" )
function love.draw ( )
  drawFunctionFromOtherScript ( )
end
Here, we've defined a function in one script, while the main file has love.draw defined. The main file won't know the existence of any other scripts, or their draw functions, unless you somehow explictly give a reference to it (like with require). There are other approaches you can take too, but that's the gist of it.

Also keep in mind that if you define love.draw multiple times, only the one that was last defined is used. As such, you (usually) tend to just define love.draw once, and then call other functions inside it. You could also change what function love.draw points at during runtime (ex: using different love.draw during main menu and gameplay), but you'd need to handle tracking/swapping that yourself in your game logic.
Midhun
Prole
Posts: 5
Joined: Sun Oct 06, 2024 1:33 pm

Re: Help in using draw function in other scripts.

Post by Midhun »

Thank you so much. It worked
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 11 guests