Help in using draw function in other scripts.
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Help in using draw function in other scripts.
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???
-
- Party member
- Posts: 559
- Joined: Wed Oct 05, 2016 11:53 am
Re: Help in using draw function in other scripts.
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:
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.
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
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.
Thank you so much. It worked
Who is online
Users browsing this forum: Bing [Bot] and 11 guests