Custom Node and Exported Variables Issues

Godot Version

4.2.2 stable

Question

I’m relatively new to Godot but have been quickly knocking out small projects. Without getting into too much detail, after doing some research I decided I wanted to take a stab at creating my own “Tile” node. The idea being this could potentially be used by itself or with a TileMap or upcoming TileMapLayer in 4.3 to get the functionality I want for the game idea I have.

I’m familiar with instantiating a child scene, but wanted to use @tool and class_name to get as close as possible to developing my own node, as I feel that’s a good skill to have. This is the code I have so far:

@tool
class_name Tile extends Area2D

var Sprite := Sprite2D.new()
var Hitbox := CollisionShape2D.new()
var Center := Marker2D.new()

@export var sprite_file: CompressedTexture2D:
	set(texture):
		Sprite.set_texture(texture)
		notify_property_list_changed()

@export var collision_shape: RectangleShape2D:
	set(shape):
		Hitbox.set_shape(shape)
		notify_property_list_changed()

func _ready():
	add_child(Sprite)
	add_child(Hitbox)
	add_child(Center)

I can add a texture for the sprite and shape for the collision shape just fine but the Inspector doesn’t update with the selections. I searched and found notify_property_list_changed was suppose to be the solution but it isn’t working for me. Then I discovered that if I close the editor then reopen the project the texture and shape aren’t saved. I feel like these problems are related but after reading the documentation a ton and searching online I can’t figure it out. I’m going to go with instantiating a child scene since it’s a lot simpler and I am comfortable with that but I don’t want to just give up and not know what I was doing wrong.

You need to assign the exported variable a value

@export var sprite_file: CompressedTexture2D:
	set(texture):
        sprite_file = texture # <-- do this
		Sprite.set_texture(texture)

Do this for the others as well

1 Like

I feel so dumb, I think I was staring at it too long and missed something so basic. Thank you so much!

1 Like

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