Kotlin custom setter

I am trying to create a custom set of properties for the libGDX sprite.

var sprite : Sprite? = null
    get() = sprite
    set(s : String) { sprite = Sprite(Texture(s)) }

But he says that he smust have a type Sprite, is it possible to do what I am trying to do?

+4
source share
1 answer

First of all, you should use fieldinstead of spriteinside your custom getter / setter. Otherwise, the result will be a recursive call.

What you are trying to do does not work as the compiler says. You need to overload setter and add it as a regular method to your class as follows:

setSprite(s : String) { sprite = Sprite(Texture(s)) }
+2
source

Source: https://habr.com/ru/post/1688616/


All Articles