Skip to content

Better API for "if tween then kill" check #12629

@samsface

Description

@samsface

Describe the project you are working on

A game that relies heavily on tweens, tweens which frequently need to be stopped and restarted.

For example, when the player hovers over a UI element, a tween starts to scale it. If the player then moves the mouse to hover over a different UI element, we want to stop the original tween and reuse it to start scaling the new element instead.

Describe the problem or limitation you are having in your project

When reusing a tween, we must always type 3 lines of code:

if member_variable_tween:
   member_variable_tween.kill()

member_variable_tween = create_tween()

These three lines of code show up constantly in Godot projects that use tweens.

This leads to two main issues:

  • It’s tedious and repetitive to write.
  • It’s easy to forget the “if exists, then kill” check, which can introduce subtle bugs.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Let’s add a new function that lets us kill and create a tween with a one liner.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

# we can add a new method adjacent to create_tween
member_variable_tween = create_or_replace_tween(member_variable_tween)
# we can add a new param to create_tween, and this would kill the passed tween (I don't really like this option personally but it's shorter to write)
member_variable_tween = create_tween(member_variable_tween)

If this enhancement will not be used often, can it be worked around with a few lines of script?

N/A as this enhancement would be used often.

Is there a reason why this should be core and not an add-on in the asset library?

While it’s trivial to work around with a helper method, this issue is so common for Godot users that it deserves a first-class solution in the engine core.


I've read this similar proposal. Not a fan of the approach. Often, I want to rerun the tween but with slightly different parameters, so reusing the exact same tween isn't always ideal.
#8966

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions