Debounce: Difference between revisions
>NXTBoy |
>NXTBoy |
(No difference)
|
Revision as of 14:28, 12 July 2011
A Debounce system is a set of code that keeps a function from running too many times. It comes from the idea of mechanical switch bounce, where a switch bounces when pushed, creating multiple signals. In the context of Roblox, this problem occurs mainly with the Touched event, when a part touches another multiple times in a short space of time.
Theory
Let's say you have a button on the floor. When you jump on the button, it prints a message to the output. Your code would look like this:
game.Workspace.Button.Touched:connect(function(hit) print("Button pressed") --Print the message wait(1) --Wait for 1 second print("Hi :D") --Print another message end)
This will put this message in the output:
Button pressed Hi :D
The problem is that the physics engine, because of the way bricks touch each other, will not register just one collision, it may cause several Touched events to fire. So your output will look more like this:
Button pressed Button pressed Button pressed Button pressed Button pressed Hi :D Hi :D Hi :D Hi :D Hi :D
Rather than executing sequentially, all the event handlers execute at the same time.
Now if you're using a button for a Regeneration Script then it will make 5 of whatever you are regenerating. This is an issue because all 5 will be in the same spot causing all kinds of problems. So you can see the problem when you have a script without a debounce system.
Debouncing a system prevents this kind of problem. When an action happens, such as someone pressing your floor button, the script locks any new actions from that event until a time passes or the action is complete. It can be used for much more than just buttons, think of different ways you can use it for your scripts.
Use Case
It's fairly simple to convert an existing script to using debounce. Lets use the same script we had above, and add a couple of lines. In this case we will put in a time limit to wait for until the function can be run again.
local buttonPressed = false --Store whether the button is pressed in a local variable game.Workspace.Button.Touched:connect(function(hit) if not buttonPressed then -- Is it not pressed? buttonPressed = true -- Mark it as pressed, so that other handlers don't execute print("Button pressed") wait(1) print("Hi :D") -- Do Stuff buttonPressed = false -- Mark it as not pressed, so other handlers can execute again end end)
This will cause your output to look like this:
Button pressed Hi :D
That's more like it! You can use this same concept, by adding the same 4 lines to different scripts, in most any script involving functions. It doesn't even have to just be touched objects, it can be used to keep people from pressing a button more than once, firing a weapon more often than you want, or preventing a new event from happening before the old one is done. Take a look at the next example.
Real World
Here's the Local Gui script of the Rocket Launcher tool:
enabled = true function onButton1Down(mouse) if not enabled then return end enabled = false mouse.Icon = "rbxasset://textures\\GunWaitCursor.png" wait(12) mouse.Icon = "rbxasset://textures\\GunCursor.png" enabled = true end
When you fire a rocket, the script shows the reload icon. Then the function waits for 12 seconds. During this time, enabled is false, so if the player tries to fire another rocket, the script won't run because the function will just return right away. After the 12 seconds are up, the reload cursor goes away and enabled becomes true again, allowing the user to fire another rocket.
Advanced notation
After a while, it might get tedious defining a separate debounce variable for each event handler. Instead, you can write a debounce function, that returns a debounced copy of its first argument.
function debounce(func) local isRunning = false -- Create a local debounce variable return function(...) -- Return a new function if not isRunning then isRunning = true func(...) -- Call it with the original arguments isRunning = false end end end
Applying this to the original code:
game.Workspace.Button.Touched:connect(debounce(function(hit) print("Button pressed") --Print the message wait(1) --Wait for 1 second print("Hi :D") --Print another message end))