SharedObject doesn't keep the data after app is closed

Hello!

I’ve made a StorageManager class which saves the best score. Everything works fine until I close the app and reopen it, the class doesn’t save the best score after the app is closed.

Here is StorageManager.hx:

package utils;

import openfl.net.SharedObject;

class StorageManager {

public var score: UInt;
public var bestScore: UInt;

private var localStorage: SharedObject;

public function new() {

    localStorage = SharedObject.getLocal("around_data");

}

public function manageScore(_score: UInt): Void {

    score = _score;

    if (localStorage.data.bestScore == null) {

        localStorage.data.bestScore = score;
        localStorage.flush();

    } else {

        var _bestScore = localStorage.data.bestScore;
        if (_bestScore < score) {

            localStorage.data.bestScore = score;

            localStorage.flush();

        }

    }

    bestScore = localStorage.data.bestScore;

}

}

On what target are you testing?

Try replacing localStorage.flush(); by

if (localStorage.flush() == FLUSHED) { trace("flushed ok"); }
else { trace("problem during flush"); }

to see if the flush had any problems.

I’m targeting Android. The flush method works well.

Try to pass both arguments to SharedObject.getLocal(), like SharedObject.getLocal("around_data", "mycoolapp");

Also do you use -Dlegacy or not?

Passing both arguments doesn’t solve the problem.
No, I’m not using -Dlegacy, but if I use it the score is saved but now all the texts are left centered with the default font and the Sprites have a weird behaviour.

Strange. I’m using legacy, because it is stable.
TextFormatAlign.CENTER works fine for me in legacy, also everything is fine with sprites.
(legacy don’t work well on x86 androids, but work fine on arm)

Here is my one of my textfields:

    var format: TextFormat = new TextFormat();
    format.font = "fonts/Dense-Regular.ttf";
    format.size = Lib.current.stage.stageHeight / 5;
    format.color = 0xffc107;
    format.align = TextFormatAlign.CENTER;

    titleText = new TextField();
    titleText.text = "AROUND";
    titleText.defaultTextFormat = format;
    titleText.width = Lib.current.stage.stageWidth;
    titleText.height = format.size;
    titleText.x = 0;
    titleText.y = Lib.current.stage.stageHeight * 1 / 4 - titleText.height / 2;

    addChild(titleText);

It’s left centered, default font, and the size is really small. The color works well.

I use textField.embedFonts = true; to be able to use custom fonts. Other (textFormat.align , textFormat.size) works fine for me:

I don’t remember exact reason why I use textField.defaultTextFormat = textFormat; after setting all properties on textField, but before setting text. Maybe it is related, maybe not.

I just tested SharedObjects on Android, this worked:

var object = SharedObject.getLocal ("HelloWorld");

trace (object.data.hello);

object.data.hello = "LDSFKJ";
object.flush ();

The first time it traces null but it traces my text the second time I run it (even after reinstall)

Does it not work for you? If not, then could you share a code example that we could use to reproduce the issue? :slight_smile: