202 posts
Posted 30 March 2013 - 08:00 PM
I want to try to make a simple menu that will select different things on a list depending on which keys you press. I've tried to make this many times before and have completely failed. It always does something completely different and I end up giving up until I feel like trying to make another one. I've tried to look at and use other people's menu programs and they work fine but I don't understand them. I want to be able to make my own and not just copy off of other people. I know you aren't really supposed to ask other people to just write code for you in ask a pro but I just don't even know where to begin. Could someone please try to help me understand how to do this?
Help is appreciated!
Thanks! :D/>
P.S. Sorry if this doesn't make a lot of sense. I wrote it at 3 in the morning… :)/>
521 posts
Location
Stockholm, Sweden
Posted 30 March 2013 - 08:53 PM
I get your point, it's frustrating when your code doesn't work.
As you said, not a lot of people thinks that you should just give so called "free" code. But we can at least help you a lot if you give us a code to start off with.
You see, when it comes to doing stuff for others the forums community oftenly prefers "correcting" code instead of "creating" code.
2151 posts
Location
Auckland, New Zealand
Posted 30 March 2013 - 09:19 PM
If it was me, I'd go sleep and try when I'm more energised. If the doesn't work do something like go for run or even go on a trampoline if you have one.
But as you have done this before…
What I would do is have a table of all the items, have an integer with the selected item. When an up or down key is pressed reduce or increase the selected integer.
7508 posts
Location
Australia
Posted 30 March 2013 - 11:48 PM
Another approach that you can take is one that the CC devs take, where the first letter of each menu item is bound to its relevant key. however this means you cannot have 2 entries with the same name.
521 posts
Location
Stockholm, Sweden
Posted 31 March 2013 - 02:34 AM
7508 posts
Location
Australia
Posted 31 March 2013 - 03:06 AM
I presume we all mean a list something like this:
-image snip-
Right?
Yes that is what I am assuming is desired. or like the 'edit' programs menu…
1852 posts
Location
Sweden
Posted 31 March 2013 - 04:33 AM
So you want to learn how to make a menu program?
I can show you a basic here..
Spoiler
function events()
evt, p1 = os.pullEvent()
if evt == "char" then --This is the event checker and if it is char it will check which char it was
if p1 == "r" then --This is if the user pressed the char 'r'
for i = 1,5 do
term.clear()
term.setCursorPos(20,9)
print("Rebooting.")
sleep(0)
term.clear()
term.setCursorPos(20,9)
print("Rebooting..")
sleep(0)
term.clear()
term.setCursorPos(20,9)
print("Rebooting...")
sleep(0)
end
os.reboot()
elseif p1 == "b" then
term.clear()
term.setCursorPos(1,1)
error() -- This breaks out of the program
elseif p1 == "s" then
for i = 1,5 do
term.clear()
term.setCursorPos(20,9)
print("Shutting Down.")
sleep(0)
term.clear()
term.setCursorPos(20,9)
print("Shutting Down..")
sleep(0)
term.clear()
term.setCursorPos(20,9)
print("Shutting Down...")
sleep(0)
end
os.shutdown()
end
elseif evt == "key" then
--[[
To get the key number of the one you choose you can just
open up the LUA prompt and type: 'while true do print(os.pullEvent()) end' without the ''
And then just press a key and the number for it will come up
]]--
if p1 == 28 then --Enter
color = 2 ^ math.random(0,15) --Randomly selects a background color
elseif p1 == 57 then --Space
tColor = 2 ^ math.random(0,15) --Randomly selects a text color
end
end
end --Ends the function
function drawMenu()
--[[
This function draws your options
]]--
term.setTextColor(tColor) --This sets the text color
print("[B]Break \n[S] Shutdown \n[R] Reboot \n[ENTER] Change background color \n[SPACE] Change text color") --\n = New line
end
function main()
color = colors.black --Default color
tColor = colors.white -- Default color
while true do
term.setBackgroundColor(color)
term.clear()
term.setCursorPos(1,1)
drawMenu()
events()
end
end
main() --You have to call the main function so your program actually does something
2088 posts
Location
South Africa
Posted 31 March 2013 - 11:18 AM
Many people ask this so I kept one I made and always show people this one,
pastebin
202 posts
Posted 31 March 2013 - 07:16 PM
So, I tried to take oeed and jag_e_nummer_et's advice and I took a break from coding anything for a day. I hardly even touched my computer. When I sat back down and tried to make a new selector menu it worked! Everything suddenly made sense and I could see where I had done things wrong. From looking at others people's code I know it is not very good, but it works and that's all I care about. I'm gonna post the code down below to see if I can get any pointers on what I can do better. I feel like the logic where it tries to decide what to do if a key is pressed is a bit excessive and could be improved.
I've commented most of the code to try to explain my reasoning for certain things
Code:http://pastebin.com/gATUjxfnI can also very easily expand off of this for more options and different keys to press.
Thanks to everyone who tried to help me!
THANKS!!! :D/>
521 posts
Location
Stockholm, Sweden
Posted 01 April 2013 - 01:45 AM
I am happy that it worked! OuO