The TableTracker
class is a utility class that allows you to track changes to a table. You can bind a listener function to a specific path within the table, and the listener will be called whenever the value at that path is changed.
Here is an example of how to use the TableTracker
class:
-- Create a new TableTracker instance and set its initial value to a table with some initial data.
local tracker = TableTracker.new({
foo = {
bar = "hello"
}
})
-- Bind a listener to the "foo" key in the table.
local fooUnlistener = tracker:Bind({"foo"}, function(newValue)
print("The 'foo' key has changed to: " .. tostring(newValue))
end)
-- Bind a listener to the "bar" key within the "foo" key.
local unlistener = tracker:Bind({"foo", "bar"}, function(newValue)
print("The 'foo.bar' key has changed to: " .. tostring(newValue))
end)
-- Set the value at the "foo" key to a new table. This will cause the "foo" listener to be called.
tracker:Set({"foo"}, {
bar = "hello"
})
-- Set the value at the "foo.bar" key to "goodbye". This will cause the "foo.bar" listener to be called.
tracker:Set({"foo", "bar"}, "goodbye")
-- Unbind the "foo.bar" listener.
unlistener()
-- Set the value at the "foo.bar" key to "hi". This will not cause the "foo.bar" listener to be called, since it was unbound earlier.
tracker:Set({"foo", "bar"}, "hi")
The TableTracker
class has the following methods:
TableTracker.new(value)
This method creates a new TableTracker
instance. The value
parameter is optional and specifies the initial value of the tracked table. If no value is specified, the tracked table will be initialized to an empty table.
TableTracker:Bind(path, listenerFunc)
This method binds a listener function to the specified path
in the tracked table. The path
parameter is a table of keys representing the path to the value that the listener should be bound to. The listenerFunc
parameter is a function that will be called whenever the value at the specified path
changes. The function will be passed the new value as its only argument.
This method returns a TrackerListener
instance that can be used to unbind the listener from the TableTracker
instance.
TableTracker:Get(path)
This method gets the value at the specified path
in the tracked table. The path
parameter is a table of keys representing the path to the value that should be retrieved.
TableTracker:Set(path, value)
This method sets the value at the specified path
in the tracked table. The path
parameter is a table of keys representing the path to the value that should be set. The value
parameter is the new value that should be set at the specified path.
This method will cause any listener functions bound to the specified path
or any of its parent paths to be called with the new value.
Here are examples of how to use the Get
and Set
methods of the TableTracker
class:
-- Create a new TableTracker instance and set its initial value to a table with some initial data.
local tracker = TableTracker.new({
foo = {
bar = "hello"
}
})
-- Get the value at the "foo" key.
local fooValue = tracker:Get({"foo"})
-- The value of fooValue will be the table { bar = "hello" }.
-- Set the value at the "foo.bar" key to "goodbye".
tracker:Set({"foo", "bar"}, "goodbye")
-- Get the value at the "foo.bar" key.
local barValue = tracker:Get({"foo", "bar"})
-- The value of barValue will be "goodbye".
A data structure like the TableTracker
class can be extremely powerful because it allows you to track changes to a table in a very flexible and efficient way. This can be useful in a variety of situations, such as:
- When you want to be notified whenever a specific value in a table changes, so that you can take some action in response to the change.
- When you want to update other parts of your code whenever a specific value in a table changes, without having to manually check for changes yourself.
- When you want to track changes to a table that is shared by multiple parts of your code, and you want to ensure that all parts of your code are notified of any changes that happen to the table.
Overall, a data structure like the TableTracker
class can help you write cleaner, more maintainable code by providing a simple and efficient way to track changes to a table.
Download Link
Roblox Library: Table Tracker - Roblox
Roblox Model: TableTracker.rbxm (3.4 KB)