This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mrpoopy345's profile picture

Making a file a function

Started by mrpoopy345, 14 March 2014 - 08:34 PM
mrpoopy345 #1
Posted 14 March 2014 - 09:34 PM
Hello pros!
Today, for the use of pcall, I was trying to take a file, such as "startup" and take all the code from it and put it into a function.
So, for example purposes, could someone give me some code to take all the code from "startup", make that code into a function which can be used in the running program with pcall?
I am not asking to be spoon fed, just for some help.
Is there any way to do this?
Edited on 14 March 2014 - 08:35 PM
CometWolf #2
Posted 14 March 2014 - 09:46 PM
This is so simple imma just go with the spoon anyways

pcall(loadfile"/startup")
MKlegoman357 #3
Posted 14 March 2014 - 09:47 PM
loadfile does just that.


local func, err = loadfile("startup") --// Load the file

if not func then --// If program has errors 'func' is nil and 'err' contains the error
  error("There was an error loading startup:\n" .. err)
end

pcall(func) --// Call the function
mrpoopy345 #4
Posted 15 March 2014 - 11:15 AM
Thank you! But one question, just for further use, what exactly does loadfile do? What does it return?
theoriginalbit #5
Posted 15 March 2014 - 11:23 AM
what exactly does loadfile do? What does it return?
it loads a file into memory (os.loadAPI uses load file). It returns a function, just like you asked for; lol.
mrpoopy345 #6
Posted 15 March 2014 - 07:10 PM
Oh…
for some reason I thought it returned a string with the contents of the file in it *facepalm*

Thanks for all the help!