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

Lua Error: I need help programing and fixing this.

Started by Hydralisk1994, 01 January 2013 - 03:57 PM
Hydralisk1994 #1
Posted 01 January 2013 - 04:57 PM
Programing type:
Lua with Gui

Problem:
I am trying to make a Gui that has info on items. Right now I can use it but after 2 buttons are selected it stops working well. The word stone just keeps poping up if you click enter or try to scroll.

Possible upgrades:
I want to be able to also scroll up because there is to much text. Half of what I have is hidden. Maybe make "bottons" that don't work but are placed beside everyline so they can scroll up the bottons to see the rest of the text. And at the bottom (it is already there) the options for other pages and the exit button. Thanks.

Pastebin version of script:
http://pastebin.com/TieR2P5r

Lua script (What I got so far):

local function menu(...)
		local sel = 1
		local 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].."]") -- very customisible example print(">"..list[i])
						else
								print(" "..list[i].." ") -- very customisible
						end
				end
				while true do
						local e,e1,e2,e3,e4,e5 = os.pullEvent()
						if e == "key" then
								if e1 == 200 then -- up key
										sel = sel-1
												break
								end
								if e1 == 208 then -- down key
										sel = sel+1
												break
								end
								if e1 == 28 then
										term.setCursorPos(curX,curY)
										return list[sel],sel
								end
						end
				end
		end
end

function Option1()
  shell.run("clear")
  print"(Stone, also known as Smooth Stone, is a block found in")
  print("abundance in the Overworld. It uses the same texture on all")
  print("six sides of the block. When mined, stone produces cobblestone.")
  print("")
  print("Stone is an often-used building material in the construction")
  print("of buildings, paths and many other things.")
  print("")
  print("After air, stone is the second most common block found in a")
  print("normal map.")
  print("")
  print("EMC: 1")
  print("Blast Resistance: 30")
  print("ID: 1")
end

function Option2()
  shell.run("clear")
  print("A Grass Block is a block that was introduced very early in the")
  print("game. It uses 4 textures: a gray one for the top which is then")
  print("tinted to the correct color according to what biome the block is")
  print("in, one borrowed from the dirt block for the bottom, an edited")
  print("dirt texture with grass on the top edge on all of the sides, and")
  print("a gray texture matching the shape of the grass on the side of")
  print("the block, which is tinted to the correct biome color. When")
  print("covered by snow the side texture is white. On natural maps,")
  print("grass appears on topmost blocks of dirt with no fluids or opaque")
  print("solid blocks above them.")
  print("")
  print("In mining, grass blocks behave just like dirt - they drop dirt")
  print("resources and are best dug with a shovel; however, they make a")
  print("different sound when harvested and take slightly longer to dig up.")
  print("This can be used to tell when the player is about to break the")
  print("surface when tunneling upwards.")
  print("")
  print("EMC: 1")
  print("Blast Resistance: 3")
  print("ID: 2")
end

function Option3()
  shell.run("clear")
  print("Dirt is a block found abundantly in the Overworld. It uses the same")
  print("texture on all six sides of the block. This is a block often used")
  print("when making a temporary shelter quickly since it is commonly found.")
  print("Dirt makes up the majority of the top layers of blocks, between grass,")
  print("snow and stone. It is also found underground in clumps in all")
  print("altitudes and in the bottom of lakes and oceans. It is a very weak")
  print("block and is easily destroyed in explosions from TNT or Creepers,")
  print("making dirt shelters a fallback option at best.")
  print("")
  print("When a dirt block is adjacent to a grass block and is exposed to a")
  print("light level of at least 4 (e.g. from torches or sunlight), it will")
  print("eventually be converted into a grass block; when this happens is")
  print("random and unpredictable (see grass for growth details). Dirt can also")
  print("have mycelium spread on to the block in much the same way, but")
  print("requiring a light level of at least 9.")
  print("")
  print("Dirt will always drop a resource block, regardless of how it was mined.")
  print("It may be mined by hand fairly quickly, but using a shovel will")
  print("significantly speed up the process.")
  print("")
  print("Using a hoe on a placed block of dirt will turn it into a farmland block,")
  print("enabling seeds to be planted on it. Farmland will revert to dirt unless")
  print("something is planted on it or it is irrigated by nearby water.")
  print("")
  print("EMC: 1")
  print("Blast Resistance: 2.5")
  print("ID: 3")
end

function Option4()
  shell.run("clear")
  print("Cobblestone is a common block used for creating various items, that")
  print("resembles an uneven, roughly paved surface. Cobblestone only occurs")
  print("naturally in dungeons, NPC Villages, strongholds, or when water and")
  print("flowing lava come into contact. It is commonly used to create walls for")
  print("constructions with a medieval appearance such as castles, towers and roads.")
  print("Cobblestone is obtained by mining stone. Cobblestone is slightly harder to")
  print("mine than normal stone, and there is an alternative version of the block")
  print("known as Moss Stone. Silverfish have the ability to enter and hide in")
  print("cobblestone.")
  print("EMC: 1")
  print("Blast Resistance: 30")
  print("ID: 4")
