Jump to content

Little help for a noob?


NURabbit
 Share

Recommended Posts

Hey, I have designed a simple setup I want to use to control a bunch of refiners...

the screen outputs either 'calcium' 'copper' or 'concrete' and this is what I have for the receiving end,,

oh and every linked refiner is named 'ref'

does not seem to work or spit out an error, due to my noobishness with coding, im sure its just structured wrong or something

 

-- Define actions to be done when receiving a keyword
local actions = {
    ['calcium'] = function() 
        ref.stop()
        ref.setCurrentSchematic("1358793857")
        ref.start()
    end,
    ['copper'] = function() 
        ref.stop()
        ref.setCurrentSchematic("1447143715")
        ref.start()
	end,
    ['concrete'] = function()
        ref.stop()
        ref.setCurrentSchematic("394971409")
        ref.start()
    end
}

-- Check if an action is defined
if actions[output] then
    -- Clear screen output
    screen.clearScriptOutput()

    -- Execute the action
    actions[output]()
end

 

 

Thanks in advance!!

Edited by NURabbit
edit
Link to comment
Share on other sites

Have you tried running the actions directly from the PB, without involving the screen? Does that work? I've not used the indy-side of Lua yet so I can't speak on its correctness, but it looks like it should work.

 

Also, check out #lua in official discord: https://discord.gg/dualuniverse as well as the #lua-help on the DU Open Source Initiative Discord https://discord.com/channels/760240626942869546/760240768924778547

Link to comment
Share on other sites

  • 2 weeks later...

Well let me reiterate. my main question is this part 

 

local actions = {
    ['calcium'] = function() 
        ref.stop()
        ref.setCurrentSchematic("1358793857")
        ref.start()
    end,

 

is that structured correctly that when 'calcium' is called, it will run the 3 ref. commands one after eachother? are my brackets correct?

I am totally not a programmer, so the terms for this stuff is still foreign to me :P

 

Once again - any help a godsend! I really want to make this a reality, as I spend SO much time screwing around with schematics..

also, if this IS correct, can the commands just be issued like that all in one shot, or would the industry unit require a delay between each?

I assume it will just queue the commands on its own for when its ready but.. once again, total nub :D

Link to comment
Share on other sites

Side note - this is the sum of what I have so far for my project..

 

Programming board - 'unit'  onStart =

screen.clearScriptOutput()

Screen is linked as the last slot following the refiners, for the screen, it has an 'onOutputChanged' filter of 

-- Define actions to be done when receiving a keyword
local actions = {
    ['calcium'] = function() 
        ref.stop()
        ref.setCurrentSchematic("1358793857")
        ref.start()
    end,
    ['copper'] = function() 
        ref.stop()
        ref.setCurrentSchematic("1447143715")
        ref.start()
	end,
    ['concrete'] = function()
        ref.stop()
        ref.setCurrentSchematic("394971409")
        ref.start()
    end
}

-- Check if an action is defined
if actions[output] then
    -- Clear screen output
    screen.clearScriptOutput()

    -- Execute the action
    actions[output]()
end

 

and the screen itself has the following code

 

--# Buttonclass definition called only at the first frame
if not Button then

    -- getEllipsis( font, text, maxWidth)
    -- Return a shorten text string by with with ... at the end
    function getEllipsis(font, text, maxWidth)
        local width = getTextBounds(font, '...')

        for i = 1, #text do
            local line = getTextBounds(font, text:sub(1,i)) + width
            if line > maxWidth then
                return text:sub(1,i-1)..'...'
            end
        end
        return text
    end


    Button = {}
    Button.__index = Button
    -- Button object constructor
    -- .x : X component of the position
    -- .y : Y component of the position
    -- .width : Width of the button
    -- .height : Height of the button
    -- .caption : Associated text caption
    -- .onClick : Function called when the button is clicked
    function Button:new(x, y, width, height, caption)
        local self = {
            x = x or 0,
            y = y or 0,
            w = width or 100,
            h = height or 20,
            caption = caption or "",
            onClick = nil
        }

        -- Draws the button on the screen using the given layer

        
        function self:draw(layer, font)
            -- Localize object data
            local x, y, w, h = self.x, self.y, self.w, self.h
            local min, max = self.min, self.max

            -- Get cursor data
            local mx, my = getCursor()
            local down = getCursorDown()
            local released = getCursorReleased()

            local clicked = false
            -- Determine if the cursor is on the button and switch the state
            if (mx >= x and mx <= x+w) and (my >= y and my <= y+h) then
                
                if down then
                    clicked = true
                    
                -- Call the onClick function when the mouse button is released
                elseif released then
                    if self.onClick then
                        -- Provide cursor position in arguments
                        self:onClick( mx, my)
                    end
                end
                
            end

            --# Draw the button
            -- Define box default strokes style
            setDefaultStrokeColor(layer, Shape_BoxRounded, 1, 1, 1, 1)
            setDefaultStrokeWidth(layer, Shape_BoxRounded, 0.1)

            -- If the button is clicked change the background
            if clicked then
                setNextFillColor(layer, 0.1, 0.1, 0.1, 1)
            else
                setNextFillColor(layer, 0.3, 0.3, 0.3, 1)
            end
            addBoxRounded(layer, x, y, w, h, 1)

            -- Draw caption and value display          
            local caption = getEllipsis(font, self.caption, w-12)
            local font = font or nil

            setNextTextAlign( layer, AlignH_Center, AlignV_Middle)
            addText( layer, font, caption, x+0.5*w, y+0.5*h)
        end


        return setmetatable(self, Button)
    end

end


-- Get screen resolution
local rx, ry = getResolution()
local medium = loadFont('Play', 20)

--# Initialization called only at the first frame
if not _init then

    -- Draw the 3 buttons
    buttonRed = Button:new(0.5*rx - 100, 0.35*ry - 16, 220, 42, "Refine - Calcium")
    buttonGreen = Button:new(0.5*rx - 100, 0.5*ry - 16, 220, 42, "Refine - Copper")
    buttonBlue = Button:new(0.5*rx - 100, 0.65*ry - 16, 220, 42, "Refine - Concrete")
    
    buttonRed.onClick = function(self) setOutput('calcium') end
    buttonGreen.onClick = function(self) setOutput('copper') end
    buttonBlue.onClick = function(self) setOutput('concrete') end

    _init = true
end

--# Rendering
local layer = createLayer()

-- Draw buttons
buttonRed:draw( layer, medium)
buttonGreen:draw( layer, medium)
buttonBlue:draw( layer, medium)

-- Request a run at each frame
requestAnimationFrame(1)




 

Edited by NURabbit
Link to comment
Share on other sites

  • 2 weeks later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...