More scripts than main?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
More scripts than main?
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?
Re: More scripts than main?
If you have another file test.lua you would put require( "test" ) inside main.
-
- Party member
- Posts: 559
- Joined: Wed Oct 05, 2016 11:53 am
Re: More scripts than main?
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:
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):
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
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
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest