Trackers and Detecting Variable Changes

The Tracker class is a simple implementation of a tracker, which is a type of data structure that allows you to track the value of a variable and be notified whenever the value changes. It is defined as follows:

The Tracker.new() function takes an initial value and returns a new Tracker object.

The Tracker class has several methods, including Bind() , Set() , and Get() . The Bind() method allows you to attach a listener function to the tracker, which will be called whenever the value changes. The Set() method allows you to set the value of the tracker and notify all listener functions. The Get() method simply returns the current value of the tracker.

Here is an example of how you might use the Tracker class:

local myTracker = Tracker.new(0)

local unlistener = myTracker:Bind(function(value)
	print("The value of myTracker is now " .. tostring(value))
end)

myTracker:Set(1) -- prints "The value of myTracker is now 1"
myTracker:Set(2) -- prints "The value of myTracker is now 2"

unlistener()
myTracker:Set(3) -- does not print anything

In this example, we create a new Tracker object with an initial value of 0 . We then attach a listener function to the tracker using the Bind() method. Whenever the value of the tracker changes, the listener function will be called and will print out the new value. We then use the Set() method to change the value of the tracker twice, which will cause the listener function to be called twice. Finally, we use the unlistener() function returned by Bind() to remove the listener, and we set the value of the tracker again. This time, the listener function is not called because it was removed.

The Tracker class could be useful in a variety of situations where you need to track the value of a variable and be notified when it changes. For example, you might use a Tracker to track the position of a player in a game, and update the player’s position on the screen whenever the tracker’s value changes. You might also use a Tracker to track the state of a user interface, and update the UI whenever the tracker’s value changes.

Here is an example of how you might use the Subscribe() function and the Get() method of the Tracker class:

local myTracker1 = Tracker.new(0)
local myTracker2 = Tracker.new(0)
local myTracker3 = Tracker.new(0)

local unsubscribe = Tracker.Subscribe({myTracker1, myTracker2, myTracker3}, function(trackerThatChanged)
	print(“One of the trackers have changed”)
end)

myTracker1:Set(1) -- prints “One of the trackers have changed”
myTracker2:Set(2) -- prints “One of the trackers have changed”
myTracker3:Set(3) -- prints “One of the trackers have changed”

unsubscribe()
myTracker1:Set(4) -- does not print anything

In this example, we create three Tracker objects with initial values of 0 . We then use the Subscribe() function to attach a listener function to all three trackers. Whenever the value of any of the trackers changes, the listener function will be called and will print out the new value of the tracker. We then use the Set() method to change the value of each tracker, which will cause the listener function to be called three times. Finally, we use the unsubscribe() function returned by Subscribe() to remove the listener from all three trackers, and we set the value of one of the trackers again. This time, the listener function is not called because it was removed.

Here is an example of how you might use the Set and Get methods in the Tracker class:

-- Create a new Tracker to keep track of the player's score
local playerScoreTracker = Tracker.new(0)

-- Set the player's score to a new value
playerScoreTracker:Set(100)

-- Get the current value of the player's score
local currentScore = playerScoreTracker:Get()

-- Print the player's current score
print("Player's current score: " .. tostring(currentScore))

In this example, we create a new Tracker to keep track of the player’s score. We then use the Set method to set the player’s score to a new value, and the Get method to retrieve the current value of the tracker. Finally, we print the player’s current score to the screen.

As a first-time open-source contributor, I am excited to share my work with the community and hope that others will find it useful. The Tracker class is a powerful data structure that allows users to keep track of values and run functions whenever those values are changed. This can be useful in many different scenarios, such as tracking the state of a game, updating the UI in response to changes in data, or coordinating the behavior of different parts of an application. Overall, I am proud to contribute the Tracker class to the open-source community and hope that it will be a useful addition to the toolset of other developers.

Download Link

Roblox Library: Tracker - Roblox
Roblox Model: Tracker.rbxm (2.5 KB)

3 Likes