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

How to set a buttons current active [Direwolf20 Button API]

Started by Noobular, 29 March 2015 - 07:00 AM
Noobular #1
Posted 29 March 2015 - 09:04 AM
Using direwolf20's button api i have came across a problem where the toggled button and the boolean are not syncing up
Boolean being something being on and off compared to the active status on the button

is there any way i can check and change the current active on a button to match up with the comparative.

BUTTON API


local r = peripheral.wrap("right) -- this is the reactor im talking about


function ReactorSwitch1() -- On/Off Switch
  if r.getActive() then
	r.setActive(false)
	print("Reactor Deactivated")
  else
	button.toggleButton("SWITCH")
	print("Reactor Activated")
	r.setActive(true)
  end

I should point out I already notice that i didnt add a switch in the first part of the if statement , but the point was to try and sync them up so its not possible at that point. unless I make a seperate if to try and bounce it back .
Dragon53535 #2
Posted 29 March 2015 - 09:26 AM
Two things.

One, it would be beneficial to use the Touchpoint API created by one of the forum's admins Lyqyd. Touchpoint

Two, you forgot a quote on your first line.


Now for the helping, I'm assuming you're trying to get it to change the button every time it is pressed. If not, I'll add help for that in a bit.
What seems to be is that button.toggleButton should work correctly, and swap the colors of the buttons.

If you want it to force the button to be off or on, then you're going to need to edit the togglebutton part of the code to allow for a second argument and if that argument exists, then change to the state defined by a boolean value.


function toggleButton(name,state)
  if type(state) == "boolean" then
	  button[name].active = state
  else
    button[name]["active"] = not button[name]["active"]
    screen()
  end
end  
Edited on 29 March 2015 - 07:28 AM
Noobular #3
Posted 29 March 2015 - 09:49 AM
I didnt copy directly atleast the top part lol i grabbed the function from mine and just retyped that lol
Noobular #4
Posted 29 March 2015 - 09:57 AM
It does infact normally swap colors its just that when the pc is loaded up its not always set to the right active as the machine is.


i tried to refer to it as button.toggleButton("SWITCH",true) it does still switch it , altho changing colors doesn't work. no errors.
GopherAtl #5
Posted 29 March 2015 - 01:13 PM
button.toggleButton ignores that 2nd arg, it always toggles the state. The problem is when the chunk unloads and reloads, the reactor will remember if it was on/off but your program doesn't, and the program launching will always start with the button false.

After you define your button, but before you enter your main loop, you should do this:


  if r.getActive() then
    button.toggleButton("SWITCH")
  end

So only if the reactor is active, you toggle the button from it's initial state - always false - to true.
Noobular #6
Posted 29 March 2015 - 01:39 PM
button.toggleButton ignores that 2nd arg, it always toggles the state. The problem is when the chunk unloads and reloads, the reactor will remember if it was on/off but your program doesn't, and the program launching will always start with the button false.

After you define your button, but before you enter your main loop, you should do this:


  if r.getActive() then
	button.toggleButton("SWITCH")
  end

So only if the reactor is active, you toggle the button from it's initial state - always false - to true.

Thanks for helping was eventually going to do that lol
Dragon53535 #7
Posted 31 March 2015 - 05:55 PM
I realize the problem you were having with the code I was helping you with,

If you see at the post I made above, inside the if part, there was no screen() call, which is why it didn't update. It technically updated, but didn't show it's update.



function toggleButton(name,state)
  if type(state) == "boolean" then
	  button[name].active = state
      screen()
  else
	button[name]["active"] = not button[name]["active"]
	screen()
  end
end