Difference between revisions of "love.filesystem.load"

m (Add LoadMode.)
 
(15 intermediate revisions by 11 users not shown)
Line 1: Line 1:
Load a file (but not run it)
+
{{newin|[[0.5.0]]|050|type=function}}
 +
Loads a Lua file (but does not run it).
 +
 
 +
This is equivalent to [https://www.lua.org/manual/5.1/manual.html#pdf-loadfile loadfile] except it operates on LÖVE filesystem paths.
 +
 
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
chunk = love.filesystem.load( name )
+
chunk, errormsg = love.filesystem.load( name, mode )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|string|name|The name (and path) of the file}}
+
{{param|string|name|The name (and path) of the file.}}
 +
{{New feature|12.0|
 +
{{param|LoadMode|mode ("bt")|Controls to only allow precompiled chunk, plain text, or both.}}
 +
|120}}
 
=== Returns ===
 
=== Returns ===
{{param|function|chunk|The loaded chunk}}
+
{{param|function|chunk|The loaded chunk.}}
== Example ==
+
{{param|string|errormsg (nil)|The error message if file could not be opened.}}
 +
== Examples ==
 
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.
 
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:
 
Also, it is worth noting that loaded files can return values. For example, the following file:
 +
 
<source lang="lua">
 
<source lang="lua">
return 1+1
+
return 1+1, "foo"
 
</source>
 
</source>
  
Will return 2, when called like this:
+
Will return 2 and "foo", when called like this:
  
 
<source lang="lua">
 
<source lang="lua">
chunk = love.filesystem.load( name ) -- load the chunk
+
local chunk = love.filesystem.load("someFolder/myFile.lua") -- load the chunk
local result = chunk() -- execute the chunk
+
local result1, result2 = chunk() -- execute the chunk
print('result: ' .. tostring(result)) -- prints 'result: 2'
+
print("Results: "..tostring(result1)..", "..tostring(result2)) -- prints 'Results: 2, foo'
 
</source>
 
</source>
  
This has the problem of completely halting your lua if there is a syntax error on the loaded file. If you want more robustness, you can surround the chunk with '''pcall''':
+
This raises an error if there's a syntax error in the loaded file. If you want your game to continue if the file isn't valid (for example if you expect it to be written by users), you can protect the loading and calling of the file with [https://www.lua.org/manual/5.1/manual.html#pdf-pcall '''pcall''']:
  
 
<source lang="lua">
 
<source lang="lua">
chunk = love.filesystem.load( name ) -- load the chunk
+
-- success, valueOrErrormsg = runFile( name )
local result, errMsg = pcall(chunk) -- execute the chunk safely
+
local function runFile(name)
 +
local ok, chunk, err = pcall(love.filesystem.load, name) -- load the chunk safely
 +
if not ok    then  return false, "Failed loading code: "..chunk  end
 +
if not chunk then  return false, "Failed reading file: "..err    end
 +
 
 +
local ok, value = pcall(chunk) -- execute the chunk safely
 +
if not ok then  return false, "Failed calling chunk: "..tostring(value)  end
  
if result == false then -- result will be false if there is an error
+
return true, value -- success!
  print('The following error happened: ' .. tostring(errMsg))
 
else
 
  print('The result of loading is: ' .. tostring(result))
 
 
end
 
end
 +
 +
print(runFile("goodFile.lua"))
 +
print(runFile("fileWithRuntimeError.lua"))
 +
print(runFile("fileWithSyntaxError.lua"))
 +
print(runFile("nonexistentFile.lua"))
 
</source>
 
</source>
 
Notice that this code will be confused if the file returns false on purpose. Avoid returning false inside your loaded code.
 
  
 
== See Also ==
 
== See Also ==
 
* [[parent::love.filesystem]]
 
* [[parent::love.filesystem]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Load a file (but not run it)}}
+
{{#set:Description=Loads a Lua file (but does not run it).}}
 +
{{#set:Since=000}}
 +
== Other Languages ==
 +
{{i18n|love.filesystem.load}}

Latest revision as of 05:56, 14 April 2024

Available since LÖVE 0.5.0
This function is not supported in earlier versions.

Loads a Lua file (but does not run it).

This is equivalent to loadfile except it operates on LÖVE filesystem paths.

Function

Synopsis

chunk, errormsg = love.filesystem.load( name, mode )

Arguments

string name
The name (and path) of the file.
Available since LÖVE 12.0
LoadMode mode ("bt")
Controls to only allow precompiled chunk, plain text, or both.

Returns

function chunk
The loaded chunk.
string errormsg (nil)
The error message if file could not be opened.

Examples

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:

return 1+1, "foo"

Will return 2 and "foo", when called like this:

local chunk = love.filesystem.load("someFolder/myFile.lua") -- load the chunk
local result1, result2 = chunk() -- execute the chunk
print("Results: "..tostring(result1)..", "..tostring(result2)) -- prints 'Results: 2, foo'

This raises an error if there's a syntax error in the loaded file. If you want your game to continue if the file isn't valid (for example if you expect it to be written by users), you can protect the loading and calling of the file with pcall:

-- success, valueOrErrormsg = runFile( name )
local function runFile(name)
	local ok, chunk, err = pcall(love.filesystem.load, name) -- load the chunk safely
	if not ok    then  return false, "Failed loading code: "..chunk  end
	if not chunk then  return false, "Failed reading file: "..err    end

	local ok, value = pcall(chunk) -- execute the chunk safely
	if not ok then  return false, "Failed calling chunk: "..tostring(value)  end

	return true, value -- success!
end

print(runFile("goodFile.lua"))
print(runFile("fileWithRuntimeError.lua"))
print(runFile("fileWithSyntaxError.lua"))
print(runFile("nonexistentFile.lua"))

See Also


Other Languages