Page 1 of 1

More scripts than main?

Posted: Sat Sep 23, 2017 6:27 pm
by Nxvn
Still learning how Love functions... If I wanted to add more scripts than just 'main,' would I just add them the same way I added main? :huh:

Re: More scripts than main?

Posted: Sat Sep 23, 2017 8:45 pm
by BorhilIan
If you have another file test.lua you would put require( "test" ) inside main.

Re: More scripts than main?

Posted: Sat Sep 23, 2017 8:46 pm
by MrFariator
Require is the simplest method, and I'd recommend it when starting out.

Let's say you want to create myScript.lua, perhaps inside a subfolder. This subfolder should reside in the same folder as your main.lua file. You'd basically then write the functions and code there like normal, and then in your main.lua you simply write:

Code: Select all

require "myFolder/myScript" -- Exact capitalization is important
That way every variable and function that is not local will be available inside main.lua. However, doing this too much would eventually pollute your global namespace, and so you probably want to use locals instead. Here's an example how to load a local function to your main.lua (or whatever other variable):

Code: Select all

-- Inside myScript.lua
local function myFunction ( x,y )
 -- ...Function code...
end

return myFunction -- Return the function, so any other file that requires myScript gets an access to it

-- Inside main.lua
function love.load ( )
  local f = require "myFolder/myScript"
  -- We can now use myFunction from myScript
  f(0,0)
end