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

[lua] Can't figure out why...

Started by FUCKCOMPUTERCRAFT!"£, 03 January 2013 - 11:15 PM
FUCKCOMPUTERCRAFT!"£ #1
Posted 04 January 2013 - 12:15 AM
Hey, i was testing my program and have no idea why it isn't working. Any ideas on why would be helpful as i have no idea after receiving the same error again and again…

http://pastebin.com/yKxec9R9
theoriginalbit #2
Posted 04 January 2013 - 01:09 AM
Please more information. What is the error?
remiX #3
Posted 04 January 2013 - 01:34 AM
I'm not sure how you're trying to make your menus but you can't set a variable to the name of a function and try call it.
FUCKCOMPUTERCRAFT!"£ #4
Posted 04 January 2013 - 02:33 AM
Well it doesn't move up or down and when i hit enter it does nothing, but looking at the code im sure it should :L

I'm not sure how you're trying to make your menus but you can't set a variable to the name of a function and try call it.

You can, try it….


function test12345()
print("You're gona be surprised")
end
current = test12345
current()
remiX #5
Posted 04 January 2013 - 02:59 AM
Yeah nevermind, I had a brain fart. I've actually done it before in my codes before. Wasn't thinking haha.

But the way you're making the menu systems is weird, you should use a table with the options and then print the options.

Example:



local function menu(_table)
    local sel = 1
    local offX,offY = term.getCursorPos()
    local curX,curY = term.getCursorPos()
    while true do
		    if sel > #_table then sel = 1 end
		    if sel < 1 then sel = #_table end
		    for i = 1,#_table do
				    term.setCursorPos(offX,offY+i-1)
				    if sel == i then
						    print("[".._table[i].."]")
				    else
						    print(" ".._table[i].." ")
				    end
		    end
		    local e, key = os.pullEvent("key")
		    if key == 200 then -- up key
				    sel = sel-1
		    elseif key == 208 then -- down key
				    sel = sel+1
		    elseif key == 28 then
				    term.setCursorPos(curX,curY)
				    return _table[sel], sel
		    end
    end
end

-- Table with options
tab = {"Option 1", "Option 2"}

-- Call it like this
selection = menu(tab)
if selection == tab[1] then
-- Option 1 was selected
elseif selection == tab[2] then
-- Option 2 was selected
end
FUCKCOMPUTERCRAFT!"£ #6
Posted 04 January 2013 - 03:12 AM
http://www.youtube.com/watch?v=dss5J4ewSkM
FUCKCOMPUTERCRAFT!"£ #7
Posted 04 January 2013 - 05:19 AM
Yeah nevermind, I had a brain fart. I've actually done it before in my codes before. Wasn't thinking haha.

But the way you're making the menu systems is weird, you should use a table with the options and then print the options.

Example:



local function menu(_table)
	local sel = 1
	local offX,offY = term.getCursorPos()
	local curX,curY = term.getCursorPos()
	while true do
			if sel > #_table then sel = 1 end
			if sel < 1 then sel = #_table end
			for i = 1,#_table do
					term.setCursorPos(offX,offY+i-1)
					if sel == i then
							print("[".._table[i].."]")
					else
							print(" ".._table[i].." ")
					end
			end
			local e, key = os.pullEvent("key")
			if key == 200 then -- up key
					sel = sel-1
			elseif key == 208 then -- down key
					sel = sel+1
			elseif key == 28 then
					term.setCursorPos(curX,curY)
					return _table[sel], sel
			end
	end
end

-- Table with options
tab = {"Option 1", "Option 2"}

-- Call it like this
selection = menu(tab)
if selection == tab[1] then
-- Option 1 was selected
elseif selection == tab[2] then
-- Option 2 was selected
end

I was gona use table but i don't understand them, tried looking at tutorials but didn't get it…

Even still should my code not work? even if it is werid?!?
remiX #8
Posted 04 January 2013 - 05:50 AM
I'll take a harder look at your code and try find what's wrong but you do not need a function to highlight which option is selected.

WIth the code I gave you, you don't even need to tables:


