This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Tables? (for making a settings file)
Started by OrdinaryCassetteNerd, 11 December 2017 - 05:15 PMPosted 11 December 2017 - 06:15 PM
This one was making me go mad ever since I tried to make my own GUI OS. Basically, I need to create a whole table of settings that will fit into one file, but how? I have thought of writing each setting in each file, but not practical, right? The real question is, how do I fit all settings for my OS into one file? I will accept the JSON API, but I prefer the settings API as I find it much easier to use.
Posted 11 December 2017 - 06:20 PM
I see you've mentioned the settings API, have you had a look at its documentation and implementation?
More specifically, One can use textutils.serialise and textutils.unserialise to convert from a table to a string and back again. One can serialise a table, write it to a file to save settings. Then read a file and deserialise it to load settings.
More specifically, One can use textutils.serialise and textutils.unserialise to convert from a table to a string and back again. One can serialise a table, write it to a file to save settings. Then read a file and deserialise it to load settings.
Posted 11 December 2017 - 06:29 PM
OK, gonna try. Thanks!More specifically, One can use textutils.serialise and textutils.unserialise to convert from a table to a string and back again. One can serialise a table, write it to a file to save settings. Then read a file and deserialise it to load settings.
P. S. How to even create a table?
Edited on 11 December 2017 - 05:30 PM
Posted 11 December 2017 - 06:36 PM
I'd suggest not starting work on an OS quite yet. Have a read through Programming in Lua (maybe the Lua manual as well if you're feeling brave) and try to build some simple programs first: door looks, turtle quarry, etc… Otherwise it's going to be like learning to swim by chucking someone into the ocean.P. S. How to even create a table?
Posted 11 December 2017 - 07:08 PM
One thing I would suggest, if you're still interested in figuring out how to do this, might be to write an interpreter to read and write to .conf files. It'd be a good way to help introduce you to learning how to work with tables, as well as learning how to read and write from files and basic string manipulation.
Posted 11 December 2017 - 07:45 PM
I'd suggest not starting work on an OS quite yet. Have a read through Programming in Lua (maybe the Lua manual as well if you're feeling brave) and try to build some simple programs first: door looks, turtle quarry, etc… Otherwise it's going to be like learning to swim by chucking someone into the ocean.
To build on what SquidDev said, and to help you along in whatever project you end up choosing to do, I also suggest you read Lupus590's tutorial on making complex goals manageable.
And to give you an idea of the can of worms you're opening with trying to make an OS, take some time and read through BB's Guide to Coroutines - unless you can understand this, your 'OS' isn't going to be much of an OS as all.
As SquidDev said, you should really focus on simpler projects to start, then work your way up to writing something as complex as an 'OS'.
Edited on 11 December 2017 - 06:46 PM
Posted 11 December 2017 - 08:12 PM
Yeah, yeah, yeah, but how do I actually choose an entry from the table and use its value? Same thing as with arguments "args[1]"?
Posted 11 December 2017 - 08:22 PM
That depends on how you make the table in question, and syntactical preference. For example, term is a table. As is os. args is also a table, though IIRC it is deprecated.
https://www.lua.org/pil/2.5.html
https://www.lua.org/pil/2.5.html
Edited on 11 December 2017 - 07:22 PM
Posted 12 December 2017 - 05:35 PM
Then what's the non-deprecated equivalent to args?That depends on how you make the table in question, and syntactical preference. For example, term is a table. As is os. args is also a table, though IIRC it is deprecated. https://www.lua.org/pil/2.5.html
Posted 12 December 2017 - 07:24 PM
Then what's the non-deprecated equivalent to args?
http://www.computercraft.info/wiki/Hello_World_Tutorial
Posted 12 December 2017 - 08:26 PM
Then what's the non-deprecated equivalent to args?
Varargs, which are explained in this section of the manual.
Typically I will simply define tArgs:
local tArgs = {...}
…though in some cases I'll simply use them as-is, without a table.
Posted 13 December 2017 - 12:48 AM
…local tArgs = {...}
…though in some cases I'll simply use them as-is, without a table.
It's odd to me how everyone wraps varargs in a table, and names it exactly "tArgs", nothing else, almost universally. Is this some sort of unwritten rule of CC? I find this syntax much more intuitive:
local arg1, arg2, arg3 = ...
Posted 13 December 2017 - 01:08 AM
It's odd to me how everyone wraps varargs in a table, and names it exactly "tArgs", nothing else, almost universally. Is this some sort of unwritten rule of CC? I find this syntax much more intuitive:local arg1, arg2, arg3 = ...
If I'm not mistaken, tArgs is shorthand for 'table of arguments'.
I can't tell you why others use a table, but I can tell you why I do. While capturing each argument in a separate variable might be easier in some instances, what do you do when you don't know how many arguments you are going to get? For me, regardless of count, putting them in a table makes it easier to manage since each arg is a separate table entry and they're numbered sequentially (and I don't have to type out arg1, arg2, arg3, arg4, etc. = …).
Edited on 13 December 2017 - 12:12 AM
Posted 13 December 2017 - 01:11 AM
Personally, I always make tArgs so I can verify that my program is getting the correct command line arguments. That or I'm using it inside a function which is designed to take (virtually) unlimited arguments, which would be impossible to represent using that syntax.
I'm pretty sure I picked up this habit from reading the source code.
I'm pretty sure I picked up this habit from reading the source code.
Edited on 13 December 2017 - 12:13 AM
Posted 13 December 2017 - 11:07 AM
It's odd to me how everyone wraps varargs in a table, and names it exactly "tArgs", nothing else, almost universally. Is this some sort of unwritten rule of CC?
It's more of a Lua rule than just CC, but I don't like hungerian notation and would use args as my variable name. Although, in languages where you don't delare types (like Lua), I see the usefulness of hungerian notation.
Posted 13 December 2017 - 12:00 PM
Then what's the non-deprecated equivalent to args?
Varargs, which are explained in this section of the manual.
Er, not exactly. The auto-built "args" table is a feature of vararg functions, not some sort of "alternative". There is no equivalent: after Lua 5.1, you're simply expected to build that table yourself, if you want it.
The feature's still available in ComputerCraft, but many coders don't use it for whatever reason. Probably because they don't realise it's there. It's handy, though, in that it automatically includes an "n" key telling you exactly how many arguments were passed to the function - you don't have to request a count-up later.
Posted 13 December 2017 - 12:03 PM
It's worth noting that it's removed in Lua 5.2. Whilst CC uses 5.1 (and likely will forever) I do like to code with that in mind. If you use table.pack(…), one will also get the n field - especially useful when you've got nil arguments.The feature's still available in ComputerCraft, but many coders don't use it for whatever reason. Probably because they don't realise it's there. It's handy, though, in that it automatically includes an "n" key telling you exactly how many arguments were passed to the function - you don't have to request a count-up later.