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

[lual] Why is my program doing this?

Started by deity12, 17 June 2012 - 06:12 AM
deity12 #1
Posted 17 June 2012 - 08:12 AM
Right so here's my code:

while true do
term.clear()
term.setCursorPos(1,1)
print ("Turtlecom local v2.0 Running.")
print ("Press ',' to get started or '.' to quit")
local event, param1 = os.pullEvent("char")
x = param1
if x == "," then
term.clear()
term.setCursorPos(1,1)
print ("w = move forward")
print ("s = move back")
print ("a = turn left")
print ("d = turn right")
print ("q = go up")
print ("z = go down")
print ("f = place")
print ("r = place up")
print ("v = place down")
print ("Press 'm' to go the the next page...")
local event, param1 = os.pullEvent("char")
if param1 == "m" then
term.clear()
term.setCursorPos(1,1)
print ("Numbers 1-9 = Select Slot")
print ("Mining Turtle only commands:")
print ("t = dig up")
print ("g = dig")
print ("b = dig down")
print ("Press 'm' to return to commannd interface.")
local event, param1 = os.pullEvent("char")
if param1 == "m" then
term.clear()
term.setCursorPos(1,1)
elseif x == "w" then
turtle.forward()
elseif x == "s" then
turtle.back()
elseif x == "a" then
turtle.turnLeft()
elseif x == "d" then
turtle.turnRight()
elseif x == "q" then
turtle.up()
elseif x == "z" then
turtle.down()
elseif x == "f" then
turtle.place()
elseif x == "r" then
turtle.placeUp()
elseif x == "v" then
turtle.placeDown()
elseif x == "." then
print ("Closing turtlecom ...")
sleep(1)
term.clear()
term.setCursorPos(1,1)
break
else
print ("That is not a valid command.")
sleep(1)
end
end
end
end
It's supposed to make the turtle perform different actions depending on button pressed but ever since I added the 'next page' function to the help menu, it won't let me do anything but open up the menu. Have no idea why. Help would be appreciated!
rbasset #2
Posted 17 June 2012 - 11:35 AM
im not an expert by any means but im writing my own OS for computer craft im pretty sure you need and END for every ELSE IF statement.

like i said tho im no pro lol.

hope it helps!


I think you need 13 ENDS at the end of your code. plus ELSEIF is suppose to be ELSE IF i beleive. (no caps, and dont quote me on the ELSEIF statement lol)
Pinkishu #3
Posted 17 June 2012 - 11:44 AM
You don't need an end for and elseif :(/>/>
Lyqyd #4
Posted 17 June 2012 - 11:45 AM
im not an expert by any means but im writing my own OS for computer craft im pretty sure you need and END for every ELSE IF statement.

like i said tho im no pro lol.

hope it helps!


I think you need 13 ENDS at the end of your code. plus ELSEIF is suppose to be ELSE IF i beleive. (no caps, and dont quote me on the ELSEIF statement lol)

This is wrong, for the reasons I outlined in deity12's previous thread.

There are several things wrong with the code. Using proper indentation, you can see the problems:


while true do
    term.clear()
    term.setCursorPos(1,1)
    print ("Turtlecom local v2.0 Running.")
    print ("Press ',' to get started or '.' to quit")
    local event, param1 = os.pullEvent("char")
    x = param1
    if x == "," then
        term.clear()
        term.setCursorPos(1,1)
        print ("w = move forward")
        print ("s = move back")
        print ("a = turn left")
        print ("d = turn right")
        print ("q = go up")
        print ("z = go down")
        print ("f = place")
        print ("r = place up")
        print ("v = place down")
        print ("Press 'm' to go the the next page...")
        local event, param1 = os.pullEvent("char")
        if param1 == "m" then
            term.clear()
            term.setCursorPos(1,1)
            print ("Numbers 1-9 = Select Slot")
            print ("Mining Turtle only commands:")
            print ("t = dig up")
            print ("g = dig")
            print ("b = dig down")
            print ("Press 'm' to return to commannd interface.")
            local event, param1 = os.pullEvent("char")
            if param1 == "m" then
                term.clear()
                term.setCursorPos(1,1)
            elseif x == "w" then
                turtle.forward()
            elseif x == "s" then
                turtle.back()
            elseif x == "a" then
                turtle.turnLeft()
            elseif x == "d" then
                turtle.turnRight()
            elseif x == "q" then
                turtle.up()
            elseif x == "z" then
                turtle.down()
            elseif x == "f" then
                turtle.place()
            elseif x == "r" then
                turtle.placeUp()
            elseif x == "v" then
                turtle.placeDown()
            elseif x == "." then
                print ("Closing turtlecom ...")
                sleep(1)
                term.clear()
                term.setCursorPos(1,1)
                break
            else
                print ("That is not a valid command.")
                sleep(1)
            end
        end
    end
