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

Importing Code edited with notepad++

Started by Rext, 24 July 2012 - 12:50 AM
Rext #1
Posted 24 July 2012 - 02:50 AM
So I recently made this ToDo list code for my server. However this is my first computercraft code so I need some help with things. First how can I display this list on a monitor? Secondly instead of writing an "if" statement for every key is there a way to use the key number to deternme the command?(if you look at the code you will understand what I mean) And lastly can you import code from Notepad++ onto a server like you can do in single player? or do you have to type it all out?


function yN()																   --declare a function (note that I am not actually runningit anywhere)
local n=1																				 --declares the starting selected option (a 'yes')
while true do																	-- a loop, necessary for going from step4 to step 1
term.clearLine()																--clears one line for writing
if n==1 then write("[YES]   NO") else write (" YES   [NO]") end --writes options with one highlighted
a, b=os.pullEvent("key")								   --waits for a key press
if b==203 and n==2 then n=1 end --if pressed arrow left and selected option is 'no' then select 'yes'
if b==205 and n==1 then n=2 end --if pressed arrow right and selected option is 'yes' then select 'no'
if b==28 then print("") break end		 --if pressed enter then break the loop and execute the code outside it
end																								--close the loop code
if n==1 then return true end					 --if chosen 'yes' then return true
if n==2 then return false end				   --if chosen 'no', return false
end																								--close the function

local line01 = "01: "
local line02 = "02: "
local line03 = "03: "
local line04 = "04: "
local line05 = "05: "
local line06 = "06: "
local line07 = "07: "
local line08 = "08: "
local line09 = "09: "
local line10 = "10: "
function Main()
term.clear()
term.setCursorPos(1,1)
print("--------------------To Do--------------------")
print(line01)
print(line02)
print(line03)
print(line04)
print(line05)
print(line06)
print(line07)
print(line08)
print(line09)
print(line10)

print("Press any #(1-10) to change item.")
while true do
a, b=os.pullEvent("key")  
if b == 2 then
term.clear()
term.setCursorPos(1,1)
local linenum = b - 1
print("Do you want to edit Item " ..linenum.. "?")
if yN() == true then
print(line01)
print("New Item:")
local new = read()
print("Are you sure you want to change Item " ..linenum.. "?")
if yN() == true then
line01 = "01: " ..new
Main()
else
Main()
end
else
Main()
end
end
if b == 3 then
term.clear()
term.setCursorPos(1,1)
local linenum = b - 1
print("Do you want to edit Item " ..linenum.. "?")
if yN() == true then
print(line02)
print("New Item:")
local new = read()
print("Are you sure you want to change Item " ..linenum.. "?")
if yN() == true then
line02 = "02: " ..new
Main()
else
Main()
end
else
Main()
end
end
if b == 4 then
term.clear()
term.setCursorPos(1,1)
local linenum = b - 1
print("Do you want to edit Item " ..linenum.. "?")
if yN() == true then
print(line03)
print("New Item:")
local new = read()
print("Are you sure you want to change Item " ..linenum.. "?")
if yN() == true then
line03 = "03: " ..new
Main()
else
Main()
end
else
Main()
end
end
if b == 5 then
term.clear()
term.setCursorPos(1,1)
local linenum = b - 1
print("Do you want to edit Item " ..linenum.. "?")
if yN() == true then
print(line04)
print("New Item:")
local new = read()
print("Are you sure you want to change Item " ..linenum.. "?")
if yN() == true then
line04 = "04: " ..new
Main()
else
Main()
end
else
Main()
end
end
if b == 6 then
term.clear()
term.setCursorPos(1,1)
local linenum = b - 1
print("Do you want to edit Item " ..linenum.. "?")
if yN() == true then
print(line05)
print("New Item:")
local new = read()
print("Are you sure you want to change Item " ..linenum.. "?")
if yN() == true then
line05 = "05: " ..new
Main()
else
Main()
end
else
Main()
end
end
if b == 7 then
term.clear()
term.setCursorPos(1,1)
local linenum = b - 1
print("Do you want to edit Item " ..linenum.. "?")
if yN() == true then
print(line06)
print("New Item:")
local new = read()
print("Are you sure you want to change Item " ..linenum.. "?")
if yN() == true then
line06 = "06: " ..new
Main()
else
Main()
end
else
Main()
end
end
if b == 8 then
term.clear()
term.setCursorPos(1,1)
local linenum = b - 1
print("Do you want to edit Item " ..linenum.. "?")
if yN() == true then
print(line07)
print("New Item:")
local new = read()
print("Are you sure you want to change Item " ..linenum.. "?")
if yN() == true then
line07 = "07: " ..new
Main()
else
Main()
end
else
Main()
end
end
if b == 9 then
term.clear()
term.setCursorPos(1,1)
local linenum = b - 1
print("Do you want to edit Item " ..linenum.. "?")
if yN() == true then
print(line08)
print("New Item:")
local new = read()
print("Are you sure you want to change Item " ..linenum.. "?")
if yN() == true then
line08 = "08: " ..new
Main()
else
Main()
end
else
Main()
end
end
if b == 10 then
term.clear()
term.setCursorPos(1,1)
local linenum = b - 1
print("Do you want to edit Item " ..linenum.. "?")
if yN() == true then
print(line09)
print("New Item:")
local new = read()
print("Are you sure you want to change Item " ..linenum.. "?")
if yN() == true then
line09 = "09: " ..new
Main()
else
Main()
end
else
Main()
end
end
if b == 11 then
term.clear()
term.setCursorPos(1,1)
local linenum = b - 1
print("Do you want to edit Item " ..linenum.. "?")
if yN() == true then
print(line10)
print("New Item:")
local new = read()
print("Are you sure you want to change Item " ..linenum.. "?")
if yN() == true then
line10 = "10: " ..new
Main()
else
Main()
end
else
Main()
end
end
end

