Views
love.filesystem.load
Load a lua file (but not run it)
Function
Synopsis
Arguments
string name
- The name (and path) of the file
Returns
function chunk
- The loaded chunk
Example
It is important to note that love.filesystem.load does not invoke the code, it just creates a function (a 'chunk') that will contain the contents of the file inside it. In order to execute the chunk, you have to put () behind it.
Also, it is worth noting that loaded files can return values. For example, the following file:
Will return 2, when called like this:
local result = chunk() -- execute the chunk
print('result: ' .. tostring(result)) -- prints 'result: 2'
This bluescreens if there is a syntax error in the loaded file. If you want to continue your game if the file is not valid (for example if you expect it to be written by users), you can protect the calling of the chunk with pcall:
ok, chunk = pcall( love.filesystem.load, name ) -- load the chunk safely
if not ok then
print('The following error happend: ' .. tostring(chunk))
else
ok, result = pcall(chunk) -- execute the chunk safely
if not ok then -- will be false if there is an error
print('The following error happened: ' .. tostring(result))
else
print('The result of loading is: ' .. tostring(result))
end
end
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info