This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Codazoa's profile picture

Trying to make a persistent message board

Started by Codazoa, 31 October 2014 - 02:50 PM
Codazoa #1
Posted 31 October 2014 - 03:50 PM
Because our base is not in the overworld the dimension is unloaded when everyone leaves. Then all of our messages get deleted/ wiped. Is there any way to save the the last 4 or 5 messages then display them on a monitor when the computer comes back on?

This is what I have right now

attachedMonitor = peripheral.wrap("top")
attachedMonitor.setTextScale(.5)
attachedMonitor.setTextColor(colors.black)
attachedMonitor.setBackgroundColor(colors.white)
term.clear()
term.setCursorPos(1,1)
write("Username: ")
local user = read()
write("Message: ")
local msg = read()
term.clear()
term.setCurosrPos(1,1)
term.redirect(peripheral.wrap("top"))
print(">"..msg.." ~"..user)
KingofGamesYami #2
Posted 31 October 2014 - 05:18 PM
For this, you will need to use the fs api to save the messages as they are displayed. I would also recommend placing the messages in a table, and the use of textutils.serialize

If you have any questions, please ask them!
Edited on 31 October 2014 - 04:19 PM
DannySMc #3
Posted 31 October 2014 - 05:31 PM
Yes you can by saving it as a table, but what you seem to be doing is very bulky?
try this (not tested):

-- Initialise table
a = {}
-- Initialise monitor
mon = peripheral.wrap("monitor_side")
mon.setTextScale(0.5)
mon.setTextColour(colours.black)
mon.setBackgroundColour(colours.white)
-- lazy way to clear terminal screen
function cs()
term.clear()
term.setCursorPos(1,1)
end
-- constant loop, to update monitor when someone adds a new message, for multi user this is also very easy
while true do
i = 1
for _, v in ipairs do
  mon.setCursorPos(1,1)
  mon.write(v)
  i = i + 1
end
print("> Message: ")
local msg = read()
print("> Your username: ")
local user = read()
newString = msg.." ~ "..user
table.insert(a, newString)
end
-- Code not tested! Should work though! Not got minecraft on me!

try that!

To store it, just use a config api Here is the saveConfig and loadConfig snippets from my api, just use them to save the table and then read it after:


function saveConfig(table, file)
  fConfig = fs.open(file, "w") or error("Cannot open file "..file, 2)
  fConfig.write(textutils.serialize(table))
  fConfig.close()
end
function loadConfig(file)
  fConfig = fs.open(file, "r")
  ret = textutils.unserialize(fConfig.readAll())
  return ret
end

Then instead when you iterate the table it will automatically go to the bottom of the screen if you don't delete comments, so if I was you I would add a way of removing them, although this can be done like this:


x, y = mon.getSize()
if y < #tMessages then
repeat
  table.remove(tMessages, 1)
until #tMessages <= y
end
-- do the for loop here

Put it all together and make sure it works, the part that checks it doesn't write more messages than the monitors size needs to go before the for loop!

Hope this helps, if not email me:
danny.smc95@gmail.com or pm me on here, and I shall make one for you :)/>
Edited on 31 October 2014 - 04:32 PM
Dragon53535 #4
Posted 31 October 2014 - 11:22 PM
All right so danny gave you quite a decent start however i'm sure you've still got some questions on how it works. You can check This tutorial out to see how the fs api is used for saving data. That should help you figure out what danny is doing in his save and load functions.
Codazoa #5
Posted 01 November 2014 - 08:07 AM
thanks for the help. This is the first program I'm trying to make so I'm very new to Lua. I know plenty of C++ so some things are familiar. I'm going to test out the suggestions but if I cant figure them out I will ask for help again.