25 posts
Posted 01 April 2013 - 11:42 AM
When testing computercraft programs a lot of the programs have screens like this:
blahblahblahblahinfoinfoinfoinfo
blahblahblahblahinfoinfoinfoinfo
blahblahblahblahinfoinfoinfoinfo
blahblahblahblahinfoinfoinfoinfo
blahblahblahblahinfoinfoinfoinfo
press any key to continue
and
>Option1
Option2
Option3
Press enter to select which option.
I was wondering how to do the "Press any key to continue" and the Scroll then hit Enter things, that is mainly used for menu's.
2088 posts
Location
South Africa
Posted 01 April 2013 - 12:17 PM
148 posts
Posted 01 April 2013 - 01:09 PM
Why so complicated?
write("Press any key to continue")
os.pullEvent("key")
That does exactly what he wants to have
And to the Menue thingy:
local options = {
{"Option1", func1},
{"Option2", func2},
{"OptionX", func3}
}
local selected = 1
function draw()
--Set the position for the first option. Change the second number if you want a headline or so...
term.setCursorPos(1, 1)
--Iterate over any option and print then on the screen
for ind, param in ipairs(options) do
--Clear line
term.clearLine()
--Print with or without marker
if selected == ind then
print(">"..param[1])
else
print(" "..param[1])
end
end
end
function run()
while true do
--Pull the event
local _, key = os.pullEvent("key")
if key == keys.up and selected > 1 then
--Decrement selected.
selected = selected - 1
elseif key == keys.down and selected < #options then
--Increment selected.
selected = selected + 1
elseif key == keys.enter then
--Run the function saved in the second slot of the currently selected option.
options[selected][2]()
break
end
--Redraw the screen
draw()
end
end
draw()
run()
2088 posts
Location
South Africa
Posted 01 April 2013 - 01:15 PM
Didn't he want it to print a menu and ask for press any key to continue and the menu continues… lol woops
25 posts
Posted 01 April 2013 - 01:26 PM
More complicated than I expected
148 posts
Posted 01 April 2013 - 01:33 PM
Why isn't CC showing me that someone already responsed to that… Just wrote the menue part :D/>
(It's in the same post)
25 posts
Posted 01 April 2013 - 03:19 PM
Why so complicated?
write("Press any key to continue")
os.pullEvent("key")
That does exactly what he wants to have
And to the Menue thingy:
local options = {
{"Option1", func1},
{"Option2", func2},
{"OptionX", func3}
}
local selected = 1
function draw()
--Set the position for the first option. Change the second number if you want a headline or so...
term.setCursorPos(1, 1)
--Iterate over any option and print then on the screen
for ind, param in ipairs(options) do
--Clear line
term.clearLine()
--Print with or without marker
if selected == ind then
print(">"..param[1])
else
print(" "..param[1])
end
end
end
function run()
while true do
--Pull the event
local _, key = os.pullEvent("key")
if key == keys.up and selected > 1 then
--Decrement selected.
selected = selected - 1
elseif key == keys.down and selected < #options then
--Increment selected.
selected = selected + 1
elseif key == keys.enter then
--Run the function saved in the second slot of the currently selected option.
options[selected][2]()
break
end
--Redraw the screen
draw()
end
end
draw()
run()
Could you explain this to me step by step, sorry. And could you tell me where to add the code for each of the options, where it goes to.
2088 posts
Location
South Africa
Posted 01 April 2013 - 03:24 PM
Could you explain this to me step by step, sorry. And could you tell me where to add the code for each of the options, where it goes to.
Did you take a look at mine?
Add more elseif statements in the while true do loop with each option selection and what it will do
148 posts
Posted 02 April 2013 - 01:44 AM
Could you explain this to me step by step, sorry. And could you tell me where to add the code for each of the options, where it goes to.
Okay, I'll try :D/>
#1 Defining a table that contains all options.
Spoiler
local options = {
{"String_Option1", function_Option1},
{"String_Option2", function_Option2},
{"String_OptionX", function_OptionX}
}
The idea behind this is that every numerical index in the table options represents one option.
Each option has a string representing what is going to be written to the screen and a function that is going to be called when the option was chosen.#2 Selected index
Spoiler
local selected = 1
Self explaining.
Its an integer containing the currently selected index#3 Function to draw the options to the screen
Spoiler
function draw()
term.setCursorPos(1, 1) --[1]
for ind, param in ipairs(options) do --[2]
term.clearLine() --[3]
if ind == selected then --[4]
print(">"..param[1]) --[5]
else
print(" "..param[1]) --[6]
end
end
end
[1] Set the cursor to the start of the menue.
[2] Iterate over any numerical index in the table options.
for ind, param in ipairs(options) do
...
end
-- does the same as
for ind = 1, #options do
param = options[ind]
end
[3] Clear the current line. (Print wil start a new line after it wrote it's parameters to the screen)
[4] If the index in options we're about to print to the screen is the selected ind then … else … end
[5] If it is the selected index print > and then the string in param[1], so options[ind][1]
[6] If it is not the selected index print and then the string in param[1]#3 Wait for input
Spoiler
function run()
local _, key = os.pullEvent("key") --[1]
if key == keys.up and selected > 1 then --[2]
selected = selected - 1
elseif key == keys.down and selected < #options then --[3]
selected = selected + 1
elseif key == keys.enter then --[4]
options[selected][2]()
break
end
draw() --[5]
end
[1] Pull any keyevent. _ catches the event, but that is "key", key catches a number representing the pressed key.
[2] If the key was the up key and the currently selected index is greater than 1 (To prevent it from being 0 or less) then decrement it.
[3] If the key was the down key and the selected option is less than the number of options we have then increment it
[4] If you pressed enter then run the function stored in the second slot of the currently selected option -> option[selected][2] and break then while loop to stop the menue programm.
[5] Redraw the screen because the selected option may have changed.I hope this helps you :D/>
JokerRH