Jump to content




What does loadfile() and dofile() do?


5 replies to this topic

#1 MrObsidy

  • Members
  • 63 posts
  • LocationHesse, Germany

Posted 29 August 2016 - 10:22 PM

So I've been busy looking at other peep's code and a thing I noticed rather quickly was that maaaaaany many many people used
dofile() and loadfile() instead of stuff like for example shell.run() or os.loadAPI(). I once thought I would be good at lua, but I just noticed I am not :D
Whatever, can somebody tell me
(1) Why to use these functions
(2) If they are part of CC or if they are custom-written and
(3) What the key differences to "standard" functions are.

Many thanks
-Alex

Edited by MrObsidy, 29 August 2016 - 10:24 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 30 August 2016 - 12:25 AM

dofile and loadfile are standard Lua. dofile executes a file. loadfile turns a file into a function, which you can then call.

shell.run and os.loadAPI, on the other hand, are CC lua specific. People that are used to programming in regular lua probably won't use them.

If you want to know more about dofile and loadfile, I'd just read the PIL.

#3 MrObsidy

  • Members
  • 63 posts
  • LocationHesse, Germany

Posted 30 August 2016 - 12:26 AM

many thanks

#4 CCJJSax

  • Members
  • 262 posts

Posted 30 August 2016 - 02:23 AM

That makes a lot of sense. Why did cc add in the os.load() api instead of just using these then?

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 30 August 2016 - 06:53 AM

Because loading files yourself is typically "the long way" of doing it, even if you know what you're doing (and let's face it, the associated concepts relating to environment tables are probably unfamiliar to most coders who consider themselves "proficient").

So even under regular Lua you'd typically use a wrapper function instead - require's the usual one, but CC's specific management of environment tables within CraftOS makes that a bit messy (we don't simply use _G as our environment, but rather we use a separate table which provides access to _G via a metatable, and we may or may not have multiple such tables on the one system...). Hence why os.loadAPI() is provided in its place.

Edited by Bomb Bloke, 30 August 2016 - 06:56 AM.


#6 CCJJSax

  • Members
  • 262 posts

Posted 30 August 2016 - 10:21 PM

Ok makes sense. Thanks for the info!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users