Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal

Molinko

Members
  • Content Count

    451
  • Joined

  • Last visited

  • Days Won

    35

Posts posted by Molinko

  1. local GUI = require("GUI")
    local component = require 'component'
    local thread = require 'thread'
    local event = require 'event'
    
    local reactor = component.reactor_chamber
    local currentTemps = reactor.getHeat()
    --------------------------------------------------------------------------------
    
    -- Create new application
    local application = GUI.application()
    
    -- Add panel that fits application
    application:addChild(GUI.panel(1, 1, application.width, application.height, 0x262626))
    -- Add smaller panels
    application:addChild(GUI.panel(4, 2, 23, 4, 0x880000))
    application:addChild(GUI.text(5, 3, 0xFFFFFF, "Current Reactor Temps"))
    
    -- Grab a reference to the text object you need to update
    local gui_temp = GUI.text(5, 5, 0xFFFFFF, currentTemps)
    application:addChild(gui_temp)
    application:draw(true) -- initial paint
    thread.create(application.start, application) -- Start the app event handler as a subprocess of this one
    
    while true do
      os.sleep(3) -- Update the element every 3 seconds
      gui_temp.text = tostring(reactor.getHeat())
      gui_temp:update() -- Signal the app that this element should be refreshed
    end

    This is just another attempt in case I missed something... If this doesn't work yet again, then could you run `cat /tmp/event.log` after running this (if it fails), and reply with its output if it's available. Threads and event timers don't throw errors in the main process.

     

  2. 1) yes. The quotes.

    2) autorun isn't depricated ( I don't think..) but you might like the .shrc format better.

    Run 'edit .shrc' in the home dir and add the path to the file you want to run on the first available line. It's super simple and can have multiple programs to run on startup. Also programs can take shell args just like typing it in yourself.

  3. I'm not super familiar with this library so you may have to adjust the code as it is untested.

    local GUI = require("GUI")
    local component = require 'component'
    local thread = require 'thread'
    local event = require 'event'
    
    local reactor = component.reactor_chamber
    local currentTemps = reactor.getHeat()
    --------------------------------------------------------------------------------
    
    -- Create new application
    local application = GUI.application()
    
    -- Add panel that fits application
    application:addChild(GUI.panel(1, 1, application.width, application.height, 0x262626))
    -- Add smaller panels
    application:addChild(GUI.panel(4, 2, 23, 4, 0x880000))
    application:addChild(GUI.text(5, 3, 0xFFFFFF, "Current Reactor Temps"))
    
    -- Grab a reference to the text object you need to update
    local gui_temp = GUI.text(5, 5, 0xFFFFFF, currentTemps)
    application:addChild(gui_temp)
    
    -- Create a timer that will run every 3 seconds, forever.
    local tid = event.timer(3, function()
        gui_temp.text = tostring(reactor.getHeat()) -- overwrite the text data
        gui_temp:update() -- notify the app that the state of gui_temp has changed
      end, math.huge)
    
    --------------------------------------------------------------------------------
    
    application:draw(true)
    application:start()
    event.cancel(tid) -- cleanup the timer after application:start returns

     

  4. The issue you see here is because `component` is removed from the global scope in OpenOS and must be required using the `require` function. If this file is intended to be run from an eeprom at boot instead of the standard Lua bios then `component` will still be global and this issue shouldn't remain. Running this program from the shell (as you're doing in the picture).. it will be provided an environment that doesn't contain the `component` namespace. This program seems to be intended to only run from a computer with seemingly multiple screens and gpus, cpu, redstone block(or card?), ram, and of course an eeprom. No hdd and no OS(no OpenOS floppy either). 

  5. If you could post all the code or explain what you're trying to do a bit more that would help me help you.

    Lua is single threaded so there isn't any real parallelization, so I'm having a hard time understanding what you need.

  6. Typically the GUI and your application are separated because of the reactive nature of the GUI library. Most of your code for reacting to GUI events will go into a GUIObjects event handler.

    -- # ...
    myButton.eventHandler = function(app, instance, event)
      if event == "touch" then
        -- # react to touch event. Alter state, etc...
      end
    end

    I don't have the time to test this my self but you could try using OpenOS threads to give control back to your main script.

    -- # ... more stuff up here
    local thread = require 'thread'
    
    -- # Gui logic n stuff...
    
    -- # This will now run whenever the main script yields i.e event.pulls, os.sleeps, etc..
    local guiProc = thread.create(application.start, application)
    
    -- # Continue below with main program runtime logic.
    -- # Be aware that the GUI will only be able to process stuff when this main script yields as Lua is single threaded

    Multiline text is handled by GUI.textBox rather than GUI.text as far as I can tell.

    Hope this helps you over the initial hump. Happy to help more if you still need it :) 

     

  7. Try this out..

    The require function is defined by OpenOS which isn't available in the drone env. 

    require is used to load libraries from the package path. See /lib/package.lua

    local drone = component.proxy(component.list('drone')())
    local position = component.proxy(component.list('navigation')()).getPosition()
    -- # The require keyword is for libraries accessed via a filesystem which the drone doesn't have. Therefor it isn't available.
    -- # The `component.someComponentName` functionality is defined in the OpenOS component library.. Again, not available here.
    
    if position ~= nil then
      drone.setStatusText("We in business bb")
    end
    
    while true do
      drone.move(0, 0, 0) -- to keep the drone running
    end

    Drones can be a bit of a pain to get used to as they dont have any of the sweet libs defined in OpenOS.

    See https://ocdoc.cil.li/tutorial:custom_oses to see what is available in a hardware only env like drones or and hw that is without an OS

  8. RC args are accessible from the start method in your startup.lua module under the args env name. Its a little complicated but basically when your startup rc module is loaded the rc.cfg variable with the same name (startup) is accessible as args in your module.

    -- # rc.cfg
    enabled = {"startup"}
    
    startup = {}
    -- # this table notation requires quotes btw
    startup["col0"] = 0xFF9200
    startup["col1"] = 0xFF6D00
    startup["col2"] = 0x000000
    startup["col3"] = 0xFFFFC0
    -- # /etc/rc.d/startup.lua
    local gpu = require("component").gpu
    
    function start(cfgColor, text)
      local color = args[cfgColor] -- # the `args` table here is your `startup` table as defined in /etc/rc.cfg
      local ofg = gpu.setForeground(color)
      print(text)
      gpu.setForeground(ofg)
    end
    
    -- # call start from the cmd line like so..
    -- > rc startup start col2 "this is colored text"

     

  9. The `require` function is implemented by the OS and is not in the default Lua environment. It needs a filesystem to search for packages and because the eeprom is basically a one file filesystem.. There's nowhere to look for packages. Either install OpenOS or see package.lua from OpenOS source to get an idea for how to write your own require function.

  10. There are a couple errors. The first is that the `while` keyword must be lowercase. Also within the `while` loop the if statement is missing the `end` keyword. I believe you'll also need to require the libraries you need before using them in a definition.

    local component = require "component"
    local sides = require "sides"
    
    local redstone = component.redstone
    local reactor = component.nc_fission_reactor
    
    local function GP()
      return reactor.getEnergyStored()
    end
    
    local mP, MP = 4000, reactor.getMaxEnergyStored()
    local MMP, CP = (MP / 4) * 3, GP()
    
    while CP > 0 do
      if CP > MMP then
        redstone.setOutput(sides.top, 15)
      elseif CP < mP then
        redstone.setOutput(sides.top, 0)
      end
      CP = GP() -- # don't forget to update the variable value within the loop
    end
    

     

  11. Yeah it would.. I missed that. Perhaps a nil check would be better.

    function m.addTrustedUser(user, level)
      if db.users[user] ~= nil then error "Cannot add an existing user!" end
      db.users[user] = level
      db.hasChanges = true
    end

     

  12. Overall the program looks pretty solid. Personally I like to write things with more descriptive names for when I have to revisit programs after a while. One place where things become unclear is using 'k' or 'v' in your pairs loops. If the keys and values are really arbitrary then this makes sense however several places in your code could be clarified by using better variable names. Here's an example..

    --- Distribute Json
    -- Disseminates the settings from the database to all turrets.
    function m.distribJson()
      -- # also db.turrets is an array so maybe ipairs would help a reader infer that. _ lets me see the index/key is irrelevant in this loop but perhaps some loop abstraction would be nice?
      for _,v in pairs(db.turrets) do -- # what exactly is v? maybe this chould be compId or turret_id?
        for _,player in pairs(toRemove) do
          component.invoke(v, "removeTrustedPlayer", player) -- # seems v is a uuid(component address)?
        end
     
        for user,priv in pairs(db.users) do -- # like this! this is more clear
          component.invoke(v, "addTrustedPlayer", user, priv)
        end
        for meth,bool in pairs(db.targets) do -- # bool could be more clear. perhaps should_engage or something that describes the variable purpose
          component.invoke(v, meth, bool)
        end
      end
      toRemove= {}
    end
    local privs = {
      [1] = true, -- # equal to admin
      [2] = false, -- # generic trusted priv
      admin = true, -- # named privs are nice to have
      trusted = false --# maybe 'user' is too generic? perhaps 'basic' or 'trusted'?
    }
    
    function m.addTrustedUser(user, level)
      if db.users[user] then error "Cannot add an existing user!" end
      db.users[user] = level
      db.hasChanges = true
    end
    
    m.addTrustedUser('molinko', privs[2]) -- # or privs.trusted this is nice and readable.
    m.addTrustedUser('Brodur', privs.admin) -- #  I dont have to look at the privs table to see whats happening...

    Overall your program looks fine. Personally I would just clarify(imply) whats happening a bit more with your variable names and change some to do the same.

    Nice program though :)

  13. @Log you beat me to it by about a minute :p. So I'll just add how to get a useful list OP mentioned.

    local component = require "component"
    local debug = component.debug
    local players = {}
    
    for _, name in ipairs(debug.getPlayers()) do
      local world = debug.getPlayer(name).getWorld()
      
      table.insert(players, {
          name = name,
          dimension = {
              name = world.getDimensionName(),
              id = world.getDimensionId()
            }
        })
    end
    
    print(players[1].name, players[1].dimension.name, players[1].dimension.id) --> "Molinko"	"overworld"		"0"

     

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.