Scope
Cleanup for one stay in a State. Everything added to a scope is cleaned up when the machine leaves that state, however it leaves: a normal transition, an interrupt, a timeout, Machine:ForceState or Machine:Stop.
Get the scope of the state whose callback is running with Machine:GetScope.
attack.OnEnter = function(machine)
local scope = machine:GetScope()
scope:Add(hitbox) -- hitbox:Destroy() on exit
scope:Add(track.Stopped:Connect(onEnd)) -- disconnected on exit
scope:Delay(0.3, function() -- never runs if the attack is interrupted first
machine:Send("Recovered")
end)
end
Properties
Active
This item is read only and cannot be modified. Read OnlyScope.Active: boolean
true until the state is left and the scope is cleaned up. A scope is never reused: entering the state
again gives it a new one.
Functions
Add
Scope:Add(item: T--
A function, thread, Instance, RBXScriptConnection, or a table with a Destroy or Disconnect method
) → T--
The same item, so it can be added inline
Cleans item up when the state is left: functions are called, threads cancelled, Instances destroyed,
connections disconnected, and tables have their Destroy (or Disconnect) method called.
Items are cleaned in reverse order of adding. Adding to a scope that was already cleaned cleans the item immediately.
local hitbox = machine:GetScope():Add(Hitbox.new(character))
Delay
Scope:Delay(callback: (...any) → (),--
Called with ...
...: any) → () → ()--
Cancels the timer
Calls callback after seconds of machine time, unless the state is left first. Timers are driven by
Machine:Update, so they never fire without it, and fire at the earliest on the next Update.
-- Open the cancel window 0.25s into the attack
machine:GetScope():Delay(0.25, function()
machine.Context.CanCancel = true
end)