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

DireWolf Button API Help

Started by VintageGaming, 02 March 2014 - 01:39 AM
VintageGaming #1
Posted 02 March 2014 - 02:39 AM
I know how to use DireWolf20's Button API, but I do not know how to add another button action. There is "toggleButton" and "flash," and I want one where you can set 2 buttons to change between when you click on them. For example, you click on one button, and it is highlighted and runs code in a function, and if you press another defined button, it deselects the first one you pressed, and toggles the new one, along with a function. If somebody has found out a way to do this in the Button API (like "toggleButton" and "flash") it would be greatly appreciated! Thank you! ;D
RandomShovel #2
Posted 02 March 2014 - 08:29 AM
Hello, VintageGaming!

I'm not advanced in lua like other pros, but I've managed to make something to that extent. The function is called "toggleFlash".
Heres the code: tJybCZqU

If you want to know what every bit of the function does, open this spoiler.
Spoiler

function toggleFlash(name)
if #buttonHist > 0 then
  if name == buttonHist[1] then -- If you clicked the same button twice
   table.remove(buttonHist, 1) -- Clears last button clicked from the table
  else
   toggleButton(buttonHist[1]) -- Toggles the previously clicked button
   --print("    "..buttonHist[1]) -- A little debug line 
   table.remove(buttonHist, 1)
   table.insert(buttonHist, name)  -- If a button was clicked recently, it will be added to the table buttonHist
  end
else
  table.insert(buttonHist, name)
end
toggleButton(name) -- Toggles the button you just clicked
end
VintageGaming #3
Posted 02 March 2014 - 01:58 PM
You sir, just made my day. The program works absolutely perfect! You even added when you click the button that is currently selected, it goes off. There are NO bugs. I was just wondering if you can implement a bit where when you select a button, and click it again to turn it off, it can output a line of code, if you cannot do that, that is absolutely fine, bro. There are no problems with this code, and you are awesome. :D/>
RandomShovel #4
Posted 02 March 2014 - 06:59 PM
If I understand that post correctly, you want to run a function, or a line of code, when the button is "inactive"/"active"?

Lets say you have a button. If you click it, it does something and becomes "active". If you click it again, it does something different.

Is that correct?
Edited on 02 March 2014 - 07:58 PM
VintageGaming #5
Posted 02 March 2014 - 10:20 PM
Yes,that is correct.
RandomShovel #6
Posted 02 March 2014 - 11:02 PM
With that in mind, I managed to code a basic check into that monstrosity of an API. I even attempted to format and fix it a bit. :D/>

Instructions and Explanations in spoiler
SpoilerIntroducing the new function!

function isActive(bName)  -- Replace bName with the button's name
local on = button[bName]["active"]  -- If "bName" is active, return true
if on == true then
  return true
else -- If "bName" isn't, return false
  return false
end
end
Now to use this new function!
If you're button is named button, put "button" in the parenthesis as shown below.

function doSomething()
if button.isActive("button") then
  -- Code here
else
  -- Code here
end
end

Remember to put the "toggleButton", "toggleFlash", or "flash" before that if statement. If you don't thinks will go wonky.

Anyhow, the pastebin code is the same as the last :P/>
Code: tJybCZqU
Edited on 02 March 2014 - 10:02 PM
VintageGaming #7
Posted 06 March 2014 - 03:01 AM
Sorry for being late, but this code works amazing! Thanks for everything! :D/>