Skip to content

Instantly share code, notes, and snippets.

@adam4813
Created July 14, 2015 04:38
Show Gist options
  • Save adam4813/e13bc84e87dcc4327c9e to your computer and use it in GitHub Desktop.
Save adam4813/e13bc84e87dcc4327c9e to your computer and use it in GitHub Desktop.
Protobuf example
syntax = "proto2";
package tec.proto;
message Entity {
required uint64 id = 1;
message Component {
message ComponentProperty {
enum Type {
BOOL = 0;
STRING = 1;
INT = 2;
FLOAT = 3;
}
required Type type = 1;
required string name = 2;
optional bool bvalue = 3;
optional string svalue = 4;
optional int64 ivalue = 5;
optional float fvalue = 6;
}
required string type = 1;
repeated ComponentProperty properties = 2;
}
repeated Component components = 2;
}
message EntityList {
repeated Entity entities = 1;
}
void Renderable::Out(proto::Entity_Component* target) {
proto::Entity_Component_ComponentProperty* prop = target->add_properties();
prop->set_type(proto::Entity_Component_ComponentProperty_Type_STRING);
prop->set_name("mesh_name");
prop->set_svalue(this->mesh_name);
prop = target->add_properties();
prop->set_type(proto::Entity_Component_ComponentProperty_Type_STRING);
prop->set_name("shader_name");
prop->set_svalue(this->shader_name);
prop = target->add_properties();
prop->set_type(proto::Entity_Component_ComponentProperty_Type_BOOL);
prop->set_name("hidden");
prop->set_bvalue(this->hidden);
}
void Renderable::In(proto::Entity_Component* source) {
for (int i = 0; i < source->properties_size(); ++i) {
const proto::Entity_Component_ComponentProperty& prop = source->properties(i);
if (prop.name() == "mesh_name") {
if (prop.type() == proto::Entity_Component_ComponentProperty_Type_STRING) {
this->mesh_name = prop.svalue();
}
}
else if (prop.name() == "shader_name") {
if (prop.type() == proto::Entity_Component_ComponentProperty_Type_STRING) {
this->shader_name = prop.svalue();
}
}
else if (prop.name() == "hidden") {
if (prop.type() == proto::Entity_Component_ComponentProperty_Type_BOOL) {
this->hidden = prop.bvalue();
}
}
}
}
@adam4813
Copy link
Author

Updated components

syntax = "proto2";

package tec.proto;

message Renderable {
    optional string shader_name = 1;
    optional string mesh_name = 2;
    optional bool hidden = 3;
}

message Position {
    optional float x = 1;
    optional float y = 2;
    optional float z = 3;
}

message Component {
    oneof component {
        Renderable renderable = 1;
        Position position = 2;
    }
}

message Entity {
    required uint64 id = 1;
    repeated Component components = 2;
}

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