end

function Option5()
  shell.run("clear")
  error()
end

while true do
		local selection = menu(
		"Stone 1",
		"GrassBlock 2",
		"Dirt 3",
		"Cobblestone 4",
		"Exit")
		if selection == "Stone 1" then
		 Option1()
		elseif selection == "GrassBlock 2" then
		 Option2()
		elseif selection == "Dirt 3" then
		 Option3()
		elseif selection == "Cobblestone 4" then
		 Option4()
		elseif selection == "Exit" then
		 Option5()
		end
end

If you can help me I would very much thank you for it!
Edited by
remiX #2
Posted 02 January 2013 - 12:45 AM
It happens because you don't clear the screen and reset the cursor position back to 1, 1.
And In option one " print"(Stone, also known as Smooth Stone, is a block found in") " will error.
Also, the text in some options is too much for a terminal, unless you're printing this on a monitor.

I did the scroll function you asked for and added the text into a table for easier access :)/>
This is the first time making a 'scroll function-thing' and I think it's pretty good :D/>

Spoiler

screenX, screenY = term.getSize()
-- Table
t_MainTable = {
    ["Stone 1"] = {
        "Stone, also known as Smooth Stone, is a block found in abundance in the Overworld.\n",
        "It uses the same texture on all six sides of the block. When mined, stone produces cobblestone.\n",
        "Stone is an often-used building material in the construction of buildings, paths and many other things.\n",
        "After air, stone is the second most common block found in a normal map.\n",
        "EMC: 1",
        "Blast Resistance: 30",
        "ID: 1"
    },
    ["GrassBlock 2"] = {
        "A Grass Block is a block that was introduced very early in the game.\n",
        "It uses 4 textures: a gray one for the top which is then tinted to the correct color according to what biome the block is in, one borrowed from the dirt block for the bottom, an edited dirt texture with grass on the top edge on all of the sides, and a gray texture matching the shape of the grass on the side of the block, which is tinted to the correct biome color.\n",
        "When covered by snow the side texture is white.\n",
        "On natural maps, grass appears on topmost blocks of dirt with no fluids or opaque solid blocks above them.\n",
        "In mining, grass blocks behave just like dirt - they drop dirt resources and are best dug with a shovel; however, they make a different sound when harvested and take slightly longer to dig up.\n",
        "This can be used to tell when the player is about to break the surface when tunneling upwards.\n",
        "EMC: 1",
        "Blast Resistance: 3",
        "ID: 2"
    },
    ["Dirt 3"] = {
        "Dirt is a block found abundantly in the Overworld.\n",
        "It uses the same texture on all six sides of the block.\n",
        "This is a block often used when making a temporary shelter quickly since it is commonly found.\n",
        "Dirt makes up the majority of the top layers of blocks, between grass, snow and stone.\n",
        "It is also found underground in clumps in all altitudes and in the bottom of lakes and oceans.\n",
        "It is a very weak block and is easily destroyed in explosions from TNT or Creepers, making dirt shelters a fallback option at best.\n",
        "When a dirt block is adjacent to a grass block and is exposed to a light level of at least 4 (e.g. from torches or sunlight), it will eventually be converted into a grass block; when this happens is random and unpredictable (see grass for growth details).\n",
        "Dirt can also have mycelium spread on to the block in much the same way, but requiring a light level of at least 9.\n",
        "Dirt will always drop a resource block, regardless of how it was mined.\n",
        "It may be mined by hand fairly quickly, but using a shovel will significantly speed up the process.\n",
        "Using a hoe on a placed block of dirt will turn it into a farmland block enabling seeds to be planted on it. Farmland will revert to dirt unless something is planted on it or it is irrigated by nearby water.\n",
        "EMC: 1",
        "Blast Resistance: 2.5",
        "ID: 3"
    },
    ["Cobblestone 4"] = {
        "Cobblestone is a common block used for creating various items, that resembles an uneven, roughly paved surface.\n",
        "Cobblestone only occurs naturally in dungeons, NPC Villages, strongholds, or when water and flowing lava come into contact.\n",
        "It is commonly used to create walls for constructions with a medieval appearance such as castles, towers and roads.\n",
        "Cobblestone is obtained by mining stone.\n",
        "Cobblestone is slightly harder to mine than normal stone, and there is an alternative version of the block known as Moss Stone.\n",
        "Silverfish have the ability to enter and hide in cobblestone.\n",
        "EMC: 1",
        "Blast Resistance: 30",
        "ID: 4"
    },
}