local function menu(...)
		local sel = 1
		list = {...}
		local offX,offY = term.getCursorPos()
		local curX,curY = term.getCursorPos()
		while true do
						if sel > #list then sel = 1 end
						if sel < 1 then sel = #list end
						for i = 1,#list do
										term.setCursorPos(offX,offY+i-1)
										if sel == i then
														print("["..list[i].."]")
										else
														print(" "..list[i].." ")
										end
						end
						local e, key = os.pullEvent("key")
						if key == 200 then -- up key
										sel = sel-1
						elseif key == 208 then -- down key
										sel = sel+1
						elseif key == 28 then
										term.setCursorPos(curX,curY)
										return list[sel], sel
						end
		end
end

-- Call it like this
selection, number = menu("Option 1", "Option 2")
if selection == "Option 1" then -- You can also use the 'number' variable, here it would be 1
-- Option 1 was selected
elseif selection == "Option 2" then -- number will be 2
-- Option 2 was selected
end

Edit: Remove these lines from each of the last 3 functions with os.pullEvent


        if event == "key" then 
        else
                current()
        end
        if key ~= 28 or 200 or 208 then
                current()
        end  

And then when you make mMselection = 2, there is no if statement for if it is 2.

Also, you can use if elseif else end, instead of each block being an if statement.
FUCKCOMPUTERCRAFT!"£ #9
Posted 04 January 2013 - 06:16 AM
I'll take a harder look at your code and try find what's wrong but you do not need a function to highlight which option is selected.

WIth the code I gave you, you don't even need to tables:


local function menu(...)
		local sel = 1
		list = {...}
		local offX,offY = term.getCursorPos()
		local curX,curY = term.getCursorPos()
		while true do
						if sel > #list then sel = 1 end
						if sel < 1 then sel = #list end
						for i = 1,#list do
										term.setCursorPos(offX,offY+i-1)
										if sel == i then
														print("["..list[i].."]")
										else
														print(" "..list[i].." ")
										end
						end
						local e, key = os.pullEvent("key")
						if key == 200 then -- up key
										sel = sel-1
						elseif key == 208 then -- down key
										sel = sel+1
						elseif key == 28 then
										term.setCursorPos(curX,curY)
										return list[sel], sel
						end
		end
end

-- Call it like this
selection, number = menu("Option 1", "Option 2")
if selection == "Option 1" then -- You can also use the 'number' variable, here it would be 1
-- Option 1 was selected
elseif selection == "Option 2" then -- number will be 2
-- Option 2 was selected
end

Edit: Remove these lines from each of the last 3 functions with os.pullEvent


		if event == "key" then
		else
				current()
		end
		if key ~= 28 or 200 or 208 then
				current()
		end  

And then when you make mMselection = 2, there is no if statement for if it is 2.

Also, you can use if elseif else end, instead of each block being an if statement.


I've just realised all the 0 should be 2 in the if statements…

Also why remove these functions? They are needed to ensure other event aren't interfering and that they entered valid keys…
remiX #10
Posted 04 January 2013 - 06:27 AM
Then surround the code in an 'if event == "key" then' statement.
Also, I used elseif's here so you can see how it's used:


function mMenuKI()
    event , key = os.pullEvent()
    -- Surround the code within an if statement
    if event == "key" then
        if key == 28 then--ENTER KEY
            if mMselction == 1 then --SELECTED E-Mail Network
                EMailNetworkMenu()
            elseif mMselction == 2 then --SELECTED Exit
                exitP()
            end
        elseif key == 200 and mMselction == 1 then --ARROW KEY
            mMenu()
        elseif key == 200 and mMselction == 2 then --ARROW KEY
            mMenu()
        elseif key == 208 and mMselction == 1 then --ARROW KEY
            mvMenu()
        elseif key == 208 and mMselction == 2 then --ARROW KEY
            mvMenu()
        end
    end
end
FUCKCOMPUTERCRAFT!"£ #11
Posted 04 January 2013 - 07:09 AM
Then surround the code in an 'if event == "key" then' statement.
Also, I used elseif's here so you can see how it's used:


