colyseus / colyseus-haxe

⚔ Colyseus Multiplayer SDK for Haxe
https://docs.colyseus.io/getting-started/haxe-client/
MIT License
75 stars 16 forks source link

Error: refId not found #41

Closed serjek closed 4 years ago

serjek commented 4 years ago

This happens because previousValue is null, as it is pointed out in Schema:560. Suggestion is to replace this:

        //
        // FIXME: Type.getClass(previousValue)
        // This may not be a reliable call, in case the previousValue is `null`.
        //
        var collectionClass = (fieldType == null)
          ? Type.getClass(ref)
          : Type.getClass(previousValue); // CustomType.getInstance().get(fieldType)

with this:

        var collectionClass = (fieldType == null)
          ? Type.getClass(ref)
          : #if (nodejs || js) 
              CustomType.getInstance().get(fieldType) 
            #else 
              Type.getClass(previousValue) 
            #end;
endel commented 4 years ago

Thanks @serjek, a future approach we could take (which might involve changing the schema-codegen a bit) would be to use Type.resolveClass() to get the specific constructor of generic type from MapSchema/ArraySchema, like this:

(Playground link: https://try.haxe.org/#FC2A5)

@:keep
@:generic
class MyMap<T> {
    public var child: T;
    public function new() {}
}

@:keep
class MyItem {}

class Test {
    static function main() {
        var mapOfString = new MyMap<String>();
        var mapOfNumber = new MyMap<Int>();
        var mapOfItems = new MyMap<MyItem>();

        var instance = Type.createInstance(Type.resolveClass("MyMap_MyItem"), []);
        trace(Type.getClassName(Type.resolveClass("MyMap_MyItem")));
    }
}