Question about get set

Godot Version

4.6

Question

A year and a half studying programming and Godot. I saw in a video that this set should work, but it doesn’t work for me. I don’t really understand how get and set work.

extends RigidBody2D

@export var texture:CompressedTexture2D:
	set(value):
		texture = value
		$Sprite2D.texture = value

It tells me that texture is null. How can I make this work?

what is set supposed to do? I cant find it anywhere in the documentation, but i just may not be looking hard enough :slight_smile:

Also, when writing a varaible like you are, it would look like this:

@export var texture = CompressedTexture2D

They’re called getters and setters, you can read more about them here!

1 Like

oooooooh okay thank you!

Who tells you that and when?
The script needs to be @tool if you want it to run in the editor.

What specifically are you trying to set? in the documentation it says you need a ‘get’ function as well before a set function, because both will be called.

also, this line:

set(value)
texture = value

Godot is expecting two argument for the set function. (for example, it says “milliseconds = value * 1000” in the documentation. value and 1000 are the arguments.) so i added a x1, and im getting no errors. Ill give you the code so you can try to see if it works for what you need :smiley:

extends RigidBody2D

@export var texture = CompressedTexture2D:
	set(value):
		texture = value * 1
		$Sprite2D.texture = value