Jump to content




Making a file a function


  • You cannot reply to this topic
5 replies to this topic

#1 mrpoopy345

  • Members
  • 148 posts
  • LocationMy Computer

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 by mrpoopy345, 14 March 2014 - 09:35 PM.


#2 CometWolf

  • Members
  • 1,283 posts

Posted 14 March 2014 - 09:46 PM

This is so simple imma just go with the spoon anyways
pcall(loadfile"/startup")


#3 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

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


#4 mrpoopy345

  • Members
  • 148 posts
  • LocationMy Computer

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?

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 15 March 2014 - 11:23 AM

View Postmrpoopy345, on 15 March 2014 - 11:15 AM, said:

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.

#6 mrpoopy345

  • Members
  • 148 posts
  • LocationMy Computer

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!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users