ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: Arawra on Aug 03, 2020, 01:06 PM

Title: Creating a Monitor API
Post by: Arawra on Aug 03, 2020, 01:06 PM
I'm trying to create a monitor API, as I'd like to be able to add new functions to the existing peripheral API. However, the metadatatable for `peripheral` always returns null for me. I am not sure how to further going about doing this.

https://pastebin.com/83by8DWv
Title: Creating a Monitor API
Post by: Lupus590 on Aug 03, 2020, 06:31 PM
You are probably better off adding your methods directly to the peripheral unless you intend to overwrite some of its existing functions. In which case I would advise looking up meta table methods, epecially the __index one.
Title: Creating a Monitor API
Post by: QuickMuffin8782 on Aug 10, 2020, 05:53 AM
There is also a debug library of Lua, as shown here (http://lua-users.org/wiki/DebugLibraryTutorial).
You can also use it for debugging your programs, which I don't think everyone does that much...
Title: Creating a Monitor API
Post by: Lupus590 on Aug 10, 2020, 07:48 AM
You are using colon syntax when it looks like you don't need to, you might get success if you use dot syntax instead,
Title: Creating a Monitor API
Post by: QuickMuffin8782 on Aug 10, 2020, 10:05 AM
Quote from: Arawra on Aug 03, 2020, 01:06 PMI'm trying to create a monitor API, as I'd like to be able to add new functions to the existing peripheral API. However, the metadatatable for `peripheral` always returns null for me. I am not sure how to further going about doing this.

https://pastebin.com/83by8DWv

Your code is not supposed to use the colon syntax unless it's for some purposes. Most lua coders (some who are new don't know this), actually use the dot syntax instead of the colon syntax.

Your code is,
Monitor = {}
Monitor.__index = Monitor

function Monitor:new(side)
    if peripheral.getType(side) ~= "monitor" then
        error("ERROR: This peripheral is not a monitor! It's a " .. monitor.getType())
    end
    local monitor = {}
    print(type(peripheral))
    print(getmetatable(peripheral))
    setmetatable(monitor,Monitor)
    setmetatable(Monitor, peripheral.wrap(side))
    monitor.side = side
    monitor:getCursorPos()
    monitor:intialize()
    return monitor
end

function Monitor:initialize()
    monitor.clear()
    monitor.setCursorPos(1,1)
    monitor.setTextScale(.5)
end

function Monitor.getType()
    return peripheral.getType(self.side)
end

function Monitor.newLine()
    x,y = self.getCursorPos()
    self.setCursorPos(1,y+1)
end

monitor = Monitor.new("top")
monitor:newLine()
and it is supposed to look like this,
Monitor = {}
Monitor.__index = Monitor

function Monitor.new(side)
    if peripheral.getType(side) ~= "monitor" then
        error("ERROR: This peripheral is not a monitor! It's a " .. monitor.getType())
    end
    local monitor = {}
    print(type(peripheral))
    print(getmetatable(peripheral))
    setmetatable(monitor,Monitor)
    setmetatable(Monitor, peripheral.wrap(side))
    monitor.side = side
    monitor:getCursorPos()
    monitor:intialize()
    return monitor
end

function Monitor.initialize()
    monitor.clear()
    monitor.setCursorPos(1,1)
    monitor.setTextScale(.5)
end

function Monitor.getType()
    return peripheral.getType(self.side)
end

function Monitor.newLine()
    x,y = self.getCursorPos()
    self.setCursorPos(1,y+1)
end

monitor = Monitor.new("top")
monitor:newLine()
or it will throw in a error and it won't show in autocompletions.

A way to use colon syntax when you created monitors (like how the peripheral api does):
Monitor = {}
Monitor.__index = Monitor

function Monitor.new(side)
    if peripheral.getType(side) ~= "monitor" then
        error("ERROR: This peripheral is not a monitor! It's a " .. monitor.getType())
    end
    local monitor = {}
    print(type(peripheral))
    print(getmetatable(peripheral))
    setmetatable(monitor,Monitor)
    setmetatable(Monitor, peripheral.wrap(side))
    monitor.side = side
    function monitor.initialize()
        monitor.clear()
        monitor.setCursorPos(1,1)
        monitor.setTextScale(.5)
    end

    function monitor.getType()
        return peripheral.getType(self.side)
    end

    function monitor.newLine()
        x,y = self.getCursorPos()
        self.setCursorPos(1,y+1)
    end
    function monitor.print(txt)
        self.
    end
    monitor.getCursorPos()
    monitor.intialize()
    return monitor
end

As a result, I have made a improvement on your program that you are trying to make.
Click here to see the pastebin. (https://pastebin.com/r1RWEcib)