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

Updating items on screen

Question

Greetings,

I'm new to Lua but familiar with some of the basics. I'm using this library for the GUI, but not sure on the proper procedure on updating the items on the screen. I have an adapter attached to an IC2 fluid reactor and want to call reactor.getHeat() to get the latest temps and update this child:

application:addChild(GUI.text(5, 5, 0xFFFFFF, currentTemps))

If someone can kindly point me in the right direction I think I can go from there. The rest of my code is pretty basic which I haven't played around much on it.

local GUI = require("GUI")
local component = require 'component'
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"))
application:addChild(GUI.text(5, 5, 0xFFFFFF, currentTemps))

--------------------------------------------------------------------------------

application:draw(true)
application:start()

 

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

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

 

Link to post
Share on other sites
  • 0
On 7/14/2019 at 2:08 PM, Molinko said:

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

 

Thank you for your help. Didn't seem to want to update that variable. It will get the first reading when you first run the program up but doesn't want to update properly after that. I may have to refer to the library creator on this.

Link to post
Share on other sites
  • 0
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.

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

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