Jump to content




Menu with multiple choiches.

lua

28 replies to this topic

#1 Kustus

  • New Members
  • 19 posts

Posted 29 March 2012 - 10:15 PM

HI

Ok so in technic pack I've built myself a HUGE oilrig thingy with lots of oiltanks and pumps.

I'm making a program to controll all the pumps from 1 terminal.

I made a program for 2 pumps, and it works fine.

but adding mulitple tanks and pumps makes to many possible choiches and variables.

I was hoping to make a nice menu also, but reading the tutorials they are often for 2-3 choices and I 6.

So the way I wanted this was pick a tank, choice to pump into it or from it, and lastly how many seconds it should pump.

came up with this for 1 tank.


Spoiler


#2 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 29 March 2012 - 11:12 PM

You can use tables, which let you reference multiple things with one varaible:

local data = {}

while true do
 print("Input key")
 local key = read()
 if not data[key] then
  print("Input data:")
  data[key] = read()
 else
  print("Data: " .. data[key])
 end
end

You can also use it with functions; I'll find a post and link it, as it has a pretty nice example.

Edit: Here's the example (follow the pastebin link):
http://www.computerc...ndpost__p__2503

Edited by Advert, 29 March 2012 - 11:15 PM.
Added link


#3 Kustus

  • New Members
  • 19 posts

Posted 30 March 2012 - 06:01 AM

Thanks alot! Will do a test of it once I get home from work.

#4 Kustus

  • New Members
  • 19 posts

Posted 30 March 2012 - 03:30 PM

ok, I've been playing around with the script you linked me to.

But i've got another question (suprised?" :o/>

What whould be the best/easiest way to code?

the 4 (for now) oil tanks all have the same options, to either pump to the main oil tank or pump from the main oil tank to the chosen oiltank.

the maintank has the option to pump into the oil refinery (makes fuel in technic pack) and to pump into 3 remote tanks placed closed to some contraptions around the map.
it also has the choice to pump from the Input tank (where all the new oil I find is pump into).

so whats the best way?

1. pick a tank first and then choose the options?
2. pick an option first and then chose witch tank to perform the task to?

my thoughts where that option 1 whould make alot of doubled code (same code over and over).
but im not shure how to go around number 2.

also, its the timing thing I need help with. new to lua and not shure how i can use the table thing to store multiple values to be used to enable the correct redpower signal?

also when I make 4 selections in the menu, (making 5 the auto shutdown) it gives me an error saying Out of Bounds.

#5 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 30 March 2012 - 05:37 PM

View PostKustus, on 30 March 2012 - 03:30 PM, said:

ok, I've been playing around with the script you linked me to.

But i've got another question (suprised?" :o/>

What whould be the best/easiest way to code?

the 4 (for now) oil tanks all have the same options, to either pump to the main oil tank or pump from the main oil tank to the chosen oiltank.

the maintank has the option to pump into the oil refinery (makes fuel in technic pack) and to pump into 3 remote tanks placed closed to some contraptions around the map.
it also has the choice to pump from the Input tank (where all the new oil I find is pump into).

so whats the best way?

1. pick a tank first and then choose the options?
2. pick an option first and then chose witch tank to perform the task to?

my thoughts where that option 1 whould make alot of doubled code (same code over and over).
but im not shure how to go around number 2.

also, its the timing thing I need help with. new to lua and not shure how i can use the table thing to store multiple values to be used to enable the correct redpower signal?

also when I make 4 selections in the menu, (making 5 the auto shutdown) it gives me an error saying Out of Bounds.

You should post your code when you get an error, I have an idea of what it is, but that's just an idea until I see the code :o/>

You can use functions to do the repetetive stuff, then send them arguments to make them do it on different stuff.


*Not tested, but should look something like this. You'd have to add the redstone functions, too.
function toggleTankValve(tank)
 tank.valve = not tank.valve