end
Main()


Also there is an odd bug where if you hit any key in my menu it moves across the screen instead of staying in one place.
flaminsnowman99 #2
Posted 24 July 2012 - 02:57 AM
To display it on a monitor, in your computer type
monitor "side" "program"
And unless you have access to the Server files you will need to type this all out
Noodle #3
Posted 24 July 2012 - 03:51 AM
1: Monitor "side" "program"
2: I Don't See what you mean?
3: Pastebin put "program"
4: (Efficiency) You don't need "== true" just do if yN() then
Darky_Alan #4
Posted 24 July 2012 - 08:24 AM
Pro tip from a noob. SPACING IS IMPORTANT, use sublime as an editor rather than notepad++ It's very helpfull and does most spacing automatically.
http://www.sublimetext.com/

Spacing your code makes it easier to read and keep track of, it also makes it looks cleaner.

Sure I mean in-game spacing won't really be worth shit, but to have other people look at it it helps a lot.
Noodle #5
Posted 24 July 2012 - 08:35 AM
You can upload to your computer using the pastebin program if you use notepad ++ or sublime or any text editor.
It also translates in game, a TAB = a space but its still clean.
Rext #6
Posted 24 July 2012 - 12:33 PM
1: Monitor "side" "program"
2: I Don't See what you mean?
3: Pastebin put "program"
4: (Efficiency) You don't need "== true" just do if yN() then

Thanks so much for all your help everyone. What I mean is that instead of doing
If b == 1 then
if b == 2 then

All the way through 11
Can't I do like

If b > 12 then
Print(line(b-1))

Or something like that
Noodle #7
Posted 24 July 2012 - 12:34 PM
Nope, all if's.. No easier way.
Rext #8
Posted 24 July 2012 - 12:34 PM
Nope, all if's.. No easier way.

Damn
Rext #9
Posted 24 July 2012 - 12:39 PM
Wait so for the monitor is there an easy way I can make it always display the list but you can edit it on the comp. Like do I have to use the monitor command outside of my program or can I run it internally?
Noodle #10
Posted 24 July 2012 - 12:42 PM
I think you can do monitor "side" "program" and it'll work with loops also.
Rext #11
Posted 24 July 2012 - 03:24 PM
I think you can do monitor "side" "program" and it'll work with loops also.
When I do
monitor"back""Todo"
its will say
No such program.
but if i run Todo it works just fine but nothing is displayed on the monitor. What am I doing wronge?
Lyqyd #12
Posted 24 July 2012 - 03:57 PM
You can get rid of most of those if statements that do almost-the-same-thing by storing the line data in a table. I can write up an example for you this afternoon.
Rext #13
Posted 24 July 2012 - 05:29 PM
Thanks for all the help guys! I got it working and it looks like this!