end

Most notably, that param1 is checked for equality to "m" and only if it is "m" do we proceed into other printing and other checks. Note that all code following the "m" comparison after the initial menu is written to the screen is inside this if statement, so it will only be executed if the "m" key is pressed. That's the first of the issues. The rest of them are similar. Please note also that you may want to use a while loop with a condition to stay inside the submenu until an exit key is pressed.
Pinkishu #5
Posted 17 June 2012 - 11:47 AM
edit:too slow
ardera #6
Posted 17 June 2012 - 12:04 PM
im not an expert by any means but im writing my own OS for computer craft im pretty sure you need and END for every ELSE IF statement.

like i said tho im no pro lol.

hope it helps!


I think you need 13 ENDS at the end of your code. plus ELSEIF is suppose to be ELSE IF i beleive. (no caps, and dont quote me on the ELSEIF statement lol)

I think you should learn LUA… elseif is right, and his ends are right too…
rbasset #7
Posted 17 June 2012 - 12:30 PM
Oh i know i should learn Lua haha thats why i said dont quote me cuz i honestly have no clue lol and ill be the first to admit that haha, just trying to help a fellow noob out you know, i mean I was getting some errors with my elseif statements until i added an end at the END of the code, i had 9 elseifs so i put 10 ends in and now it works perfectly, and THATs why i said dont quote me lol, no need to be trolling a noob here lol, sorry I couldnt be more help Deity12. perhaps one day lol
Pinkishu #8
Posted 17 June 2012 - 12:32 PM
Oh i know i should learn Lua haha thats why i said dont quote me cuz i honestly have no clue lol and ill be the first to admit that haha, just trying to help a fellow noob out you know, i mean I was getting some errors with my elseif statements until i added an end at the END of the code, i had 9 elseifs so i put 10 ends in and now it works perfectly, and THATs why i said dont quote me lol, no need to be trolling a noob here lol, sorry I couldnt be more help Deity12. perhaps one day lol

if you use else if instead of elseif that may be true but then i won't guarantee your code works xD
rbasset #9
Posted 17 June 2012 - 12:40 PM
haha wow you know what your absolutely right Pinkishu I just checked my code and thats exactly what i have done lol, thanks for your insight now I can shorten my code by at least a few lines lol. thanks again
ardera #10
Posted 17 June 2012 - 01:46 PM
thats why i said dont quote me cuz i honestly have no clue lol
Oh sorry, I didn't read it… :(/>/>
rbasset #11
Posted 17 June 2012 - 01:53 PM
hahaha nice….
deity12 #12
Posted 18 June 2012 - 05:11 AM
im not an expert by any means but im writing my own OS for computer craft im pretty sure you need and END for every ELSE IF statement.

like i said tho im no pro lol.

hope it helps!


I think you need 13 ENDS at the end of your code. plus ELSEIF is suppose to be ELSE IF i beleive. (no caps, and dont quote me on the ELSEIF statement lol)

This is wrong, for the reasons I outlined in deity12's previous thread.

There are several things wrong with the code. Using proper indentation, you can see the problems:


while true do
	term.clear()
	term.setCursorPos(1,1)
	print ("Turtlecom local v2.0 Running.")
	print ("Press ',' to get started or '.' to quit")
	local event, param1 = os.pullEvent("char")
	x = param1
	if x == "," then
		term.clear()
		term.setCursorPos(1,1)
		print ("w = move forward")
		print ("s = move back")
		print ("a = turn left")
		print ("d = turn right")
		print ("q = go up")
		print ("z = go down")
		print ("f = place")
		print ("r = place up")
		print ("v = place down")
		print ("Press 'm' to go the the next page...")
		local event, param1 = os.pullEvent("char")
		if param1 == "m" then
			term.clear()
			term.setCursorPos(1,1)
			print ("Numbers 1-9 = Select Slot")
			print ("Mining Turtle only commands:")
			print ("t = dig up")
			print ("g = dig")
			print ("b = dig down")
			print ("Press 'm' to return to commannd interface.")
			local event, param1 = os.pullEvent("char")
			if param1 == "m" then
				term.clear()
				term.setCursorPos(1,1)
			elseif x == "w" then
				turtle.forward()
			elseif x == "s" then
				turtle.back()
			elseif x == "a" then
				turtle.turnLeft()
			elseif x == "d" then
				turtle.turnRight()
			elseif x == "q" then
				turtle.up()
			elseif x == "z" then
				turtle.down()
			elseif x == "f" then
				turtle.place()
			elseif x == "r" then
				turtle.placeUp()
			elseif x == "v" then
				turtle.placeDown()
			elseif x == "." then
				print ("Closing turtlecom ...")
				sleep(1)
				term.clear()
				term.setCursorPos(1,1)
				break
			else
				print ("That is not a valid command.")
				sleep(1)
			end
		end
	end
