Simple tool errors when saving

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Matthew Hazlett

I am new so forgive me if this is a silly question.

I created a tool in a tscn file with an exposed property called SpriteIndex. I created a setter that updates the sprites frame property in the editor. It works, but whenever I save my project it gives me the error (times the number of tiles I have)

res://Tiles.gd:14 - Invalid set index ‘frame’ (on base: ‘Nil’) with value of type ‘int’.
(Line 14 is sprite.frame = value)

The code I have is very simple:

tool
extends StaticBody2D
export(int) var SpriteIndex setget setNdx
onready var sprite = $Sprite

func _ready():
sprite.frame = SpriteIndex

func setNdx(value):
SpriteIndex = value
if Engine.editor_hint:
sprite.frame = value

Can anyone tell me what the error is and how to get rid of the error?
It works in the editor and when I launch the project.

:bust_in_silhouette: Reply From: Matthew Hazlett

I solved this, I don’t quite know why it works now but I changed the datatype to an enum.

tool
extends StaticBody2D

enum Names {TileLeft = 0, TileMiddle = 1, TileRight = 2, ladderBase = 3, LadderTop = 4}
export(Names) var spriteNdx = Names.TileMiddle setget setNdx

onready var sprite = $Sprite

func _ready():
	sprite.frame = spriteNdx

func setNdx(value):
	spriteNdx = value
	var s = $Sprite
	s.frame = spriteNdx