Getting a sprite Key

Is it possible to get a spritekey from an image? such as: a = readImage(“Folder:name”) a.spritekey?

What do you mean by sprite key?

@Briarfox you can create a table that associates sprite keys with images:

allKeys = {}

function cacheImageFromKey( key )
    local img = readImage(key)
    allKeys[img] = key

    return img
end

function keyForImage( img )
    return allKeys[img]
end

```


At the moment you can't do this, but I will see if I can add the following behaviour in an update:

local img = readImage(key)

img.spriteKey = key -- Store a custom field on img

```


We don't do this automatically because of the following scenarios:

a = readImage("Documents:MyImage")

saveImage("Documents:MyImage", nil) -- Delete "Documents:MyImage"

print(a.spriteKey) -- still prints "Documents:MyImage"

```


a = readImage("Documents:MyImage")

print(a.spriteKey) -- prints "Documents:MyImage"

b = readImage("Documents:OtherImage")

setContext(a)
sprite(b, a.width/2, a.height/2)
setContext()

print(a.spriteKey) -- still prints "Documents:MyImage"

```

Thanks @simeon, I was thinking about the above solution but unfortunatly it will not work where I need it. I need to use readImage() simply for the sprite picker. But this issue is temporary, I just need to build a sprite picker and use your above code and It’ll work.

If you decide to add .spriteKey in a future update it would be great!

I’m working on my own sprite picker after you said you wanted to make one. I love a good challenge, even if nobody uses it.

@SkyTheCoder I’d like to see it when your done.

@Briarfox It’s not perfect, but here’s an installer. Feel free to play around with it. And, I made the sprite picker a class for easy usage. https://gist.github.com/SkyTheCoder/5845834