local list={
"1: ",
"2: ",
"3: ",
"4: ",
"5: ",
"6: ",
"7: ",
"8: ",
"9: ",
"10: "
}
local mon = nil
------------------------------------------------------------------------------
for _,s in pairs(rs.getSides()) do
	if peripheral.getType(s) == "monitor" then
		mon = peripheral.wrap(s)
		break
	end
end
------------------------------------------------------------------------------
function display(m)
if mon then
	mon.clear()
	mon.setCursorPos(1,1)
	term.clear()
term.setCursorPos(1,1)
	mn=#m
mon.write(" -----To Do-----")
	mon.setCursorPos(1,2)
for i=1, mn, 1 do
	mon.setCursorPos(1,i+1)
	mon.write(m[i])
	print(m[i])
end
print("Press any #(1-10) to change item.")
else
	print("No monitor attached!")
	sleep(1)
end
end
------------------------------------------------------------------------------
function CUI(m,text)
n=1
l=#m
while true do
term.clear()
term.setCursorPos(1,2)
for i=1, l, 1 do
if i==n then print(i, " > "..m[i]) else print(i, "   ", m[i]) end
end
print(text)
print("Use Up/Down arrows")
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>=1 and n<=l then n=n-1 end
if b==208 and n<=l and n>=1 then n=n+1 end
if b==200 and n<1 then n=l   end
if b==208 and n>l then n=1   end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
------------------------------------------------------------------------------
function edit()
a, b=os.pullEvent("key")  
term.clear()
term.setCursorPos(1,1)
if b>0 and b<12 then
local k=b-1
local options={
"Yes",
"No"
}
local menu=CUI(options,"Edit Item " ..k.. "?")
if menu ==1 then
print("Type new item.")
print(list[k])
local new=read()
local options={
"Yes",
"No"
}
local menu=CUI(options,"Change Item " ..k.. "?")
if menu ==1 then
list[k]=k.. ": " ..new
Main()
else Main()
end
Main()
else Main()
end
Main()
else Main()
end
end
------------------------------------------------------------------------------
function Main()
display(list)
while true do
edit()
end
end
------------------------------------------------------------------------------
Main()


And that's without a million if statements.
Rext #14
Posted 24 July 2012 - 05:31 PM
You can get rid of most of those if statements that do almost-the-same-thing by storing the line data in a table. I can write up an example for you this afternoon.

Thanks, but I figured it out myself :)/>/>
Noodle #15
Posted 24 July 2012 - 08:25 PM
Don't use the double quotes. You seemed to have figured it out, by using the Peripheral API.
AaronIceGem #16
Posted 08 February 2013 - 10:14 PM
d
Edited on 03 January 2014 - 10:47 AM
theoriginalbit #17
Posted 08 February 2013 - 10:25 PM
Sorry if I'm gravedigging this thread, but I cannot make a new thread myself. I can't import my code to a disk. I save the script to saves/worldname/computer/disk/the disk number, but the problem is, when I go ingame and try to read the disk, (I go to my computer with the disk drive and disk inserted) and do ls/disk and it says "Program not found" which was the same with ROM. I've made multiple floppies and saved the file to all of them, but none of them work. Any help is much appreciated, if you need more info just ask. Thanks!
Good that you know not to but please do not post on an old topic with a new question. Well actually don't post on an old dead topic all together. I know you are new and can't post, we hear that reason all the time, but if you read the bold text in this stickie, which is forum guidelines then you would have been lead to this stickie, which is in this very "Ask a Pro" section which you posted!

but the problem is type "ls /disk" not "ls/disk" (that way looks for a program called "disk" in the folder "ls")