end
local tanks = {alpha = {valve = false}, beta = {valve = true}, gamma = {valve = true}}

print("Choose a tank")
local tank = tanks[read()]
if not tank then
print("Couldn't find tank.")
return
end
print("Tank status: ", tank.valve)
print("Toggle?")
if read() == "y" then
toggleTankValve(tank)
end


#6 Kustus

  • New Members
  • 19 posts

Posted 30 March 2012 - 06:25 PM

You my good sir are somewhat of a genius. :o/>

Will try to make the entire program now.

but for the error. I did a bad thing. copied and pasted the code from pastebin that u linked.
then i altered the meny options.

    local tChoices = {}
    tChoices[0] = 'Pump from Main Oil Tank'
    tChoices[1] = 'Pump to Main Oil Tank'
    tChoices[2] = 'Pump to a remote remote location'
    tChoices[3] = 'Pump to refinery'
    tChoices[4] = 'Pump from the Fresh Oil Tank'
     
     
    table.insert(tChoices, 'Shutdown') -- If you add more stuff, the shutdown will always come at the bottom, without you having to change its #.

then when I ran the program and tried to go down to the number 5 selection (the shutdown) it said.
selection out of bounds.

dont know if I did a boo boo or if something from the original code is buggy. (I expect the reason for the error is a PEBKAC.)

#7 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 30 March 2012 - 06:29 PM

If you go down a bit, you'll see that each index (0-4 in your case) are mapped to functions that do stuff:

local tActions = {} -- Twin table of tChouices, we'll put functions in here to do stuff.



tActions[0] = function() -- Stuff a function in here!
	term.clear()
	term.setCursorPos(1, 1) -- Reset the cursor's position to be safe
	print(sSeperator)
	print("|                          Terminal Password                             |")
	print(sSeperator)
	print("Information Deleted By User")
	term.setCursorPos(1, nTermY) -- Move the cursor to the bottom
	write("    [Press enter to return to main menu]")
	read()
end

tActions[1] = function()
	term.clear()
	term.setCursorPos(1, 1)
	print(sSeperator)
	print "|                                Mining Reports                           |"
	print(sSeperator)
	print "Today I mined 32 iron in a deep mine under my"
	print "house. I got attacked by a zombie on my way out"
	print "of the mine. I had to drop all my iron so I could"
	print "Run fast enough to outrun the zombie."

	term.setCursorPos(1, nTermY) -- Move the cursor to the bottom
	write("    [Press enter to return to main menu]")
	read()
end
...

You'll need to modify those to do what you want with your pump, and add any missing ones.

#8 Kustus

  • New Members
  • 19 posts

Posted 30 March 2012 - 08:41 PM

ok the thing is coming together nicely now.

But I got one last question, (no promise)

While the tanking takes place, I have a sleep time defined by a variable.
Is it possible to make a "progress" bar based on the sleep-time variable?

like say for x seconds it whould show a progress bar that used x seconds to go to full?

Reason for this is that if you pump a long time u might want to se that something actually is happening rather then just a blank screen or whatever with no indication that something is happening?

Just think it whould make the program look a while lot nicer. :o/>

and again, thanks for all the help so far!

#9 Kustus

  • New Members
  • 19 posts

Posted 30 March 2012 - 09:02 PM

ok I think I might know how it could work, but dont know how to code it.

something like this?

write("pump for how many seconds?: ")
  t = io.read()

  t = tonumber(t)

but now I was thinking dividing the t variable by 10 and then make a function that repeated a loop 10 times with a print and a sleep where the total sleep time was equal to the t variable? and just make a print with that adds a line each time its run.
something like this maybe:


print("|-		 |")
print("|--		|")
print("|---	   |")
print("|----	  |")
print("|-----	 |")
print("|------	|")
print("|-------   |")
print("|--------  |")
print("|--------- |")
print("|----------|")

edit: ok the print is all screwed up here. but you get the idea..

but how to make it add in each print and how to make the sleep time and all that? no idea.

