display.save

I create polygon, I then use display.save to save the polygon as a .png.

shape = display.newPolygon(endPoint.x, endPoint.y, points)

display.save(shape, { filename = “shape.png”, baseDir = system.DocumentsDirectory })

all working fine

now I need to load the shape back in immediately, 

shape2 = display.newImage(“shape.png”, system.DocumentsDirectory)

shape2.x = newX

shape2.y = newY

all working fine

The problem comes when I draw another polygon and save it. I use the same above code.

In the sandbox the shape is indeed being overwritten, I can visiblily see it.

But when I come to load it in again the loaded shape is the same as the first.

Now I know this is because of how the cached file is handled in order to save texture memory by only referencing the original “shape.png”.

So my question at last:

when I save the shape how can i save it as a unique png? I know i have to change the file name but how can i add a 1 onto the end of the shape filename:

example save the first shape created as shape.png, 2nd as shape2.png, 3rd as shape3.png etc

Then I have to load in the shape i just created.

The shapes are only ever used once.

If the first shape created that is overwritten could be loaded it would be simple as the code above attempts to do that.

thanks

ok , so if I do this:

display.save(shape, { filename = tostring(dateTime)…".png", baseDir = system.DocumentsDirectory })

it will save a unique value.

problem is how do I load that back in?

Maybe use a lookback table where you store filenames?

local filenames = {}

filenames[1].name = tostring(dateTime)…".png"

maybe like this?

ok , so if I do this:

display.save(shape, { filename = tostring(dateTime)…".png", baseDir = system.DocumentsDirectory })

it will save a unique value.

problem is how do I load that back in?

Maybe use a lookback table where you store filenames?

local filenames = {}

filenames[1].name = tostring(dateTime)…".png"

maybe like this?