Set value of a Resource variable so that it updates when changing other variables

Godot Version

4.2.1

Question

In a Resource, is it possible to have a variable tha changes its value according to other variables in the same resource?
Like for example, having 3 @export int variables: number1, number2, sum.
I want to make so that when I change one of the two numbers, the sum variable updates with the new value, but it is still possible to set the sum variable manually.
I know this is easy in nodes, but it seems I’m not able to make it work when modifying a Resource .tres instance directly from the inspector.

You can use a setter method:

@export var num1: int = 3:
    set(value):
        num1 = value
        sum = num1 + num2

@export var num2: int = 3:
    set(value):
        num2 = value
        sum = num1 + num2

@export var sum: int = 6:

I tried this method, but changing the numbers doen’t affect the sum in the inspector:

image

If you want code to execute in the editor, you have to write a @tool script. You can look up tutorials on it

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.