【问题标题】:Accessing a Flatbuffers union in Python在 Python 中访问 Flatbuffers 联合
【发布时间】:2018-12-06 16:52:03
【问题描述】:

我的架构文件中有以下定义:

union UGeometry { Polygon, Point, Linestring }

table Point {
    point:Vec2;
}

table Polygon {
    points:[Vec2List]; 
}

table Geometry {
    g:UGeometry;
}

(删除了一些用于类型检查和其他内容的样板代码)

Geometry 表存储 Point、Polygon 和 LineString 类型的几何图形。我可以像往常一样在 C++ 和 Javascript 中访问它,例如在 Javascript 中,我使用以下内容获取 Polygon 类型:

var rawPolygon = flatBufGeometry.g( new storage.Polygon() );

但是,我在生成的 Python 代码中找不到这样的访问器。以下将不起作用:

rawPolygon = rawGeometry.G()(storage.Polygon.Polygon())

如何使用 Python 访问表中的 Flatbuffers 联合对象?

【问题讨论】:

    标签: python python-3.x flatbuffers


    【解决方案1】:

    这是 Google 的 monster.fbs 的示例,因为所有 flatbuffer 都具有相似的结构和生成的 python 文件。

    union Equipment { Weapon } // Optionally add more tables.
    
    
    table Monster {
      pos:Vec3;
      mana:short = 150;
      hp:short = 100;
      name:string;
      friendly:bool = false (deprecated);
      inventory:[ubyte];
      color:Color = Blue;
      weapons:[Weapon];
      equipped:Equipment;
      path:[Vec3];
    }
    
    table Weapon {
      name:string;
      damage:short;
    }
    
    root_type Monster;
    

    要访问武器,请尝试

    import MyGame.Sample.Equipment
    import MyGame.Sample.Weapon
    
    union_weapon = MyGame.Sample.Weapon.Weapon()
    union_weapon.Init(monster.Equipped().Bytes, monster.Equipped().Pos)
    

    来源: https://github.com/google/flatbuffers/blob/master/samples/monster.fbs https://github.com/google/flatbuffers/blob/master/samples/sample_binary.py

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-18
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      相关资源
      最近更新 更多