Search code examples
godotgdscript

Play animation without invoking setget?


I'm trying to play an animation of a custom property, but I don't want the setget function to be invoked

like in this case:

tool
extends Node2D

export(int) var example=0 setget set_example

func set_example(val):
    print("Here!")
    example=val

when I play the animation I just want the example value to be set but without invoking set_example()

is this or anything like this possible?


Solution

  • You can only avoid the setter from the same class (you it by not using self). So, you can make a second method that sets the variable, and then use that second method externally.

    tool
    extends Node2D
    
    export(int) var example=0 setget set_example
    
    func set_example(val):
        print("Here!")
        example=val
    
    func set_example_alt(val):
        example=val # The setter is not triggered here
    

    Note: The equivalent approach in Godot 4 beta does not work.


    If it must be a property, then you could have two properties using the same backing variable:

    tool
    extends Node2D
    
    export(int) var example=0 setget set_example
    
    export(int) var example_alt=0 setget set_example_alt, get_example_alt
    
    func set_example(val):
        print("Here!")
        example=val
    
    func get_example_alt():
        return example
    
    func set_example_alt(val):
        example=val
    

    Here the variable example_alt is not used. Instead, when another script (or the editor) uses example_alt it will be getting and setting example, but without triggering the setter.

    I see you have a related question: Avoid invoking setget function on starting up. I'll expand further there.