This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
askolds11's profile picture

Text and Touchpoint API

Started by askolds11, 07 July 2017 - 01:03 PM
askolds11 #1
Posted 07 July 2017 - 03:03 PM
Hello!

I've got two issues with this code, number one being that I've added text, it appears, but when i click a button it disappears.
Number two is pages not working.

Any help is greatly appreciated!


os.loadAPI("touchpoint")
tM = touchpoint.new("back")
tS = touchpoint.new("back")
tA = touchpoint.new("back")
m = peripheral.wrap("back")
--Pages
local t
turbine = nil
r = "N/A"

RF = {}
RPM = {}
trb = {}

trb[0] = peripheral.wrap("BigReactors-Turbine_0")
trb[1] = peripheral.wrap("BigReactors-Turbine_1")
trb[2] = peripheral.wrap("BigReactors-Turbine_2")
trb[3] = peripheral.wrap("BigReactors-Turbine_3")
trb[4] = peripheral.wrap("BigReactors-Turbine_4")
trb[5] = peripheral.wrap("BigReactors-Turbine_5")

function mainPage()
	 t = tA
end

function test()
	 print("test")
end

function changePage(changeTo) --Change Page
	 if t == tA or t == tM then
		  oldT = t
		   t = changeTo
	 else
		   t = oldT
	 end
end

function getRPM()
	 if turbine == nil then
		  r = "N/A"
	 else
		  r = trb[turbine].getRotorSpeed()
	 end
end

function information()
	 getRPM()
	 if t == tA or t == tM then
		  m.setCursorPos(1,1)
		  m.setBackgroundColor(colors.gray)
		  m.write("Turbine 1")
		  m.setCursorPos(1,2)
		  m.write("RPM:")
	 end
end



