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

Thread and Userinput problems with a IC² Fluid reactor script [GTNH]

Question

Hi,

i tried to program a simply UI Design with Heat informations of a Fluid Reactor and a simple Toggle on/off function.
but i had some problems with the refresh of the Heat display when i want the userinput. I Tried already with a thread and a new function the get the heat. but the mainprogram will still hold while it awaits the input.

and when it will easier to program i normally dont want an "Input" field normaly i dont want to display the userinput and simply toggle a redstone state from 15 to 0 and backwards by simply pressing the button (number on keyboard) but i dont know to do that :D

if anyone of you had an idea to fix that or improve that i am glad to hear from it!

my code i wrote on my own attempt.. connections to the Reactor made via an Adapter and a Redstone Port

-- Include

local term = require("term")
local component = require("component")
local sides = require("sides")
local event = require("event")
local thread = require("thread")

-- Variable
local r1 = 0
local build = "0910190124"
local vers = "0.0.2"
local rsget = component.redstone.getOutput(sides.west)

-- Boot-UP
term.clear()
print("Reactor Control")
print("Build: "..build.." Version:"..vers)
print("made by Blade")

-- function Reactor status
local function status1()
 if rsget == 15 then
  local r1 = 1
  return "Online!"
 else
  local r1 = 0
  return "Offline!"
 end
end

-- Script
print("Reaktor Status beim boot:")
term.setCursor(27,4)
print(status1())
os.sleep(5)

-- UI Design
term.setCursor(1,1)
term.clear()
print("|================================================|")
print("|                                                |")
print("| Status der Reaktoren:                          |")
print("| Reactor-1:                                     |")
print("| Reactor-2:                                     |")
print("| Reactor-3:                                     |")
print("| Reactor-4:                                     |")
print("| Reactor-5:                                     |")
print("| Reactor-6:                                     |")
print("|                                                |")
print("|------------------------------------------------|")
print("| [1-6] Toggle Reactor  |  [9] Exit Program      |")
print("|------------------------------------------------|")
print("| Input:                                         |")
print("|================================================|")

term.setCursor(14,4)
print(status1())
os.sleep(5)

local function heatget()
 while true do
  heat = component.reactor_redstone_port.getHeat()
  maxheat = component.reactor_redstone_port.getMaxHeat()
  term.setCursor(25,4)
  print("Heat: "..heat.." / "..maxheat)
 end
end

-- user input inclusive heat refresh
 thread.create(heatget())
  while true do
   event.pull()
   local userInput = io.read()
  end

 

thanks for the pataince and help :D

~ Blade8895

ps. i know my english is garbage but i try my best ;P

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

lua doesn't have preemptive threads, you have to have all threads (including the main thread) play cooperatively, everyone has to yield and place nice. plus, your code has some mistakes

1. heatget() calls heatget, it does not return a function pointer. thread.create(ptr) takes a function ptr. I think you meant thread.create(heatget)

2. heatget() never yields, you need something like os.sleep(0) in it.

3. having one thread print and a different thread read user input will cause the cursor to print over itself, and it'll look messy. you want your printing thread to be sleeping while you're taking user input. This might not be a multi-threaded program you need

4.I dont see a reason to call event.pull() before io.read() -- event.pull is going to block until ANY event, and io.read will then read key events. You could lose the first key press this way. I would just have io.read alone

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.