0 votes

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.

asked Jun 11 in Engine by Matthew Hazlett (56 points)

1 Answer

0 votes

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
answered Jun 11 by Matthew Hazlett (56 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.