how can I call a script in Love2d?
I want to have function love.draw in another script instead of shoving everything in main.lua.
or is this just impossible...
How do I call Scripts in Love2d?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: How do I call Scripts in Love2d?
You can do it like this:
Put this at the top of main.lua (Where OTHERFILE is the name of your "script"):
and then it will run when main.lua is run.
Put this at the top of main.lua (Where OTHERFILE is the name of your "script"):
Code: Select all
require "OTHERFILE" --Note the lack of ".lua"
Your screen is very zoomed in...
Re: How do I call Scripts in Love2d?
You can compile Lua script files at runtime using:
Notice that you don't write the extension of the file, so:
Should be:
If your file is not in the root folder (or one of the default folders searched by require) then its:
Where you use the "." symbol to indicate a directory (instead of the typical "/" or "\\").
There's also "dofile" but I heard it's unsupported in Love2D.
Make sure your functions in the required file are global.
Otherwise they won't be accessible from your main file.
If your functions in the included file are NOT global, you can have several options.
You can return a "table" containing the functions from the included file or use the "module" keyword:
myfile.lua
Your main file:
The module keyword:
myfile.lua
Your main file:
These are just a couple of ways to avoid polluting the global namespace.
Code: Select all
require("filename")
Code: Select all
require("myfile.lua")
Code: Select all
require("myfile")
Code: Select all
require("directoryname.filename")
There's also "dofile" but I heard it's unsupported in Love2D.
Make sure your functions in the required file are global.
Otherwise they won't be accessible from your main file.
If your functions in the included file are NOT global, you can have several options.
You can return a "table" containing the functions from the included file or use the "module" keyword:
myfile.lua
Code: Select all
local function foo() end
local function bar() end
local interface = { foo = foo, bar = bar }
return interface
Code: Select all
interface = require("myfile")
interface.foo()
myfile.lua
Code: Select all
module ("interface")
-- the following function are NOT global
function foo() end
function bar() end
Code: Select all
require("myfile")
interface.foo()
Re: How do I call Scripts in Love2d?
thanks for the awesome help. It was much easier that I thought.
(^-^)(^_^)(^0^)
(^-^)(^_^)(^0^)
Who is online
Users browsing this forum: Ahrefs [Bot] and 85 guests