Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flash] Type coercion failure when loading maps from SharedObject #2649

Open
Oxdeception opened this issue Jun 28, 2023 · 1 comment
Open

Comments

@Oxdeception
Copy link

Describe the bug
Type coercion fails when attempting to use a map loaded from a SharedObject in the Flash target.

Steps to Reproduce

  1. Save a Map in a SharedObject
  2. Load SharedObject
  3. Attempt to use saved Map

Minimal Example

import openfl.events.Event;
import openfl.events.MouseEvent;
import openfl.net.SharedObject;
import openfl.display.Sprite;

class Main extends Sprite {
    var myMap:Map<String, Int> = ["test" => 1];
    public function new () {
        super();
        this.addEventListener(Event.ADDED_TO_STAGE, setup);
    }

    public function setup(e:Event) {
        this.stage.addEventListener(MouseEvent.CLICK, save);
        this.stage.addEventListener(MouseEvent.RIGHT_CLICK, load);
    }

    function save(e:Event) {
        var save = SharedObject.getLocal("test", "/");
        save.data.myMap = myMap;
        save.flush();
        trace("Saved");
    }

    function load(e:Event) {
        var load = SharedObject.getLocal("test", "/");
        trace(Type.getClassName(Type.getClass(load.data.myMap)));
        myMap = load.data.myMap;
        trace("Loaded");
    }
}

Expected behavior
Map is loaded from the SharedObject

Screenshots

TypeError: Error #1034: Type Coercion failed: cannot convert Object@2800c5302e51 to haxe.IMap.
        at Main/load()[/Projects/Haxe/issues/SharedObjectMaps/Source/Main.hx:28]

OpenFL Targets
Flash

Additional context
Does not occur on other targets.

@joshtynjala
Copy link
Member

With SharedObject and ByteArray, only the core AS3 language types can be serialized automatically. As I understand it, custom types need to implement flash.utils.IExternalizable to be restored when read from a SharedObject or a ByteArray (and i think maybe flash.net.registerClassAlias needs to be involved too).

Since haxe.IMap does not implement the IExternalizable interface, and we can't force it to implement that interface because it's a Haxe core language type that isn't under OpenFL's control, there probably isn't much that can be done to improve this situation. You may want to consider using a different type that can be serialized. For instance, Flash's Dictionary type is relatively similar to Map (though it isn't strictly typed like Map, so it's not a perfect replacement).

It may also be possible to create a custom IMap implementation that also implements IExternalizable. It could use a regular Map internally to store the data, and act as kind of a wrapper. I've never tried this myself, but it might be something to look into, if you really want to use a map, and would prefer to avoid another type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants