More scripts than main?

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
Nxvn
Prole
Posts: 1
Joined: Sat Sep 23, 2017 6:23 pm

More scripts than main?

Post 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:
BorhilIan
Prole
Posts: 38
Joined: Wed Mar 15, 2017 6:46 am

Re: More scripts than main?

Post by BorhilIan »

If you have another file test.lua you would put require( "test" ) inside main.
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: More scripts than main?

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests