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

[lua] Need help writing a program

Started by robertcarr22, 08 January 2013 - 10:13 AM
robertcarr22 #1
Posted 08 January 2013 - 11:13 AM
I am trying to copy Direwolf20's powercontrol program.
This is a menu program which he uses to control his security stations and forcefields.
Shown in this video.

I have this so far from copying from what I can see in the videos I copied the two functions and guessed what to do after that.

local currline = 0
local pos
pos = 1
local loctable
local postable
local linetable
linetable = {}
postable = {}
loctable = {}
local event
local keypressed
local countlines
local page
page = 1

function readFile()
  file = io.open("power","r")
  for line in file:lines() do
	currline = currline + 1
	for name, active in string.gmatch(line, "(.+):(/>/>/>%a)") do
	  print(name.."="..tostring(active))
	  loctable[name] = active
	end
  end
  file:close()
end

function printMenu()
  file = io.open("power","r")
  term.clear()
  term.setCursorPos(1,1)
  local currpos
  local wire = 0
  currpos = 1
  countlines = 0
  for line in file:lines() do
  for name, active in string.gmatch(line, "(.+):(/>/>/>%w+)") do
	loctable[name] = active
	countlines = countlines + 1
	wire = redstone.getBundledOutput("bottom")
	if active == "Open" then
  
	  term.setTextColor(colors.lime)
	  wire = colors.combine(wire, 2^countlines)
	
	else
	  term.setTextColor(colors.red)
	  wire=colors.subtract(wire, 2^countlines)
	end
	redstone.setBundledOutput("bottom", wire)
	if currpos == pos then
	  print(name.."="..active.."*")
	else
	  print(name.."="..active)
	end
	postable[countlines] = active
	linetable[countlines] = line
	currpos = currpos+1
  end
  end
  term.setTextColor(colors.white)
  file:close()
end	
	  
readFile()
while true do
  printMenu()
  local event, a = os.pullEvent("key")
  if a == 200 then
	pos = pos - 1
	printMenu()
  end
  if a == 208 then
	pos = pos + 1
	printMenu()
  end
  if a == 28 then
  
	printMenu()
  end
end


Hope you can help me figure this out! :)/>
thanks!
ChunLing #2
Posted 08 January 2013 - 12:58 PM
What seems to be the trouble?
robertcarr22 #3
Posted 09 January 2013 - 07:09 AM
What seems to be the trouble?

I dont know what to do after this to make it work :P/>
remiX #4
Posted 09 January 2013 - 07:16 AM
Can you tell us what goes on within the video, I don't feel like watching a 36 minute video :P/>

Or which part(s) of the video does he code
robertcarr22 #5
Posted 09 January 2013 - 07:24 AM
Ye sorry about that probably should have specified :P/>
if you watch like from 0:45 he shows some of the code and explains it then he demonstrates it :)/>
robertcarr22 #6
Posted 09 January 2013 - 07:27 AM
I have got it to read the file and show correctly the color of whether something is open or closed and the moving up and down works but I dont know how to do the 'press enter to change between open and closed' bit
robertcarr22 #7
Posted 10 January 2013 - 09:33 AM
Still cant figure it out :/ can someone please help?
crazyguymgd #8
Posted 10 January 2013 - 09:37 AM
Still cant figure it out :/ can someone please help?

is the code above still the most recent copy? I'd be happy to take a look but what you have above doesn't work and I don't want to debug it all if you've already done some of that.
robertcarr22 #9
Posted 11 January 2013 - 03:48 AM
Still cant figure it out :/ can someone please help?

is the code above still the most recent copy? I'd be happy to take a look but what you have above doesn't work and I don't want to debug it all if you've already done some of that.

I have changed it a bit but not much.
Heres a pastebin so its easier to download to minecraft: http://pastebin.com/M3Hyj0VW
The moving up and down works but the enter doesn't.

The reason it probably didnt work is cos you need to put this:

Zone 1:Closed
Zone 2:Open
Zone 3:Closed
or something similar in a separate program called 'power' (without quotes :P/>)

currently the code is set up to have only 3 lines due to line 72 in menu but it would be good for it to auto detect how many lines the 'power' file has.
crazyguymgd #10
Posted 11 January 2013 - 05:43 AM
Do you get any errors? After a quick glance on my phone I noticed you use curpos in the last while loop but declare it as a local variable up in the function above. This may be the problem?
robertcarr22 #11
Posted 11 January 2013 - 06:34 AM
Do you get any errors? After a quick glance on my phone I noticed you use curpos in the last while loop but declare it as a local variable up in the function above. This may be the problem?

Nope the moving up and down works, the only thing that doesnt is pressing enter to change from open to closed and vice versa
anonimo182 #12
Posted 11 January 2013 - 07:15 AM
You will need a variable to change every time a user press enter and for every selection, lke selection1 = true, selection2 = false, selection3 = true, and if the user press enter, the selection will inverse in the current pos
crazyguymgd #13
Posted 11 January 2013 - 08:45 AM
Problem I found:
Every time you call printMenu(), you read from the file again, overwriting any data you may have changed if the enter key works, and reprint the old data. So you may be doing that part correctly but you're overwriting it every time through the loop.
robertcarr22 #14
Posted 11 January 2013 - 08:53 AM
Problem I found:
Every time you call printMenu(), you read from the file again, overwriting any data you may have changed if the enter key works, and reprint the old data. So you may be doing that part correctly but you're overwriting it every time through the loop.

