Page 1 of 1

Help in using draw function in other scripts.

Posted: Sun Oct 06, 2024 1:39 pm
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???

Re: Help in using draw function in other scripts.

Posted: Sun Oct 06, 2024 2:59 pm
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.

Re: Help in using draw function in other scripts.

Posted: Mon Oct 07, 2024 11:01 am
by Midhun
Thank you so much. It worked