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

[resolved] Not sure how to do this...

Started by srentiln, 25 March 2013 - 02:32 PM
srentiln #1
Posted 25 March 2013 - 03:32 PM
Hello!

A little background so that when I say things people don't get overly confused:
I started playing on a feed the beast server where we were looking for a good solution for getting around using the MYSTCraft linking books. Being the borderline MYST-Geek that I am (loved every game in the series and was sad that CYAN Worlds was hit as hard as it was), I designed a Nexus Hub machine that relies on a computer terminal to make selections.

The problem:
The code that I out together doesn't work properly. I took a menu script that I found here and added on some basic code to respond to it.


local function menu(...)
local sel = 1
local list = {...}
local offX,offY = term.getCursorPos()
local curX,curY = term.getCursorPos()
while true do
  if sel > #list then sel = 1 end
  if sel < 1 then sel = #list end
  for i = 1,#list do
   term.setCursorPos(offX,offY+i-1)
   if sel == i then
	print("["..list[i].."]") print(">"..list[i])
   else
	print(" "..list[i].." ")
   end
  end
  while true do
   local e,e1,e2,e3,e4,e5 = os.pullEvent()
   if e == "key" then
	if e1 == 200 then -- up key
	 sel = sel-1
	 break
	end
	if e1 == 208 then -- down key
	 sel = sel+1
	 break
	end
	if e1 == 28 then
	 term.setCursorPos(curX,curY)
	 return list[sel],sel
	end
   end
  end
end
end
local function book(a)
local side = "back"
local color = a
redstone.setBundledOutput(side, color)
sleep(1)
redstone.setBundledOutput(side, 0)
sleep(5)
redstone.setBundledOutput(side, colors.purple)
sleep(1)
redstone.setBundledOutput(side, 0)
end


print("Please select a destination")
local selection = menu(" ")
if selection == " " then
	book()

at the last three lines, the space is where the player names go, using elseif for additional names and the book() being filled by colors.color (example, colors.blue).

After fixing numerous typos in the terminal, I got the menu to come up. However, when I made a selection, it would come up with a command line. Obviously, this was because the program does not have a loop built in, so I added in a "while true do" to try to get it to bring up the menu again after a selection. Instead, it has locked the terminal in the loop and the same wire is turning on and off over and over.

The only other way I can think of to get the system to start over after execution is to somehow tie in a "reboot" since I saved the code as the startup.

What is the "best" way to resolve the issue?
immibis #2
Posted 25 March 2013 - 06:35 PM
To fix it, put the loop in the right place.
Also pastebin your code so we can see what you're talking about more easily.
srentiln #3
Posted 25 March 2013 - 07:14 PM
Never used pastebin for anything before…first time for almost everything I guess. This is the code with the loop that I tried to use to bring the menu back up after execution:

http://pastebin.com/1pnBuyde

I have replaced player names with numbers just in case any of them would have any problems with it.

if this were PHP or C++, I would have the menu as a seperate element and any interaction with it include another file that holds the script to be executed via a switch statement rather than using a loop to return to a menu. The best guess I can think of would be to change the loop to be conditional on the returned selection value and set it to a specific value after each execution. However, if there is a more efficient way to go about it, I would prefer to know it.
Bubba #4
Posted 25 March 2013 - 09:19 PM
I rarely do this because I feel like it encourages laziness and I'm a big proponent of hard work and all that jazz, but I wrote up a program that I hope is something similar to what you are looking for. The reason that I didn't just fix your program is because there were some strange things going on and I wasn't sure exactly where to start (You mentioned PHP and C++ so I'd imagine that some of those things might be old habits from there - Lua is an odd little language that you have to use for a while before getting used to). I've commented my code to help you understand what's going on, and I left it up to you to decide how to use the result that is returned by the program (meaning I did not implement the redpower portion of your code).

I will say one thing about your nexushub code:

local function book(a)
		local side = "back"
		local color = {...}
		redstone.setBundledOutput(side, color) -- stored space line
		sleep(1)
		redstone.setBundledOutput(side, 0) -- reset all lines to no power
		sleep(5) -- give the player time to use it
		redstone.setBundledOutput(side, colors.purple) -- send back to storage space
		sleep(1)
		redstone.setBundledOutput(side, 0)
end

This code is strange. Why does it accept the variable "a" and never use it? Why are you trying to capture arguments inside of a table and then use that table as a number (colors.blue, colors.red, etc. are actually numbers in case you didn't know that already). It looks like you're trying to capture the arguments from the main program (e.g. when you type "nexushub 1 2 3 4" or some such thing in the shell to start it). That doesn't work from inside a function though (although you can capture arguments that are inserted by other functions by putting … inside of the declaration parentheses such as this -> local function book(a, …) ).

I hope this helps.

-Bubba

Edit: A small correction in the program comments:

If the user presses enter, it returns true

Should be…
If the user presses enter, it returns the value of the "selected" variable
srentiln #5
Posted 26 March 2013 - 04:22 AM
…Why does it accept the variable "a" and never use it?…

That's a transfer error. My the actual code puts it

local function book(a)
...
local color = a
...

and for whatever reason I corrected the first part but not the second, heh

as to the "colors. …" part, I am just using the same syntax as is in the wiki api entry. It was while checking to see if that part was working that I saw that the loop was sticking.

You mentioned PHP and C++ so I'd imagine that some of those things might be old habits from there

A combination of having my initial "programing education" from web coding and the fact that I have taught myself everything I know about it. Very easy to get dug into habits with coding when you are using w3 schools tutorials for reference, heh.



Looking over your code, it seems that you might have misinterpreted my intentions. I do appreciate that you took the time to write up a reference example and I can use it as a reference while reworking the program, but unless I'm misinterpreting the drawList function it seems like you thought I was trying to make the listed items colored. The colors are all in reference to the redpower api and the use of bundled cables and insulated wires in order to separate each storage spot. Probably not of interest to you, but I thought I should clarify.

Thanks for the help up to now. I think I'll try the thought I had first just to see if it fixes the problem, and if not just rewrite the whole thing; My syntax is a bit out of step, but what can you expect from a ChemEng who decided to learn a little programming on the side? Heh
Bubba #6
Posted 26 March 2013 - 05:35 AM
Oops! I guess I did misinterpret your code a bit. Sorry about that. But if you wanted to you could actually convert it pretty easily. If you noticed at the bottom I put a comment about using list[result] to get the color of the selected item as opposed to the number that was selected. In that way, you can use the color that is selected on the screen for outputting to a bundled cable :)/>

Looking at your code again I'm able to comprehend it a bit better now that I realize what you want. It should work if you replace the color ={…} thing with color=a
Oh and you are also missing an end to close the while loop at the very bottom.
srentiln #7
Posted 26 March 2013 - 09:08 AM
thanks, I'll have to bug the server owner to make the adjustments from the server console so I can avoid making more errors from typos, heh