function mMenuKI()
	event , key = os.pullEvent()
	-- Surround the code within an if statement
	if event == "key" then
		if key == 28 then--ENTER KEY
			if mMselction == 1 then --SELECTED E-Mail Network
				EMailNetworkMenu()
			elseif mMselction == 2 then --SELECTED Exit
				exitP()
			end
		elseif key == 200 and mMselction == 1 then --ARROW KEY
			mMenu()
		elseif key == 200 and mMselction == 2 then --ARROW KEY
			mMenu()
		elseif key == 208 and mMselction == 1 then --ARROW KEY
			mvMenu()
		elseif key == 208 and mMselction == 2 then --ARROW KEY
			mvMenu()
		end
	end
end

No idea what on earth you have done but it works :)/>

Im now just changing the code a bit to get what i want ill post if i don't understand something or have hit a brick wall…
FUCKCOMPUTERCRAFT!"£ #12
Posted 04 January 2013 - 07:21 AM
I think Lua just doesn't like me :L Nothing ever seems to work…


---------------------------------------
--MAIN MENU KEY INTERACTION
function mMenuKI()
	event , key = os.pullEvent()
	-- Surround the code within an if statement
	if event == "key" then
               if key ~= 28 or 200 or 208 then
                       current()
               elseif key == 28 then--ENTER KEY
			if mMselction == 1 then --SELECTED E-Mail Network
				EMailNetworkMenu()
			elseif mMselction == 2 then --SELECTED Exit
				exitP()
			end
		elseif key == 200 and mMselction == 1 then --ARROW KEY
			mMenu()
		elseif key == 200 and mMselction == 2 then --ARROW KEY
			mMenu()
		elseif key == 208 and mMselction == 1 then --ARROW KEY
			mvMenu()
		elseif key == 208 and mMselction == 2 then --ARROW KEY
			mvMenu()						  
		end
	end
end
----------------------------------------

When adding in the if statement so if they hit like f it does nothing (Which doesn't stop them i just tried…) it now also means you can't use the arrow keys i tried implimenting it diffrent ways but it just doesn't work…
remiX #13
Posted 04 January 2013 - 07:43 AM

if key ~= 28 or key ~= 200 or key ~= 208 then

But you don't need that! The one I posted should work fine.
FUCKCOMPUTERCRAFT!"£ #14
Posted 04 January 2013 - 07:45 AM

if key ~= 28 or key ~= 200 or key ~= 208 then

But you don't need that! The one I posted should work fine.

I copy and pasted it in and erased mine and yes it worked i can hit any key and it just exits…
remiX #15
Posted 04 January 2013 - 08:04 AM
Add a while true do loop then


function mMenuKI()
while true do
        event , key = os.pullEvent()
        -- Surround the code within an if statement
        if event == "key" then
                if key == 28 then--ENTER KEY
                        if mMselction == 1 then --SELECTED E-Mail Network
                                EMailNetworkMenu()
                        elseif mMselction == 2 then --SELECTED Exit
                                exitP()
                        end
                elseif key == 200 and mMselction == 1 then --ARROW KEY
                        mMenu()
                elseif key == 200 and mMselction == 2 then --ARROW KEY
                        mMenu()
                elseif key == 208 and mMselction == 1 then --ARROW KEY
                        mvMenu()
                elseif key == 208 and mMselction == 2 then --ARROW KEY
                        mvMenu()
                end
        end
end
end
Problem solved
FUCKCOMPUTERCRAFT!"£ #16
Posted 04 January 2013 - 08:12 AM
Okay so what was the problem with my code? Why couldn't what i had be used???
remiX #17
Posted 04 January 2013 - 08:27 AM
I think it's because you used the or wrong,
you used
if key ~= 29 or 200 or ...
but it should be
 if key ~= 29 or key ~= 200 ...
FUCKCOMPUTERCRAFT!"£ #18
Posted 04 January 2013 - 08:44 AM
I think it's because you used the or wrong,
you used
if key ~= 29 or 200 or ...
but it should be
 if key ~= 29 or key ~= 200 ...

Ahh okay :)/> Thanks for your help :D/> I'm sure ill confuse myself again later…