Main Menu

Creating a Monitor API

Started by Arawra, Aug 03, 2020, 01:06 PM

Previous topic - Next topic

Arawra

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

Lupus590

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.

QuickMuffin8782

There is also a debug library of Lua, as shown here.
You can also use it for debugging your programs, which I don't think everyone does that much...
"The Blue Blur" - Working on Net Star OS, a new frontier for Red Star OS 3.0
linktr.ee/quickmuffin8782

Lupus590

You are using colon syntax when it looks like you don't need to, you might get success if you use dot syntax instead,

QuickMuffin8782

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.
"The Blue Blur" - Working on Net Star OS, a new frontier for Red Star OS 3.0
linktr.ee/quickmuffin8782