#10 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 30 March 2012 - 09:10 PM

How long does the pump pump?

You could try something like this:
-- Don't use this, it's broken and all wrong :o/>/>
function sleepWithPercentOutput(n)
 local startTime = os.clock() -- get the tick we start in
 local timer = os.startTimer(n) -- start the timer we'll use for determining when to exit
 local cursorx, cursory = term.getCursorPos()
 for sEvent, t in os.pullEvent() do
  term.setCursorPos(cursorx, cursory) -- reset the cursor position
  term.write("    ") -- clearing the space
  term.setCursorPos(cursorx, cursory) -- get into position for printing below; doing this before checking if to exit to clear the %  sign
  if sEvent == "timer" and t == timer then
   break
  end
  local percent = tostring(os.clock() - startTime / n)
  percent = #percent == 3 and percent or (#percent == 2 and ("0" .. percent) or "00" .. percent) -- make it 3 characters long the weird way
  term.write(percent .. "%")
 end
end

Testing the code I wrote now, but I think it should work.

*Won't actually show a progress bar, but will tell you how many %. You could add in the remaining seconds if you wanted to, too.

Edit: I derped; this is completely broken :o/>.

Will fix it in a few.

Edit: Fixed:

function sleepWithPercent(n)
 local startTime = os.clock() -- Get the starting time
 local timer = os.startTimer(n) -- Create a timer so we know when to stop sleeping
 local cursorx, cursory = term.getCursorPos() -- Save the cursor position to avoid spamming the terminal
 local tick = os.startTimer(0) -- Create another timer so we can update the % shown on the terminal
 for sEvent, t in os.pullEvent do -- Loop, with the results of os.pullEvent as sEvent and t
  if sEvent == "timer" and t == timer then -- Check for the exit timer
   term.setCursorPos(cursorx, cursory) -- Clean the screen
   term.write("    ")
   term.setCursorPos(cursorx, cursory)
   break -- Break out of the loop
  elseif sEvent == "timer" and t == tick then -- Check for the update timer
   term.setCursorPos(cursorx, cursory)
   tick = os.startTimer(0.1) -- Re-start the timer since timers only run once
   local p = tostring(math.floor(((os.clock() - startTime) / n) * 100)) -- Calculate the %age
   p = #p == 3 and p or (#p == 2 and ("0" .. p) or "00" .. p) -- Pad with 0s infront if it's not long enough
   term.write(p .. "%")
  end
 end
end

-- Use it like so:
print("Taking over world, please wait...")
term.write("Progress: ")
sleepWithPercent(20)
print("Attempt failed. Your local authorities have been notified.")


#11 Kustus

  • New Members
  • 19 posts

Posted 30 March 2012 - 09:27 PM

the pumps go for about 30 seconds to 2 minutes?



Ok i tested putting this into my original tank program to se how it works, it dosn't. :o/>

I called it test and adjusted it to fit the program (I think?)

get the error:

test:3: bad argument: double excpected, got nil

here's the program.
Spoiler


Do you see the error? couse I read trough the function you made, Don't grasp it completly but understand how it works, copied it right into my program.



edit: aha, a derp. :o/> no wonder it didn't work. :)/> thought i had messed up a copy and paste code. XD

#12 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 30 March 2012 - 09:35 PM

View PostKustus, on 30 March 2012 - 09:27 PM, said:

the pumps go for about 30 seconds to 2 minutes?



Ok i tested putting this into my original tank program to se how it works, it dosn't. :o/>

I called it test and adjusted it to fit the program (I think?)

get the error:

test:3: bad argument: double excpected, got nil

here's the program.
Spoiler


Do you see the error? couse I read trough the function you made, Don't grasp it completly but understand how it works, copied it right into my program.



edit: aha, a derp. :o/> no wonder it didn't work. :)/> thought i had messed up a copy and paste code. XD

Indeed, a major one. I've fixed it now, though :)/>

