65 posts
Location
Hesse, Germany
Posted 29 August 2016 - 11: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 on 29 August 2016 - 09:24 PM
3057 posts
Location
United States of America
Posted 30 August 2016 - 01: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.
65 posts
Location
Hesse, Germany
Posted 30 August 2016 - 01:26 AM
many thanks
259 posts
Posted 30 August 2016 - 03:23 AM
That makes a lot of sense. Why did cc add in the os.load() api instead of just using these then?
7083 posts
Location
Tasmania (AU)
Posted 30 August 2016 - 07: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 on 30 August 2016 - 05:56 AM
259 posts
Posted 30 August 2016 - 11:21 PM
Ok makes sense. Thanks for the info!