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

Need help with Tables/Shortening the script

Started by ArchAngel075, 18 August 2012 - 03:41 PM
ArchAngel075 #1
Posted 18 August 2012 - 05:41 PM
Firstly i apologize at my poor setting out and any random 'print' lines since they are for debugging

Im trying to create a 'LCD' display, its currently only 4x4 pixels using binary and RP bundled wires(since the colored wires use binary)
Im trying to make it load an near infinite amount of "frames" and allow you to edit these frames easily, so as to set what pixel lights up when that frame is run.
IE: using this binary order of 1;2;4;8;16;31;64;128;256 ETCETC
frame 1 is binary 1 = so only the top most left pixel lights up
frame 2 is binary 2 = so pixel 1x2 lights up(next to pixel one on right)
frame 3 is binary 65535 = All pixels light up (sum of all the binary values)

and if played back with a speed of a frame every 0.1s it lights them in that order.

Lastly allowing you to on the fly edit what binary to use in that frame.
IE: after editing the frames up to 500 you use a slow play back function(frame rate = frame ever 2 seconds)
You notice frame 400 is not set correctly. Thus you enter in command "edit" then choose frame 400 and edit it to its correct value.

Since i have made this already its ok if i dont get help but because i wat to go bigger than 4x4 using a idea i have i want the code to be shorter(original code for working design = 700+ lines that are a monster of copy paste)

After looking up various ways to shorten it i discovered tables.
Ive never used them before so it was tough but I have a idea.
SO lastly: I have made this code in 3 hours so, its a beta in my eyes but once completed i just add it to my LCD screen computer:

WARNING:
Im not sure if spoilers exist so it will be a wall Of text>(again im not in habit to layout my work since i can follow it easily i apologize)

–[[START]]–


local Frame = {K}
Frame[2] = 7
G = 1
K = 1
Frame[K] = G
print(Frame[1])
print("")
G = 2
K = K + 1
Frame[K] = G
print(Frame[2])
print(Frame[K])
K = 3
Frame[K] = 16
sleep(2)
K = 0
LAST = 0
BIT = 0

--[[NEW FUNCTION]]--

function DEBUG() -- Debug command to force a frame to have a value for testing
   K = io.read()
   Frame[K] = io.read()
   print("DONE!")
   sleep(1)
     for K = 1,100 do
	    print(Frame[K]
	 end
MMENU()
end
 
--[[NEW FUNCTION]]--

function EDIT() -- this is for when you wish to edit the frame.
    print("Please input the Frame number you wish to Edit, Use 'exit' to return to menu : ")
    K = io.read() -- as mentioned it asks for the frames number ie frame 400
	    if K == "exit" or K == "Exit" then -- just checking if you wish to stop editing frames
		    MMENU()
			    else
				    LAST = K
				    print(K)
				    sleep(1)
				    print("Now please input the BIT to use for the frame "..K) -- this is the binary value, ie frame 400 BIT value is 64.
		  		  BIT = io.read()
		  		  print("The program will now save this value!")
		  		  sleep(0.7)
--	   		  Frame[K] = BIT --I found this caused some trouble, but its still here incase i need to enable it
		 		   term.clear()
		 		   term.setCursorPos(1,1)
		 		   print("The last frame was "..LAST.." with a BIT of "..BIT) -- for when you have to edit a long series of frames
		  		  sleep(1)
		  		  EDIT()
	    end
end

--[[NEW FUNCTION]]--

function MMENU()
    term.clear()
    term.setCursorPos(1,1)
    print("Welcome to the FrameIT!")
    print("--------------------------------------")
    print("The last Frame was "..LAST.." with a BIT of "..BIT)
    print("--------------------------------------")
    print("Please Input your command : ")
    print("")
    print("Edit --- Edit the frames and thier BIT values")
    print("Play --- Run the current stored video")
    Com = io.read()
	    if Com == "Edit" or Com == "edit" then
	    EDIT()
		    elseif Com == "Play" or Com == "play" then
			    PLAYB()
		    elseif Com == "Exit" or Com == "exit" then
			    print("Goodbye")
			    sleep(1)
			    term.clear()
			    term.setCursorPos(1,1)
		    elseif Com == "debug" then -- this is merely because i wanted to test a method i thought i messed up
			    DEBUG()
		    else
		    MMENU()
	    end
end
 
--[[NEW FUNCTION]]--

function PLAYB() -- function that runs through the frames
    term.clear()
    term.setCursorPos(1,1)
    print("Please input the start frame : ") -- for testing frames it will run the frames from X to Z specified by you to help with the debugging "movies"
    MIN = tonumber(io.read())
    term.clear()
    term.setCursorPos(1,1)
    print("Please input the last frame to play until : ")
    MAX = io.read()
   print(MAX)
	 sleep(3)
    term.clear()
    term.setCursorPos(1,1)
    print("The Play back will be from "..MIN.." to "..MAX)
    K = 0
    M = MIN
    Frame[K] = 0
    PLAYB2()
end
 
--[[NEW FUNCTION]]--

function PLAYB2() --this is where the frames are run as a movie
    K = 0
	    if M < MAX then -- I apologize for the MANY prints they are simply for debugging, i hope you can make sense of it all.
		    print("Frame is >> "..Frame[K])
		    K = K + 1 --[[just so that the loop will run through every frame by setting the current frame to run = last frame + 1.The binary for frames are stored as
Frame[n] where n is the frame number.]]
		    M = M + 1
		    print(K.." ==this is K ")
		    print("------- "..K)
		    print(M.. " <<THIS IS M") --More debug messages
		    sleep(3)
		    rs.setBundledOutput("back",Frame[K]) --the program says i am using nil after the 4th frame(the frames 1-4 are already preset for debugging)
		    print("this is M>>".. M) -- debugginjg
		    print(MAX) --debug to make sure it wont go over MAX frame set
		    sleep(3)
		    PLAYB2() --loop itself
	    else
	    print("END")
	    sleep(1)
	    MMENU()
    end
end

MMENU() -- Just so that everything runs after setting up the functions etc.

–[[END]]–

NOTE
Simply put its not storing the Frame[n] values, because when it runs the non preset values(frames 5+) it errors with a 'nil'

The program can be run without any contraptions, since it only outputs redstone, for testing purposes I suggest making a Bundled cable attach to back and run 8 ascending(white,orange etc) colored wires and connect each wire to its own lamp in line.

If any further information is needed ill try to provide.
Noodle #2
Posted 19 August 2012 - 05:23 AM
Could you organize the code a bit better (in the forum)? Its hard to read which is probably why nobody has replied?
Maybe add the code tags around the actual code?
ArchAngel075 #3
Posted 19 August 2012 - 10:04 AM
Yes, i woke up this morning right now and decided i should fix it up some.>

EDIT:
Fixed up, added spaces and such and CODE tags, also around the 2 last functions are the best spaced since that when i broke out the "4Spaces" macro.
ArchAngel075 #4
Posted 24 August 2012 - 08:39 PM
SOLVED

Funny enough it looked like a missing 'local' or i had been setting my "Frame[n] = io.read()" incorrectly when it should have been "n = io.read() NEWLINE "Frame[n] = n" Or something like that.

Now i gotta figure out how to make my new program run more since testing shows it has a 125 frame limit and on a 125 frames setting it can only play back all 125 frames around 5 times before sending an "178" error. Most likely to be a overload of somesort. Looking into how i run my loops. Once fixed a new thread will be made showing how to make the 4x4 display using RP2 and optionally Wireless redstone.