Unable to set value of one variable to a value of another

Godot Version

Godot 4.3

Question

Whenever I set variant to width or height in general, variant has no value

extends CharacterBody2D

@export_enum("2") var width: String
@export_enum("2","3","4","5") var height: String
var VariantsAndFrames = {
	"2x2":0,
	"2x3":1,
	"2x4":2,
	"2x5":3
}


var prevVelocity = Vector2.ZERO
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@export var gravity_multiplier: float = -0.00000980392*-350
@onready var animated_sprite = $AnimatedSprite2D

var variant = str(width, "x", height)
# Even if I set variant to just width, variant has no value
# var variant = width

var without any @ annotations are set at the very start, before any @export or @onreadys, Iā€™d recommend setting the value within _ready if more than one variable relies on another, but for just this one you could make it @onready to apply after @export

@onready var variant: String = str(width, "x", height)

If your variable always depends on some other variables and you never set it directly, then you can create a custom getter for it

var variant: String:
    get:
        return str(width, "x", height)
1 Like