Difference between revisions of "love.filesystem.load"

(Added example)
Line 9: Line 9:
 
=== Returns ===
 
=== Returns ===
 
{{param|function|chunk|The loaded chunk}}
 
{{param|function|chunk|The loaded chunk}}
== Examples ==
+
== Example ==
=== Execute everything in the folder "levels" ===
+
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:
 
<source lang="lua">
 
<source lang="lua">
for i, file in love.filesystem.enumerate("levels") do
+
return 1+1
    love.filesystem.load("levels/" .. file)() -- last pair of parentheses runs it
+
</source>
end</source>
+
 
 +
Will return 2, when called like this:
 +
 
 +
<source lang="lua">
 +
chunk = love.filesystem.load( name ) -- load the chunk
 +
local result = chunk() -- execute the chunk
 +
print('result: ' .. tostring(result)) -- prints 'result: 2'
 +
</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''':
 +
 
 +
<source lang="lua">
 +
chunk = love.filesystem.load( name ) -- load the chunk
 +
local result, errMsg = pcall(chunk) -- execute the chunk safely
 +
 
 +
if result == false then -- result will be false if there is an error
 +
  print('The following error happened: ' .. tostring(errMsg))
 +
else
 +
  print('The result of loading is: ' .. tostring(result))
 +
end
 +
</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=Load a file (but not run it)}}

Revision as of 15:53, 14 November 2010

Load a file (but not run it)

Function

Synopsis

chunk = love.filesystem.load( name )

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:

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 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:

chunk = love.filesystem.load( name ) -- load the chunk
local result, errMsg = pcall(chunk) -- execute the chunk safely

if result == false then -- result will be false if there is an error
  print('The following error happened: ' .. tostring(errMsg))
else
  print('The result of loading is: ' .. tostring(result))
end

Notice that this code will be confused if the file returns false on purpose. Avoid returning false inside your loaded code.

See Also