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

Creating an Options Menu

Started by whiteknight1488, 10 June 2012 - 05:43 PM
whiteknight1488 #1
Posted 10 June 2012 - 07:43 PM
I have this code under "edit startup"

HorzLoadBar(15,35,"*")

function HorzMenu(menutable1, Ylocation)
local selection1 = 1
while true do
term.setCursorPos(1, Ylocation)
for i = 1,#menutable1 do

if i == selection1 then write"[" end
write(menutable1)
if i == selection1 then write"]" end
write" | "

end
local r,s = os.pullEvent()

if r == "key" then
if s == 203 then selection1 = selection1 - 1 end
if selection1 == 0 then selection1 = #menutable1 end
if s == 205 then selection1 = selection1 + 1 end
if selection1 == #menutable1 +1 then selection1 = 1 end

if s == 28 then
term.setCursorPos(1,Ylocation + 1)
return menutable1[selection1]
end
end

end
end

print ("Please Select")

items = {"Dump", "Emergency Shut OFF", "Reactor Startup"}
menuResult = HorzMenu(items, 6)

It works it just i dont know how to "assign" commands to the options… for example:
Dump—> I need the computer to send a a redstone pulse the code is redpulse left 100 1 but where does that go and where can i put this to be assigned to the "dump" selection
thanks alot
Xfel #2
Posted 10 June 2012 - 07:50 PM
Try this:

-- somewhere at the top
function dump()
...
end
function emergencyShutdown()
...
end
function startReactor()
...
end

HorzLoadBar(15,35,"*")

function HorzMenu(menutable1, Ylocation)
local selection1 = 1
while true do
term.setCursorPos(1, Ylocation)
for i = 1,#menutable1 do

if i == selection1 then write"[" end
write(menutable1[i])
if i == selection1 then write"]" end
write" | "

end
local r,s = os.pullEvent()

if r == "key" then
if s == 203 then selection1 = selection1 - 1 end
if selection1 == 0 then selection1 = #menutable1 end
if s == 205 then selection1 = selection1 + 1 end
if selection1 == #menutable1 +1 then selection1 = 1 end

if s == 28 then
term.setCursorPos(1,Ylocation + 1)
return menutable1[selection1]
end
end

end
end

print ("Please Select")

items = {"Dump","Emergency Shut OFF", "Reactor Startup"}
menuResult = HorzMenu(items, 6)

actions= {["Dump"]=dump, ["Emergency Shut OFF"]=emergencyShutdown, ["Reactor Startup"]=startReactor}
actions[menuResult]()


EDIT: forgot a little detail
whiteknight1488 #3
Posted 10 June 2012 - 08:09 PM
Im getting string startup 47 '}" expected with this code
Xfel #4
Posted 10 June 2012 - 08:39 PM
fixed
whiteknight1488 #5
Posted 10 June 2012 - 08:50 PM
Lol thanks but i got another problem startup 11 attempt to call nil
kazagistar #6
Posted 11 June 2012 - 01:54 AM
It means you are trying to call a function that does not exist or is not yet defined on line 11.
MysticT #7
Posted 11 June 2012 - 02:32 AM
I think the problem is here:

HorzLoadBar(15,35,"*")

function HorzMenu(menutable1, Ylocation)
You call the function HorzLoadBar, but you defined HorzMenu. Also, you should define the functions before calling them.