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

[Solved] Toggling a button

Started by MrFastZombie, 10 November 2014 - 07:39 PM
MrFastZombie #1
Posted 10 November 2014 - 08:39 PM
I've been using Direwolf's Button API to make a controller for my mob spawner, and I got everything working except for making the button toggle the bundled cable. I use an If and Else statement, but it won't turn it on, however, it seems to be reading the Else statement just fine.

Without the If/Else statement, it works fine. But the point of the statement is to make it toggle.

Dire's API

The Touch screen program:

http://pastebin.com/mCUUxQ7F (Full Code)


function lights()
  Button.toggleButton("Lights")

  if Button.toggleButton == true then
	p.setBundledOutput(1)
	print("Lights on")
  else
	p.setBundledOutput(0)
	print("Lights off")
  end
end
Edited on 11 November 2014 - 10:55 PM
Lyqyd #2
Posted 10 November 2014 - 09:06 PM
Looks like Button.toggleButton is a function, so it will never be equal to a boolean true. You'll need to use the active/inactive state of the button to compare against.
MrFastZombie #3
Posted 10 November 2014 - 09:42 PM
Looks like Button.toggleButton is a function, so it will never be equal to a boolean true. You'll need to use the active/inactive state of the button to compare against.
Thanks for the reply!

I am pretty new to code, how exactly would I do that?
Bomb Bloke #4
Posted 10 November 2014 - 11:15 PM
Dire's code doesn't expose that information to you, so you'd need to either edit his API or track it yourself.

On the other hand, you can see what your current redstone output state is, so it'd be simpler to track that:

  if p.getBundledOutput() == 0 then
        p.setBundledOutput(1)
        print("Lights on")
  else
        p.setBundledOutput(0)
        print("Lights off")
  end
MrFastZombie #5
Posted 10 November 2014 - 11:30 PM
Dire's code doesn't expose that information to you, so you'd need to either edit his API or track it yourself.

On the other hand, you can see what your current redstone output state is, so it'd be simpler to track that:

  if p.getBundledOutput() == 0 then
		p.setBundledOutput(1)
		print("Lights on")
  else
		p.setBundledOutput(0)
		print("Lights off")
  end

Thanks for the help, unfortunately though, it still does the same exact thing.
Bomb Bloke #6
Posted 11 November 2014 - 12:12 AM
Ah, see, this is where it might be a good idea to explain what it did in the first place. ;)/>

Also, I'm working under the assumption that you're using some sort of peripheral from another mod to go between your system and your cables - judging by your code, anyway. More details on exactly how you've wired things up wouldn't go amiss.
MrFastZombie #7
Posted 11 November 2014 - 12:54 AM
Ah, see, this is where it might be a good idea to explain what it did in the first place. ;)/>

Also, I'm working under the assumption that you're using some sort of peripheral from another mod to go between your system and your cables - judging by your code, anyway. More details on exactly how you've wired things up wouldn't go amiss.

Well, when I press the button it created on the touch screen, it always prints "Lights off", as I told it to when turning off the redstone signal. But it never turns it on again.

And yes, I do have a peripheral from another mod between the system and cables.
Bomb Bloke #8
Posted 11 November 2014 - 01:12 AM
Hmm. I'd try breaking the cable and the peripheral, then replace the cable then peripheral (in that order). If it's anything like OpenPeripheral's peripheral proxy, attempting to move things around in the wrong order can cause that kind of screwiness (apparently due to an issue on ComputerCraft's side).
MrFastZombie #9
Posted 11 November 2014 - 03:41 AM
Hmm. I'd try breaking the cable and the peripheral, then replace the cable then peripheral (in that order). If it's anything like OpenPeripheral's peripheral proxy, attempting to move things around in the wrong order can cause that kind of screwiness (apparently due to an issue on ComputerCraft's side).

I've just tried that, and it still seems to have the same issue.
Bomb Bloke #10
Posted 11 November 2014 - 03:54 AM
Depending on your build of ComputerCraft, it may be that the state-reading code just doesn't work. Apparently modders have had trouble getting the new bundled cable API to function correctly.

In which case you should still be able to track it externally:

local lights = {["state"] = false}

function lights()
  Button.toggleButton("Lights")

  if not lights.state then
        p.setBundledOutput(1)
        lights.state = true
        print("Lights on")
  else
        p.setBundledOutput(0)
        lights.state = false
        print("Lights off")
  end
end
MrFastZombie #11
Posted 11 November 2014 - 09:59 PM
Depending on your build of ComputerCraft, it may be that the state-reading code just doesn't work. Apparently modders have had trouble getting the new bundled cable API to function correctly.

In which case you should still be able to track it externally:

local lights = {["state"] = false}

function lights()
  Button.toggleButton("Lights")

  if not lights.state then
		p.setBundledOutput(1)
		lights.state = true
		print("Lights on")
  else
		p.setBundledOutput(0)
		lights.state = false
		print("Lights off")
  end
end
I've just tried this, and so far I am unsure if it works or not, but I have a good feeling. Issue is, now I am getting a new error when I press the button.

"Button:79: attempt to call nil"

On line 79 of the API is this:

data["func"]()

Now, I tried moving

local lights = {["state"] = false}
to the top of the code, but that gave me a different error:

"MobFarm:22: attempt to index ? (a function value)"

Which points to:

if not lights.state then

EDIT: I put the Local variable inside of the function, and now the button does activate the wire, but it will not turn them back off.
Edited on 11 November 2014 - 09:02 PM
Lyqyd #12
Posted 11 November 2014 - 10:15 PM
You were right to move the table declaration outside of the function, but it needs to be named something else. Otherwise, the function (also named lights) will simply overwrite the table value.
MrFastZombie #13
Posted 11 November 2014 - 10:39 PM
You were right to move the table declaration outside of the function, but it needs to be named something else. Otherwise, the function (also named lights) will simply overwrite the table value.

Yes! This seems to be working now. Thanks to both of you for your time an patience!