#13 Kustus

  • New Members
  • 19 posts

Posted 30 March 2012 - 09:41 PM

Ok, MY GOD you are awesome!

Not going to hassle you, but the timing is off. a input of 10 seconds took 21 seconds to complete.

and input of 20 seconds took 21 seconds to complete,

So i thought it was just to much code to keep up. BUT

input of 30 seconds also took 21 seconds. :o/>

but it looked AWESOME!

edit: me so stupid... I'm guessing the line
sleepWithPercent(20)

means 20 seconds.. so the t variable should be instead of the numbers? right?



second edit: nope, stil takes 21 seconds to complete, no matter what I type. :o/>

#14 Kustus

  • New Members
  • 19 posts

Posted 30 March 2012 - 09:47 PM

feels like I'm standing in front of a beautifull nude lady just waiting for me, but I keep bumping into an invisible wall.
know where u want to go and what to do, just pondering howto. :o/>


Edit: Ok I tried the code for 2 minutes, and its pretty close to accurate,

but below a certain point it wont go, trying to find the threshold
, BUT IT WORKS!

a sincere thank you from norway!

it was the sleepWithPercent(20) that fooled me to long. asumption is the mother of all f-ups.

works like a charm.

adding you to the credits list at the end of the movie about me when I become famous. :o/>

#15 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 30 March 2012 - 09:55 PM

Hmm, it's dependant on minecraft time, so the timing might be off a bit (this runs assuming 20 ticks per second -- the server may 'skip' some, but I'm not sure how that works, exactly.)

I'll test some.

Another thought: Could you paste the code you're using?

Edit: I've timed it twice, once at 20 seconds, and another at 2, and I got 20.04 seconds on the first (using a stopwatch), 1.94x on the second, so it seems to be working for me. Judging from your example, I think you're forgetting to pass the number of seconds to sleep to the function (you use it just like sleep(), but it'll print a 4-letter long string with the %age done)

#16 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 30 March 2012 - 10:06 PM

View PostKustus, on 30 March 2012 - 09:47 PM, said:

feels like I'm standing in front of a beautifull nude lady just waiting for me, but I keep bumping into an invisible wall.
know where u want to go and what to do, just pondering howto. :o/>


Edit: Ok I tried the code for 2 minutes, and its pretty close to accurate,

but below a certain point it wont go, trying to find the threshold
, BUT IT WORKS!

a sincere thank you from norway!

it was the sleepWithPercent(20) that fooled me to long. asumption is the mother of all f-ups.

works like a charm.

adding you to the credits list at the end of the movie about me when I become famous. :o/>

Happens to the best of us :)/>

No problem at all; glad I could help :)/>

#17 Kustus

  • New Members
  • 19 posts

Posted 30 March 2012 - 10:08 PM

Coming right up, just gotta get in the last few bits and pieces. :o/>

OK, I went over the code.
The main menu works, but the second menu I made comes up, but when I select a tank it goes back to the main menu. Can you se the error? (kinda like where's Waldo?"

but this is how its looking so far.
Spoiler


#18 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 30 March 2012 - 11:20 PM

You could convert this menu into a function, that'd make adding sub-menus very easy.

I think I'll have a go at it :o/>

#19 Kustus

  • New Members
  • 19 posts

Posted 30 March 2012 - 11:28 PM

nice! if u ever need a kidney, let me know. U just saved my easter holliday. been trying to figure this out for 4 days now. 2 allnighters with 8 hours of work each day.

just saying, looking forward to the result. But I'm hitting the sack now.

#20 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 31 March 2012 - 12:56 AM

I have something coming along nicely, but I'm starting to create utility functions, as usual :o/>

Here's what I have so far: http://pastebin.com/MsAEDu6C

I think It's working now, I'll make a post with some examples once I'm awake again :o/>

Edited by Advert, 31 March 2012 - 03:56 AM.
Finished(ish)






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users