The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

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.

in Engine by (95 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
by (95 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.