end

Most notably, that param1 is checked for equality to "m" and only if it is "m" do we proceed into other printing and other checks. Note that all code following the "m" comparison after the initial menu is written to the screen is inside this if statement, so it will only be executed if the "m" key is pressed. That's the first of the issues. The rest of them are similar. Please note also that you may want to use a while loop with a condition to stay inside the submenu until an exit key is pressed.

Like, your post my other thread you seem to know what your talking about, but once again I'm not quite sure I understand… Could you provide a corrected version of the code so that I can learn from that? Also, nice to know you can spell my name right unlike 95% of the internet.
archit #13
Posted 19 June 2012 - 12:31 AM
He is saying that everything after the (if param1 == "m" then) is inside that block, which means you can't reach any of it unless you press m.

You need to take everything else out of that if block and add it under it instead.
Lyqyd #14
Posted 19 June 2012 - 07:39 AM
im not an expert by any means but im writing my own OS for computer craft im pretty sure you need and END for every ELSE IF statement.

like i said tho im no pro lol.

hope it helps!


I think you need 13 ENDS at the end of your code. plus ELSEIF is suppose to be ELSE IF i beleive. (no caps, and dont quote me on the ELSEIF statement lol)

This is wrong, for the reasons I outlined in deity12's previous thread.

There are several things wrong with the code. Using proper indentation, you can see the problems:


while true do
	term.clear()
	term.setCursorPos(1,1)
	print ("Turtlecom local v2.0 Running.")
	print ("Press ',' to get started or '.' to quit")
	local event, param1 = os.pullEvent("char")
	x = param1
	if x == "," then
		term.clear()
		term.setCursorPos(1,1)
		print ("w = move forward")
		print ("s = move back")
		print ("a = turn left")
		print ("d = turn right")
		print ("q = go up")
		print ("z = go down")
		print ("f = place")
		print ("r = place up")
		print ("v = place down")
		print ("Press 'm' to go the the next page...")
		local event, param1 = os.pullEvent("char")
		if param1 == "m" then
			term.clear()
			term.setCursorPos(1,1)
			print ("Numbers 1-9 = Select Slot")
			print ("Mining Turtle only commands:")
			print ("t = dig up")
			print ("g = dig")
			print ("b = dig down")
			print ("Press 'm' to return to commannd interface.")
			local event, param1 = os.pullEvent("char")
			if param1 == "m" then
				term.clear()
				term.setCursorPos(1,1)
			elseif x == "w" then
				turtle.forward()
			elseif x == "s" then
				turtle.back()
			elseif x == "a" then
				turtle.turnLeft()
			elseif x == "d" then
				turtle.turnRight()
			elseif x == "q" then
				turtle.up()
			elseif x == "z" then
				turtle.down()
			elseif x == "f" then
				turtle.place()
			elseif x == "r" then
				turtle.placeUp()
			elseif x == "v" then
				turtle.placeDown()
			elseif x == "." then
				print ("Closing turtlecom ...")
				sleep(1)
				term.clear()
				term.setCursorPos(1,1)
				break
			else
				print ("That is not a valid command.")
				sleep(1)
			end
		end
	end
end

Most notably, that param1 is checked for equality to "m" and only if it is "m" do we proceed into other printing and other checks. Note that all code following the "m" comparison after the initial menu is written to the screen is inside this if statement, so it will only be executed if the "m" key is pressed. That's the first of the issues. The rest of them are similar. Please note also that you may want to use a while loop with a condition to stay inside the submenu until an exit key is pressed.

Like, your post my other thread you seem to know what your talking about, but once again I'm not quite sure I understand… Could you provide a corrected version of the code so that I can learn from that? Also, nice to know you can spell my name right unlike 95% of the internet.

I'm going to pseudo code this at you because I'm on my phone, not at my computer. This should give you the gist of it, though:


local function subMenu()
    while true do
        clearTheScreen()
        printTheSubMenu()
        e, p = os.pullEvent()
        if e == char then
            if p == someChar then
                performAction()
            elseif p == goBackChar then
                return
            end
        end
    end
end

while true do
    clearTheScreen()
    printTheMenu()
    e, p = os.pullEvent()
    if e == char then
        if p == someKey then
            doSomeStuff()
        elseif p == subMenuKey then
           subMenu()
        elseif p == exitChar then
            return
        else
            whineAboutInvalidOption()
        end
    end
end