local function menu(...)
    local sel = 1
    local 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].."]") -- very customisible example print(">"..list[i])
            else
                print(" "..list[i].." ") -- very customisible
            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

function drawTable(_table)
    yPos = 1
    while true do
        term.setCursorPos(1, 1)
        term.clear()
        for i = yPos, #_table do
            print(_table[i])
            x, y = term.getCursorPos()
            if y >= 19 then break end
        end
        x, y = term.getCursorPos()
        msg = "Press 'x' to go back"
        term.setCursorPos(screenX - #msg + 1, screenY)
        write(msg)
        term.setCursorPos(x, y)
        e, button, x, y = os.pullEvent()
        cX, cY = term.getCursorPos()
        if e == "mouse_scroll" then
            if button == 1 and cY >= 18 and yPos < #_table then
                -- Down
                yPos = yPos + 1
            elseif button == -1 and yPos > 1 then
                -- Up
                yPos = yPos - 1
            end
        elseif e == "char" then
            -- Exit all the text
            if button == "x" then return end
        end
    end
end

while true do
    term.setCursorPos(1, 1)
    term.clear()
    local selection = menu(
    "Stone 1",
    "GrassBlock 2",
    "Dirt 3",
    "Cobblestone 4",
    "Exit")
    if selection == "Exit" then
        term.setCursorPos(1, 1)
        term.clear()
        break
    else
        drawTable(t_MainTable[selection])
    end
end
Hydralisk1994 #3
Posted 02 January 2013 - 11:37 AM
Thank you so much! I'll Test it right away! :D/>
Hydralisk1994 #4
Posted 02 January 2013 - 11:47 AM
The scroll dosent work I don't think. But thank you for fixing the error! And making a letter exit the screen!
remiX #5
Posted 02 January 2013 - 07:41 PM
Scroll works fine for me… Do you have version 1.4?
Hydralisk1994 #6
Posted 03 January 2013 - 08:39 AM
Tekkit version. Probably doesn't accept mouse scroll as a input. Can you make arrow keys scroll please?
remiX #7
Posted 03 January 2013 - 09:48 AM
No, tekkit doesn't have mouse support :P/> But isn't there a version 1.4 for tekkit now?

Arrow support:

Spoiler

screenX, screenY = term.getSize()
-- Table
t_MainTable = {
    ["Stone 1"] = {
	    "Stone, also known as Smooth Stone, is a block found in abundance in the Overworld.\n",
	    "It uses the same texture on all six sides of the block. When mined, stone produces cobblestone.\n",
	    "Stone is an often-used building material in the construction of buildings, paths and many other things.\n",
	    "After air, stone is the second most common block found in a normal map.\n",
	    "EMC: 1",
	    "Blast Resistance: 30",
	    "ID: 1"
    },
    ["GrassBlock 2"] = {
	    "A Grass Block is a block that was introduced very early in the game.\n",
	    "It uses 4 textures: a gray one for the top which is then tinted to the correct color according to what biome the block is in, one borrowed from the dirt block for the bottom, an edited dirt texture with grass on the top edge on all of the sides, and a gray texture matching the shape of the grass on the side of the block, which is tinted to the correct biome color.\n",
	    "When covered by snow the side texture is white.\n",
	    "On natural maps, grass appears on topmost blocks of dirt with no fluids or opaque solid blocks above them.\n",
	    "In mining, grass blocks behave just like dirt - they drop dirt resources and are best dug with a shovel; however, they make a different sound when harvested and take slightly longer to dig up.\n",
	    "This can be used to tell when the player is about to break the surface when tunneling upwards.\n",
	    "EMC: 1",
	    "Blast Resistance: 3",
	    "ID: 2"
    },
    ["Dirt 3"] = {
	    "Dirt is a block found abundantly in the Overworld.\n",
	    "It uses the same texture on all six sides of the block.\n",
	    "This is a block often used when making a temporary shelter quickly since it is commonly found.\n",
	    "Dirt makes up the majority of the top layers of blocks, between grass, snow and stone.\n",
	    "It is also found underground in clumps in all altitudes and in the bottom of lakes and oceans.\n",
	    "It is a very weak block and is easily destroyed in explosions from TNT or Creepers, making dirt shelters a fallback option at best.\n",
	    "When a dirt block is adjacent to a grass block and is exposed to a light level of at least 4 (e.g. from torches or sunlight), it will eventually be converted into a grass block; when this happens is random and unpredictable (see grass for growth details).\n",
	    "Dirt can also have mycelium spread on to the block in much the same way, but requiring a light level of at least 9.\n",
	    "Dirt will always drop a resource block, regardless of how it was mined.\n",
	    "It may be mined by hand fairly quickly, but using a shovel will significantly speed up the process.\n",
	    "Using a hoe on a placed block of dirt will turn it into a farmland block enabling seeds to be planted on it. Farmland will revert to dirt unless something is planted on it or it is irrigated by nearby water.\n",
	    "EMC: 1",
	    "Blast Resistance: 2.5",
	    "ID: 3"
    },
    ["Cobblestone 4"] = {
	    "Cobblestone is a common block used for creating various items, that resembles an uneven, roughly paved surface.\n",
	    "Cobblestone only occurs naturally in dungeons, NPC Villages, strongholds, or when water and flowing lava come into contact.\n",
	    "It is commonly used to create walls for constructions with a medieval appearance such as castles, towers and roads.\n",
	    "Cobblestone is obtained by mining stone.\n",
	    "Cobblestone is slightly harder to mine than normal stone, and there is an alternative version of the block known as Moss Stone.\n",
	    "Silverfish have the ability to enter and hide in cobblestone.\n",
	    "EMC: 1",
	    "Blast Resistance: 30",
	    "ID: 4"
    },
}

local function menu(...)
    local sel = 1
    local 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].."]") -- very customisible example print(">"..list[i])
		    else
			    print(" "..list[i].." ") -- very customisible
		    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

