Repository: ManimCommunity/manim
File: manim/mobject/value_tracker.py (Python)

Code before:
    --------
    .. manim:: ValueTrackerExample

        class ValueTrackerExample(Scene):
            def construct(self):
                number_line = NumberLine()
                pointer = Vector(DOWN)
                label = MathTex("x").add_updater(lambda m: m.next_to(pointer, UP))

                pointer_value = ValueTracker(0)
                pointer.add_updater(
                    lambda m: m.next_to(
                                number_line.n2p(pointer_value.get_value()),
                                UP
                            )
                )
                self.add(number_line, pointer,label)
                self.play(pointer_value.animate.set_value(5)),
                self.wait(0.5)
                self.play(pointer_value.animate.set_value(3))
                self.play(pointer_value.animate.increment_value(-2))
                self.wait(0.5)
                pointer_value += 1.5
                self.wait(1)
                pointer_value -= 4
                self.wait(0.5)

    """

    def __init__(self, value=0, **kwargs):
        Mobject.__init__(self, **kwargs)
        self.points = np.zeros((1, 3))
        self.set_value(value)

    def get_value(self):
        """Get the current value of the ValueTracker. This value changes continuously when :attr:`animate` for the ValueTracker is called."""
        return self.points[0, 0]

    def set_value(self, value):
        """Sets a new value (float, int) to the ValueTracker"""
        self.points[0, 0] = value
        return self

    def increment_value(self, d_value):
        """Increments (adds) a value (float, int) to the ValueTracker"""
        self.set_value(self.get_value() + d_value)

    def __iadd__(self, d_value):
        """adds `+=` syntax to increment the value of the ValueTracker"""
        self.increment_value(d_value)
        return self

Review comment (suggestion):
I think increasing the wait times would make the animation loop more smoothly.
```py
self.play(pointer_value.animate.increment_value(-2))
self.wait(1)
pointer_value += 1.5
self.wait(1)
pointer_value -= 4
self.wait(2)
```

Code after:
    --------
    .. manim:: ValueTrackerExample

        class ValueTrackerExample(Scene):
            def construct(self):
                number_line = NumberLine()
                pointer = Vector(DOWN)
                label = MathTex("x").add_updater(lambda m: m.next_to(pointer, UP))

                pointer_value = ValueTracker(0)
                pointer.add_updater(
                    lambda m: m.next_to(
                                number_line.n2p(pointer_value.get_value()),
                                UP
                            )
                )
                self.add(number_line, pointer,label)
                pointer_value += 1.5
                self.wait(1)
                pointer_value -= 4
                self.wait(0.5)
                self.play(pointer_value.animate.set_value(5)),
                self.wait(0.5)
                self.play(pointer_value.animate.set_value(3))
                self.play(pointer_value.animate.increment_value(-2))
                self.wait(0.5)
    """

    def __init__(self, value=0, **kwargs):
        Mobject.__init__(self, **kwargs)
        self.points = np.zeros((1, 3))
        self.set_value(value)

    def get_value(self):
        """Get the current value of the ValueTracker. This value changes continuously when :attr:`animate` for the ValueTracker is called."""
        return self.points[0, 0]

    def set_value(self, value):
        """Sets a new value (float, int) to the ValueTracker"""
        self.points[0, 0] = value
        return self

    def increment_value(self, d_value):
        """Increments (adds) a value (float, int) to the ValueTracker"""
        self.set_value(self.get_value() + d_value)

    def __iadd__(self, d_value):
        """adds `+=` syntax to increment the value of the ValueTracker"""
        self.increment_value(d_value)
        return self