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

Please help! Storing and displaying info

Started by MorgedwinT, 12 April 2015 - 06:57 PM
MorgedwinT #1
Posted 12 April 2015 - 08:57 PM
Hello
I've been trying to create a simple program to work with lanteaCrafts (a stargate mod) cc integration with which a player can input the name of a gate, and that gates address and have it stored somewhere, then be able to pull up a list of all entered gates and their respective addresses, I've tried a few different things such as attempting to make a table with player input then serialize the table and have it read back, I'm still very new to programming as I've been playing with it for only a week, but I learn fast so please feel free to suggest more advanced techniques than I probably havent thought of.
Thanks!
Lupus590 #2
Posted 12 April 2015 - 10:46 PM
do a search for similar programs and look at their code
MorgedwinT #3
Posted 13 April 2015 - 07:12 AM
Ive spent a good while looking thru other people's programs and figuring out how they work, however in all of my searching I haven't been able to find a program that does something similar to what I described, if you know of one please point me in that direction and I'll gleadly see what I can learn from it, thanks!
Tag365 #4
Posted 13 April 2015 - 07:45 PM
Firstly you can use a table to store multiple variables of any type similarly to an array. The APIs are accessed from a table, in fact the environment is a table too. For more information click here.

Second, to ask the user for input you can use the read function, or you can use the event system.

To store information permanently use the fs API to save and load information on the hard drive. It has a function named open which allows you to open files and then either read or write to them.

To open a file for writing use this code to create a file handle:

local fileHandle = fs.open("stargateHistory", "w")
To open a file for reading lines replace the "w" with "r".

You can then use the fileHandle to add or read data from a file. After you are done reading or writing to the file close it by calling fileHandle.close(). A simple use of this functionality is this:

local gateHistory = {[1] = math.random(4800)}
local fileHandle = fs.open("stargateHistory", "r")
if fileHandle then -- we need to make sure that this is actually a valid handle so we don't throw an error.
	gateHistory = textutils.unserialize(fileHandle.readAll())
	fileHandle.close()
end

gateHistory[#gateHistory + 1] = math.random(4800)

print("Saving new information to file...")
fileHandle = fs.open("stargateHistory", "w")
fileHandle.writeLine(textutils.serialize(gateHistory))
fileHandle.close()

-- Print the values in the gateHistory table
for k, v in pairs(gateHistory) do
	print("Key "..k.." Value: "..tostring(v))
end

Now notice that the table values persist as we are saving it to the hard disk. Of course you can replace the random numbers with the gates that you used or whatever values that fit your application.
Edited on 14 April 2015 - 01:57 PM
Dog #5
Posted 13 April 2015 - 08:05 PM
Here are a number of DHD programs you can look at to get some ideas…

LanteaCraft
Stagate Control by CMaster (no longer developed?)
SGControl by The_Zenith_ (no longer developed?)

SGCraft
SG Control System by SeaLife (no longer developed?)
SuperGate Dialer by LandStryder (no longer developed?)
StarDial by MudkipTheEpic (no longer developed?)
SGCraft DHD by thatParadox (released in Nov 2014) - this is a YouTube video with some explanation peppered throughout

and my DHD programs - ccDHD and ccDHDlite (both actively developed) - both of which work with LanteaCraft and SGCraft
jakejakey #6
Posted 14 April 2015 - 02:31 AM
Want to save a variable do this:
fs.delete("test")
f = fs.open("test","w")
f.writeLine(test)
f.close()
This will save the variable test to a file named test.
To read a variable:
f = fs.open("test","r")
test = f.readAll()
f.close()
this will open a file named test and save it to a variable named test.