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

[CC 1.80pr1] Object-oriented API gives a table-related error

Started by Windows10User, 06 April 2018 - 03:31 PM
Windows10User #1
Posted 06 April 2018 - 05:31 PM
So, I'm creating an OS. I would LOVE to have the GUI drawing and stuff in a separate and object-oriented API, so I started working on that. When I coded the button object in the API, I try to draw a button through my bootloader, and it gives me this error: "loader.lua:4: attempt to index ? (a nil value)". This doesn't make sense, even after double-checking my code… It even seems to load the API just fine!

Program in which the API is used:

term.clear()
term.setCursorPos(1, 1)
os.loadAPI("DoorOS/apis/GUI/GUI.api")
local btn1 = GUI.button:new()
btn1:draw()

API:

button = {
    bkgColor = colors.blue,
    text = "Text",
    w = 6,
    h = 3,
    textx = 2,
    texty = 2,
    x = 2,
    y = 2,

    new = function(self)
        local new = {}
        setmetatable(new, {__index = self})
        return new
    end,

    draw = function(self)
term.setCursorPos(x, y)
        term.setBackgroundColor(self.bkgColor)
        for i=1, self.w do
            for i=1, self.h do
                term.write(" ")
                term.setCursorPos(self.x + i, self.y)
            end
            term.setCursorPos(self.x, self.y + 1)
        end
        term.setCursorPos(self.x + 2, self.y + 2)
        term.write(self.text)
    end,

cock = function(self)
print("Test")
end
}

Used tutorial: http://www.computercraft.info/forums2/index.php?/topic/14938-simple-object-oriented-programming/
Bomb Bloke #2
Posted 07 April 2018 - 01:43 AM
You're trying to index into GUI, but you've os.loadAPI()'d a file called "GUI.api". Indexing into GUI.api directly won't work, as Lua's notation rules would lead the interpreter to believe that's a table inside a table.

You could use:

_G["GUI.api"].button:new()

… but really it's simpler to just rename the file. Under CC1.80+, ".lua" extensions are automatically stripped when loading APIs. Under earlier versions, you're better off not using extensions at all.
Lupus590 #3
Posted 07 April 2018 - 11:45 AM
Have a look at this:
https://github.com/lupus590/CC-Random-Code/blob/8a5f12a2c42af4474b6e974878057a4495598a87/src/lua%20file%20extention%20support#L22


Edit: I think I copied the wrong thing, I moved the file just before posting this. Fix on the way.

Edit 2: fixed.
Edited on 07 April 2018 - 11:58 AM
Windows10User #4
Posted 07 April 2018 - 11:57 AM
You're trying to index into GUI, but you've os.loadAPI()'d a file called "GUI.api". Indexing into GUI.api directly won't work, as Lua's notation rules would lead the interpreter to believe that's a table inside a table.

You could use:

_G["GUI.api"].button:new()

… but really it's simpler to just rename the file. Under CC1.80+, ".lua" extensions are automatically stripped when loading APIs. Under earlier versions, you're better off not using extensions at all.

Thanks, mate! Will try it out.