Jump to content




[lua] Need help writing a program


23 replies to this topic

#1 robertcarr22

  • Members
  • 21 posts

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!

#2 ChunLing

  • Members
  • 2,027 posts

Posted 08 January 2013 - 12:58 PM

What seems to be the trouble?

#3 robertcarr22

  • Members
  • 21 posts

Posted 09 January 2013 - 07:09 AM

View PostChunLing, on 08 January 2013 - 12:58 PM, said:

What seems to be the trouble?

I dont know what to do after this to make it work :P

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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

#5 robertcarr22

  • Members
  • 21 posts

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 :)

#6 robertcarr22

  • Members
  • 21 posts

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

#7 robertcarr22

  • Members
  • 21 posts

Posted 10 January 2013 - 09:33 AM

Still cant figure it out :/ can someone please help?

#8 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 10 January 2013 - 09:37 AM

View Postrobertcarr22, on 10 January 2013 - 09:33 AM, said:

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.

#9 robertcarr22

  • Members
  • 21 posts

Posted 11 January 2013 - 03:48 AM

View Postcrazyguymgd, on 10 January 2013 - 09:37 AM, said:

View Postrobertcarr22, on 10 January 2013 - 09:33 AM, said:

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.

#10 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

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?

#11 robertcarr22

  • Members
  • 21 posts

Posted 11 January 2013 - 06:34 AM

View Postcrazyguymgd, on 11 January 2013 - 05:43 AM, said:

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

#12 anonimo182

  • Members
  • 252 posts
  • LocationIn the universe

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

#13 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

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.

#14 robertcarr22

  • Members
  • 21 posts

Posted 11 January 2013 - 08:53 AM

View Postcrazyguymgd, on 11 January 2013 - 08:45 AM, said:

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 :/

#15 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

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


#16 robertcarr22

  • Members
  • 21 posts

Posted 11 January 2013 - 09:12 AM

View Postcrazyguymgd, on 11 January 2013 - 09:03 AM, said:

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)


#17 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

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.

#18 robertcarr22

  • Members
  • 21 posts

Posted 11 January 2013 - 09:49 AM

View Postcrazyguymgd, on 11 January 2013 - 09:17 AM, said:

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

#19 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 11 January 2013 - 09:57 AM

View Postrobertcarr22, on 11 January 2013 - 09:49 AM, said:

View Postcrazyguymgd, on 11 January 2013 - 09:17 AM, said:

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 ;)

#20 robertcarr22

  • Members
  • 21 posts

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users