function drawTable(_table)
    yPos = 1
    while true do
	    term.setCursorPos(1, 1)
	    term.clear()
	    for i = yPos, #_table do
		    print(_table[i])
		    x, y = term.getCursorPos()
		    if y >= 19 then break end
	    end
	    x, y = term.getCursorPos()
	    msg = "Press 'x' to go back"
	    term.setCursorPos(screenX - #msg + 1, screenY)
	    write(msg)
	    term.setCursorPos(x, y)
	    e, key = os.pullEvent()
	    cX, cY = term.getCursorPos()
	    if e == "key" then
		    if key == 208 and cY >= 18 and yPos < #_table then
                -- Down
                yPos = yPos + 1
            elseif key == 200 and yPos > 1 then
                -- Up
                yPos = yPos - 1
            end
	    elseif e == "char" then
		    -- Exit all the text
		    if key == "x" then return end
	    end
    end
end

while true do
    term.setCursorPos(1, 1)
    term.clear()
    local selection = menu(
    "Stone 1",
    "GrassBlock 2",
    "Dirt 3",
    "Cobblestone 4",
    "Exit")
    if selection == "Exit" then
	    term.setCursorPos(1, 1)
	    term.clear()
	    break
    else
	    drawTable(t_MainTable[selection])
    end
end
Hydralisk1994 #8
Posted 03 January 2013 - 12:43 PM
I think using on.arrowUp() would work. I tried the keys and it didn't do anything.

if on.arrowDown()pressed then
– Down
yPos = yPos + 1

or something like that? I suck at programing :D/>
remiX #9
Posted 03 January 2013 - 12:45 PM
Huh? Didn't the code I posted in my previous post work?

It should… what does it do? O.o
Hydralisk1994 #10
Posted 03 January 2013 - 01:10 PM
I press the uparrow and nothing happens.

Nor does the down arrow work
remiX #11
Posted 03 January 2013 - 01:40 PM
Works fine with me with CC emulator.

Does the main menu work? With the list of Stone, grass, dirt, cobble stone and exit? Do the arrows work there? If they work there, then they should work for the next part.

Did you maybe change anything after copying?
Hydralisk1994 #12
Posted 03 January 2013 - 02:52 PM
CC emulator is a higher version of cc. Tekkit uses 1.2.5. The emulator uses 1.4.
remiX #13
Posted 03 January 2013 - 10:02 PM
Yeah I know, but when I used tekkit 1.2.5, keys worked fine.

Answer my previous question,

"Does the main menu work? With the list of Stone, grass, dirt, cobble stone and exit? Do the arrows work there? If they work there, then they should work for the next part."

Edit: Tested it in tekkit now and it's not working, hmm I'll try fix it now. Not sure why it's not working

Edit2: Fixed it and changed it a bit, the information of the blocks is stored into files which is then stored into a table, because it's just easier like that.

You will need http api to be enabled.

pastebin
Hydralisk1994 #14
Posted 04 January 2013 - 09:46 AM
Thanks! Ill test it!
Hydralisk1994 #15
Posted 04 January 2013 - 10:20 AM
pastebin link dosent bring me to anything
remiX #16
Posted 04 January 2013 - 10:28 AM
Hmm, weird. I must have put an expiration for it by mistake,

pastebin

edit:
Changed code a bit to make it much easier to add new pastebin codes with their block name.
All you do is add ["Blockname"] = "pastebin Code" into the t_PastebinCodes table.

Better version
Hydralisk1994 #17
Posted 04 January 2013 - 10:49 AM
I <3 U
Hydralisk1994 #18
Posted 04 January 2013 - 11:14 AM
Works perfectly!