--Things happen from here
do --Buttons
	 tA:add("All Turbines", function() test() end, 52, 2, 65, 4)
	 tA:add("Turbine 1", function() turbine = 0 end, 47, 6, 57, 8)
	 tA:add("Turbine 2", function() turbine = 1 end, 59, 6, 69, 8)
	 tA:add("Turbine 3", function() turbine = 2 end, 47, 10, 57, 12)
	 tA:add("Turbine 4", function() turbine = 3 end, 59, 10, 69, 12)
	 tA:add("Turbine 5", function() turbine = 4 end, 47, 14, 57, 16)
	 tA:add("Turbine 6", function() turbine = 5 end, 59, 14, 69, 16)
	 tA:add("Overall", function() changePage() end, 59, 23, 69, 25)
	 tA:add("Manual", function() end, 59, 18, 69, 20)
	 tA:add("Auto", function() end, 47, 18, 57, 20)

	 tM:add("All Turbines", function() test() end, 52, 2, 65, 4)
	 tM:add("Turbine 1", function() test() end, 47, 6, 57, 8)
	 tM:add("Turbine 2", function() end, 59, 6, 69, 8)
	 tM:add("Turbine 3", function() end, 47, 10, 57, 12)
	 tM:add("Turbine 4", function() end, 59, 10, 69, 12)
	 tM:add("Turbine 5", function() end, 47, 14, 57, 16)
	 tM:add("Turbine 6", function() end, 59, 14, 69, 16)
	 tM:add("Overall", function() changePage(tS) end, 59, 23, 69, 25)
	 tM:add("Manual", function() changePage(tM) end, 59, 18, 69, 20)
	 tM:add("Auto", function() changePage(tA) end, 47, 18, 57, 20)
	 --tM:add("Power", function() end,
	 --tM:add("Coils", function() end,
     --tM:add("Reactor", function() end,

	 tS:add("Main", function() changePage(oldT) end, 59, 23, 69, 25)

end

mainPage() --set mode to auto on start
while true do
	 m.clear()
	 t:draw() --draw buttons
	 information() --RPMs, RF/t, etc.
	 local event, p1 = t:handleEvents(os.pullEvent())
	 if event == "button_click" then
		  t:run(p1)
	 end
end

Thanks!
Edited on 07 July 2017 - 01:10 PM
Bomb Bloke #2
Posted 08 July 2017 - 06:47 AM
Your loop down the bottom of the script clears the monitors every time it repeats. Since any event triggers another iteration, you'll never see the text from your test() function when pressing All Turbines (as it'll be removed the moment it's written).

You start on the tA page, and the Overall / Manual / Auto buttons there aren't calling changePage() correctly. Manual / Auto don't do anything, and Overall puts you on to "no page at all", silencing information().
askolds11 #3
Posted 08 July 2017 - 11:28 AM
Thanks for the response, but how would I fix these problems?
The test() function works fine as it prints to the terminal, but the m.print() in information() doesn't, I understand that every time I click a button it will clear the monitor, but shouldn't it print the things in information() after the clear?

EDIT: Oh, sorry, I didn't notice I only added the changePage() functions to the tM page, so that I should be able to fix once i next get on.
Edited on 08 July 2017 - 09:32 AM
askolds11 #4
Posted 09 July 2017 - 11:12 PM
Nevermind that previous reply I made - I think I just fully understood what you've said.

I've added a lot more code to my program, but now it's throwing an error "multiple points"

Could you possibly tell me what's wrong?

Current code: https://pastebin.com/thAUNuY6
(I'm sorry if some indents are different than others, I made all the new code outside minecraft, I just redid the indents in minecraft, but i might've missed some)

Thanks!
KingofGamesYami #5
Posted 09 July 2017 - 11:53 PM
Line 192:

         m.write("Turbine "..p+1..": "..RPM[p].." RPM "..RF[p].." RF/t") --Turbine's stats

Lua sees

1..

And thinks you're trying to make a decimal number with multiple decimals ("points"). Which isn't possible to do.
Lyqyd #6
Posted 10 July 2017 - 02:13 AM
You'll also need to replace the t:run line with:


t.buttonList[p1].func()
askolds11 #7
Posted 10 July 2017 - 11:20 AM
Current code: https://pastebin.com/usvieaPS

Thanks a lot for your help!

The program mostly works, but there are a few problems and things I don't know how to implement:
1. After some time(usually like 30 seconds), the screen begins to flicker like this (https://gfycat.com/B...fulSinfulAlbino)
2. When I click the button "All Turbines", it gives an error "trb:180: attempt to concatenate string and nil"
3. From line 239 to line 274

  if trb[0].getActive == true and t.buttonList.Turbine_1.inactive then
    t:toggleButton("Turbine 1")
  end

  if trb[0].getActive == false and t.buttonList.Turbine_1.active then
    t:toggleButton("Turbine 1")
  end
I've tried to add a "if this button is active but the turbine isn't, disable button" and vice versa, but I don't know how to do the button name since there are spaces in it.
Edited on 10 July 2017 - 09:21 AM
Bomb Bloke #8
Posted 10 July 2017 - 01:39 PM
1) Currently your code is rigged to start a new timer when any event occurs - including the expiry of an older timer (which you're no longer interested in because you've already created a new one!).

Say you click ten times - that's ten new timers, and as each expires another one gets created to take its place, ensuring that you'll always have at least ten timers running at once. That number only goes up as more clicks / keypresses / whatevers occur.

Set your first timer before the loop begins, and only set a new one when it expires or a valid click is registered. Likewise, those are the only times you should be redrawing the screen.

2) okCount is always going to be one higher than the number of turbines you've put in your turbineOk table, because you increment it after adding an entry - thus turbineOk[okCount] is pretty much always nil. Ditto for uhohCount / turbineUhOh.

3) You're using dot notation to index into each table there, but that obviously doesn't work if key names contain spaces (or certain other characters for that matter). Hence it's just a case of going back to square brackets:

t.buttonList["Turbine 1"].active

You could express the whole thing that way if you wanted to:

t["buttonList"]["Turbine 1"]["active"]

Note that no "inactive" key ever gets set, though. If you want to test for a button in that state, use:

not t["buttonList"]["Turbine 1"]["active"]

… or somesuch.
Edited on 10 July 2017 - 11:41 AM
askolds11 #9
Posted 10 July 2017 - 03:21 PM
Right, so using the information you gave me I've fixed the flickering, but i tried the square brackets but they didn't work

  if trb[0].getActive == true and not t.buttonList["Turbine 1"].active then
    t:toggleButton("Turbine 1")
  end

Also, I tried placing the uhohCount and okCount before the entry, but it still seems to crash just after printing "OK:"
Bomb Bloke #10
Posted 11 July 2017 - 01:40 AM
didn't work

So what exactly did happen?

Also, I tried placing the uhohCount and okCount before the entry, but it still seems to crash just after printing "OK:"

Did you also alter the loop that actually prints the new table structure? Basically you need to ensure that you're only trying to print table elements that actually exist.
askolds11 #11
Posted 11 July 2017 - 11:13 AM
https://pastebin.com/RdGhsz7D

1. Nothing happened, the buttons are still there but they didn't activate(they should've since the turbines were active).

