NURabbit Posted January 3, 2023 Share Posted January 3, 2023 (edited) 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 January 3, 2023 by NURabbit edit Link to comment Share on other sites More sharing options...
Yoarii Posted January 3, 2023 Share Posted January 3, 2023 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 More sharing options...
NURabbit Posted January 14, 2023 Author Share Posted January 14, 2023 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 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 Link to comment Share on other sites More sharing options...
NURabbit Posted January 14, 2023 Author Share Posted January 14, 2023 (edited) 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 January 14, 2023 by NURabbit Link to comment Share on other sites More sharing options...
NURabbit Posted January 14, 2023 Author Share Posted January 14, 2023 !@#!@#$ I got it working a bit!!! Now my problem is it only works on one of my refiners.. how can I make it work on all my ref. units at once? Link to comment Share on other sites More sharing options...
JohnNoGoodman Posted January 26, 2023 Share Posted January 26, 2023 Build an other Universe? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now