Difference between revisions of "love.filesystem.load"

m (included link to other languages)
m (Add newin)
(8 intermediate revisions by 5 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).
 +
 
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
chunk = love.filesystem.load( name )
+
chunk, errormsg = love.filesystem.load( name )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|string|name|The name (and path) of the file}}
+
{{param|string|name|The name (and path) of the file.}}
 
=== Returns ===
 
=== Returns ===
{{param|function|chunk|The loaded chunk}}
+
{{param|function|chunk|The loaded chunk.}}
 +
{{param|string|errormsg (nil)|The error message if file could not be opened.}}
 
== Example ==
 
== 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.
 
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.
Line 25: Line 28:
 
</source>
 
</source>
  
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''':
+
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 [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
+
local ok, chunk, result
local ok, errMsg = pcall(chunk) -- execute the chunk safely
+
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
+
  if not ok then -- will be false if there is an error
  print('The following error happened: ' .. tostring(errMsg))
+
    print('The following error happened: ' .. tostring(result))
else
+
  else
  print('The result of loading is: ' .. tostring(errMsg))
+
    print('The result of loading is: ' .. tostring(result))
 +
  end
 
end
 
end
 
</source>
 
</source>
Line 41: Line 49:
 
* [[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 ==
 
== Other Languages ==
 
{{i18n|love.filesystem.load}}
 
{{i18n|love.filesystem.load}}

Revision as of 12:14, 15 December 2018

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

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

Function

Synopsis

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

Arguments

string name
The name (and path) of the file.

Returns

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

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:

return 1+1

Will return 2, when called like this:

chunk = love.filesystem.load( name ) -- load the chunk
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:

local ok, chunk, result
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