Ahhh! so it will! any chance you know how to fix that then :/
crazyguymgd #15
Posted 11 January 2013 - 09:03 AM
Here's what I came up with for you. It uses your general method just cleaned up a bit. Took out some unnecessary variables, made loctable hold all the data so we're not trying to access multiple tables. And some other little things. Here you go!



local loctable = {}
local currline = 0
local pos = 1
local wire = 0

function readFile()
  term.clear()
  term.setCursorPos(1,1)

  local countlines = 1
  file = io.open("power","r")

  for line in file:lines() do
    for name, active in string.gmatch(line, "(.+):(/>%w+)") do
      loctable[countlines] = {["name"] = name, ["active"] = active}
    end
    countlines = countlines + 1
  end
  file:close()
end

readFile()
function printMenu()
  term.setCursorPos(1,1)
  term.clear()
  for i = 1, #loctable do
    wire = redstone.getBundledOutput("bottom")
    if loctable[i].active == "Open" then
      term.setTextColor(colors.lime)
      wire = colors.combine(wire, 2^#loctable)
    else
      term.setTextColor(colors.red)
      wire=colors.subtract(wire, 2^#loctable)
    end
    redstone.setBundledOutput("bottom", wire)

    if i == pos then
      print(loctable[i].name.."="..loctable[i].active.."*")
    else
      print(loctable[i].name.."="..loctable[i].active)
    end  
  end
  term.setTextColor(colors.white)
end      


while true do
  printMenu()
  local event, a = os.pullEvent("key")
  if a == 200 and pos > 1 then
    pos = pos - 1
  end
  if a == 208 and pos < 3 then
    pos = pos + 1
  end
  if a == 28 then

    if loctable[pos].active == "Open" then
       loctable[pos].active = "Closed"
     else
       loctable[pos].active = "Open"
     end  


  end  
end
robertcarr22 #16
Posted 11 January 2013 - 09:12 AM
Here's what I came up with for you. It uses your general method just cleaned up a bit. Took out some unnecessary variables, made loctable hold all the data so we're not trying to access multiple tables. And some other little things. Here you go!

-snip-

thank you so much for this! but… :P/>
when I run it nothing shows up and if i press enter it says

menu2:58: attempt to index ? (a nil value)
crazyguymgd #17
Posted 11 January 2013 - 09:17 AM
I just ran it and it works fine. That error would mean nothing is in loctable at index pos. Did you add the code I gave you to an existing program or run it on it's own. Again, it works for me with no error.
robertcarr22 #18
Posted 11 January 2013 - 09:49 AM
I just ran it and it works fine. That error would mean nothing is in loctable at index pos. Did you add the code I gave you to an existing program or run it on it's own. Again, it works for me with no error.

I've found what was wrong.. on line 14 I changed this:

"(.+):(/>/>%w+)"

to this:


"(.+):(/>%w+)"

and now it works :)/>
Thanks for all your help!! :D/>
crazyguymgd #19
Posted 11 January 2013 - 09:57 AM
I just ran it and it works fine. That error would mean nothing is in loctable at index pos. Did you add the code I gave you to an existing program or run it on it's own. Again, it works for me with no error.

I've found what was wrong.. on line 14 I changed this:

"(.+):(/>/>/>%w+)"

to this:


"(.+):(/>/>%w+)"

and now it works :)/>
Thanks for all your help!! :D/>

Ahh gotcha. Well glad I could help. But try to make sure you understand what I did different from you and think about why I would have chosen to do it. Otherwise you're not really gaining anything ;)/>
robertcarr22 #20
Posted 11 January 2013 - 10:10 AM
Sorry to bother :L

right so now I want it to output the colour of wire that corresponds to the number of the line the zone is on in the power file (e.g. Line 1 = orange wire, Line 2 = Magenta etc.) to the bundled cable. I've changed the code to get this to work but it doesn't output to match the screen until you move the arrow either up or down

code: http://pastebin.com/3CnQ1hw6
crazyguymgd #21
Posted 11 January 2013 - 10:16 AM
you initialize t to 0 and there is no color to 0
Nevermind this. I'm gonna pass off to someone else who knows something about bundledcables :)/> although I'll probably go start playing with them sometime soon :)/>
robertcarr22 #22
Posted 11 January 2013 - 10:30 AM
you initialize t to 0 and there is no color to 0
Nevermind this. I'm gonna pass off to someone else who knows something about bundledcables :)/> although I'll probably go start playing with them sometime soon :)/>

Great thanks! :)/>
crazyguymgd #23
Posted 11 January 2013 - 10:37 AM
So I gave it a bit of thought, and if you add another field to the loctable,
["color"] = 2^countlines 
then when you use colors.combine(wire,loctable.wire) should add the correct color?
robertcarr22 #24
Posted 12 January 2013 - 05:28 AM
So I gave it a bit of thought, and if you add another field to the loctable,
["color"] = 2^countlines 
then when you use colors.combine(wire,loctable.wire) should add the correct color?

Well I have that part working the only problem I have is that it doesnt change as soon as you hit enter only once you move up/down