If I used a table, is there a way I could update and save that table via RedNet so that it's not lost on reboot?
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Update Tables via RedNet
Started by OnyxFox, 10 August 2015 - 11:01 PMPosted 11 August 2015 - 01:01 AM
What I have in my world is a Computer controlled door that opens using an OP Sensor, set to open when only I walk over the threshold. I'm also using a pocket computer running a program I wrote so I can also send a command to open/close the door. What I would like to add is a way to update who has privileges for the door via RedNet. That way I could type something like "door addID <playername>" or "door removeID <playername>" to alter it without having to dig open my wall to update the computer.
If I used a table, is there a way I could update and save that table via RedNet so that it's not lost on reboot?
If I used a table, is there a way I could update and save that table via RedNet so that it's not lost on reboot?
Posted 11 August 2015 - 01:23 AM
Yes, you would use textutils.serialize to make the table into a string (provided there aren't any functions in said table) and use the fs or io API to write that string to a file. On starting the script, you would read the file using fs or io, and use textutils.unserialize to turn the string back into a table.
Posted 11 August 2015 - 11:08 PM
Yes, you would use textutils.serialize to make the table into a string (provided there aren't any functions in said table) and use the fs or io API to write that string to a file. On starting the script, you would read the file using fs or io, and use textutils.unserialize to turn the string back into a table.
Ok thanks I'll work on that. Can I add to the table without having to open and edit the file before I send it?
So if the Table looks like this
local list = {
OnyxFox = true,
Mewtant6 = true,
maggmuffin = true
}
is there a way to use a terminal command to add to that table?Like if I wrote in the terminal
doorID add <playername>
How do I get it to add "<playername> = true" to the table?Also the reverse with remove <playername>?
Any chance you could give me a small example code for something similar? Doesn't need to be exact it would just help with grasping the concept.
Posted 12 August 2015 - 12:48 AM
You don't need to send a table from the pocket computer to the door computer.
When the program 'door add name' is run on the pocket computer, it would send 'name' over rednet.
The door computer would listen for messages, and when it receives one, it would add the message to the list of acceptable names, and save the list
Obviously, this is extremely insecure, but there are a few ways to fix that if you are concerned.
If you have any questions on any of this, feel free to ask :)/>
When the program 'door add name' is run on the pocket computer, it would send 'name' over rednet.
The door computer would listen for messages, and when it receives one, it would add the message to the list of acceptable names, and save the list
Obviously, this is extremely insecure, but there are a few ways to fix that if you are concerned.
If you have any questions on any of this, feel free to ask :)/>
Posted 12 August 2015 - 01:51 AM
Ok so then I could use 'add' or 'remove' as the protocol?
So if I already have an existing table, what commands do I use to add or remove one value, without rewriting the table?
I'm playing on a private server with my friends, and I am the only one who endeavors to program with CC, so I'm not extremely worried about security at the moment :)/>
So if I already have an existing table, what commands do I use to add or remove one value, without rewriting the table?
I'm playing on a private server with my friends, and I am the only one who endeavors to program with CC, so I'm not extremely worried about security at the moment :)/>
Edited on 11 August 2015 - 11:52 PM
Posted 12 August 2015 - 03:44 AM
Ok so then I could use 'add' or 'remove' as the protocol
There are a few ways you could communicate if a name should be added or removed.
One idea is that when you send a name to the door computer, if it already has the name, it deletes it, but if it doesn't have the name, it adds it.
Another way is to send a string like 'add:name' and 'remove:name' then separate the two using Lua's patterns
(I can help you do that if you'd like)
The final way (that I think is reasonable) is to send a table, like this:
{"add" , "name"}
{"remove" , "name"}
So if I already have an existing table, what commands do I use to add or remove one value, without rewriting the table?
Tables are extremely powerful, you might want to read up on them when you get a chance
Obviously you know the basics, but you might want to get more into them
http://www.lua.org/pil/2.5.html
The format you'll want to use for storing names is like this:
local names = { --# You can really call it whatever you want
name1 = true
name2 = true
--# ect
}
Why do they equal 'true'? It makes finding if a name is in the table much faster
--# Adding a name to the table
names[ "name" ] = true
--# Removing a name from the table
names[ "name" ] = nil
--# Checking if a name is in the table
if names[ nameInQuestion ] then
--# Stuff
end
Edited on 12 August 2015 - 01:46 AM
Posted 12 August 2015 - 09:32 PM
Ok I am using OP Terminal Glasses. So if I type "$$names add name" it returns "names add name" as a string.
Is there any way to unserialize that into a table or something?
Is there any way to unserialize that into a table or something?
Edited on 12 August 2015 - 07:32 PM
Posted 13 August 2015 - 05:29 AM
Ok I am using OP Terminal Glasses. So if I type "$$names add name" it returns "names add name" as a string.
Is there any way to unserialize that into a table or something?
Yep, you can do it pretty compactly with string.gmatch and patterns (The pattern is "%S+")
--# Returns a table of the arguments
local function split(sString)
local temp = {}
for i in string.gmatch(sString, "%S+") do
table.insert(temp , i)
end
return temp
end
Posted 16 August 2015 - 02:34 PM
Thanks! I'll reply again later after I work on it some if I have more questions!