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

Unsure how to switch between page instances in touchpoint

Started by plague006, 05 January 2015 - 07:50 PM
plague006 #1
Posted 05 January 2015 - 08:50 PM
I wrote a program (http://pastebin.com/rqQxwaPw) using the touchpoint API (http://pastebin.com/pFHeia96). No crashes (yay) and it's sending the proper redstone for Test 7-12 but pressing page 2 does nothing, while I'm expecting it to change pages: where did I go wrong?

I read through the touchpoint forum thread and I saw some example code of multiple pages

Thanks for any help/pointers.

My program only:
Spoiler

-- load the touchpoint API
os.loadAPI("touchpoint")

-- name/location of monitor
monName = "monitor_2"

-- the side to output redstone signal
rsDirection = "bottom"

-- establish two pages of buttons
local page_1 = touchpoint.new(monName)
local page_2 = touchpoint.new(monName)

-- one variable to put each page into in order to simplify switching
local t

-- functions to switch pages
function page1()
	t = page_1
end

function page2()
	t = page_2
end

-- array for targets
target = {
["Page 1"] = 0,
["Page 2"] = 0,
["Test 7"] = 1,
["Test 8"] = 2,
["Test 9"] = 3,
["Test 10"] = 4,
["Test 11"] = 5,
["Test 12"] = 6,
["Test 1"] = 7,
["Test 2"] = 8,
["Test 3"] = 9,
["Test 4"] = 10,
["Test 5"] = 11,
["Test 6"] = 12,
}

-- a function to send redstone for appropriate buttons
function rsOut()
  redstone.setAnalogOutput(rsDirection, target[p1])
end

do
	-- page 1
	-- left column
	page_1:add("Test 7", rsOut, 2, 2, 18, 4)
	page_1:add("Test 8", rsOut, 2, 6, 18, 8)
	page_1:add("Test 9",rsOut, 2, 10, 18, 12)
	
	-- right column
	page_1:add("Test 10", rsOut, 22, 2, 38, 4)
	page_1:add("Test 11", rsOut, 22, 6, 38, 8)
	page_1:add("Test 12", rsOut, 22, 10, 38, 12)
	page_1:add("Page 2", page2, 22, 16, 38, 18)


	-- page 2
	-- left column
	page_2:add("Test 1", nil, 2, 2, 18, 4)
	page_2:add("Test 2", nil, 2, 6, 18, 8)
	page_2:add("Test 3", nil, 2, 10, 18, 12)
	page_2:add("Page 1", page1(), 2, 16, 18, 12)
	
	-- right column
	page_2:add("Test 4", nil, 22, 2, 38, 4)
	page_2:add("Test 5", nil, 22, 6, 38, 8)
	page_2:add("Test 6", nil, 22, 10, 38, 12)
end


-- Starts with page 1, checks for button clicks, flashes them and runs the program it has
--page1()

t:draw()
while true do
  -- handleEvents will convert monitor_touch events to button_click if it was on a button
  local event, p1 = t:handleEvents(os.pullEvent())
  if event == "button_click" then
	t:flash(p1)
  end
end
Lyqyd #2
Posted 05 January 2015 - 10:19 PM
It shouldn't be sending the redstone when you click the buttons in that program since you never call any of the functions associated with the buttons. Throw this in your button_click handling:


t.buttonList[p1].func()

If you don't add functions to the other buttons, that will, of course, cause your program to crash if you try to click on one of the ones without functions, so you could add a quick check to ensure that there is actually a function first. It will also help to move the t:draw() into the while loop. If the redstone really is already working, moving the t:draw() might be all you need to do, but I somewhat doubt that.
plague006 #3
Posted 05 January 2015 - 10:55 PM
I wasn't (and still am not) having the easiest time keeping things synced between SSP, SMP, and pastebin, so you're totally right about the redstone. The version I thought I was posting had the redstone output in the button_click handling.

That aside, after implementing your changes and catching a few other mistakes I made it's running perfectly.

Thank you.