2. I tried, but I don't know if I did it right - I changed the until to 0 not -1.
Bomb Bloke #12
Posted 11 July 2017 - 12:02 PM
1) Ah, that'll be because you're comparing "trb[1].getActive" (a function pointer) against "true" (a boolean value). You meant to call the trb[1].getActive function (to get its result) by sticking () after it.

You could actually simplify a bit by converting this sort of thing:

  if trb[0].getActive() == true and not t.buttonList["Turbine 1"].active then
    t:toggleButton("Turbine 1")    
  end
  .
  .
  .
  if trb[0].getActive() == false and t.buttonList["Turbine 1"].active then
    t:toggleButton("Turbine 1")
  end

… into just this:

  if trb[0].getActive() ~= t.buttonList["Turbine 1"].active then
    t:toggleButton("Turbine 1")
  end

2) It's still possible for turbineOk[okCount] to be nil if okCount is already 0 as your repeat loop begins. You could switch to a "while" loop, but a "for" loop is neater here:

         for i = 1, okCount do
           m.write(" #"..turbineOk[i])
         end

This way, the loop won't run its code at all unless okCount is at least 1 or more.
Edited on 11 July 2017 - 10:03 AM
askolds11 #13
Posted 11 July 2017 - 06:24 PM
https://pastebin.com/TsGqNPBi
Thank you!

Everything except 3 buttons work.
The buttons are "On", "Off", "Steam";

First of all, they don't do anything at all for some reason - They should set the status to either on, off or steam, which should change the auto mode.
Second, this might be because the first thing doesn't work, but I tried to set them up the same as turbine buttons:

if status == on and not t.buttonList["On"].active then
  t:toggleButton("On")
  if t:buttonList["Off"].active then
	t:toggleButton("Off")
  end
  if t.buttonList["Steam"].active then
	t:toggleButton("Steam")
  end
end

Is there anything wrong?
EDIT: Also, could it be possible to change the active/inactive colors while the program is running or would that require another page?
Another edit: Forgot to mention, that code made the information prints not work
Edited on 11 July 2017 - 07:40 PM
Bomb Bloke #14
Posted 11 July 2017 - 11:43 PM
Remember that on / off / steam sans quotes refer to variables (which you never define, so their values are nil), whereas "on" / "off" / "steam" with quotes are strings.

Your "mode" variable has a similar issue; setting it to "auto" will technically work, because that variable has a value (you've assigned a function pointer to it), but setting it to "manual" will flip it to nil again.
askolds11 #15
Posted 12 July 2017 - 09:34 AM
Thanks! I think everything works now, if I find something that I can't fix, I'll just ask here :)/>