Defold-Protobuf

logo

Defold Protobuf

Defold NE for work with protobuf

Defold-Protobuf Native Extension for the Defold Game Engine

This extension allow you work with google protobuf protocol (files .proto), encode and decode them.

Github Link
Asset Link

Short API:

local protoc = require("pb.protoc")

protoc:loadfile("/resources/test.proto")
local data = {
	values = {
		first = {
		number = 1.5,
		unumber = 20,
		string = "hello"
	}
}

-- some.Example - name of message from test.proto
local bytes = pb.encode("some.Example", data)
local unpackage = pb.decode("some.Example", bytes)

Examples you can find in GitHub repository

Notes

This is extension, what we use in Family Age
If you will use our extension and find some bugs or have some suggestions, you are welcome, tell about it!
NE wrapped by @vergil12345678

24 Likes

Great work! I was planning to build NE for Flatbuffer and this will guide me a lot.
Thanks everyone from Family Age team for those great NEs

8 Likes

@Insality
Great work. This extention can save great effort when using pb.

I had tried this extention today and it worked well in most cases. There is only one finding: cann’t decode protobuf type “any”. For details, you can take a look here:

I’m testing this as a way to minimise data with Nakama. It’s very efficient, about four times the size of json in my initial tests. It doesn’t seem to work in HTML5, which makes sense since it’s not listed as a supported platform.

Could this extension be made to work for HTML5, or is that crazy talk?

2 Likes

Yes, it should be possible. There’s not much that is platform specific in the extension.

1 Like

It turns out it IS possible! I’ve implemented the game logic in the game and want to optimise the messages from json. So I went back to see where the HTML5 build failed. And now it doesn’t, it just works! I must have configured it incorrectly last time in some way. Apologies for the previous post. Protobuf is go!

2 Likes

Is it possible to encode nested tables with Protobuf?

This is an example of what I’d like to encode (all keys are strings except the indexed array under objects_hit_ttls):

{ --[[0x1206a0300]]
  objects_hit_ttls = { --[[0x1206a0350]]
    44 = { --[[0x1206a03f0]]
      1 = 0.61397633105516,
      2 = 0.62163049429655,
      3 = 0.622458358109,
      4 = 0.62388489693403,
      5 = 0.63753166496754,
      6 = 0.6444776058197,
      7 = 0.64909203350544,
      8 = 0.6500257730484,
      9 = 0.65298429131508,
      10 = 0.65339064002037,
      11 = 0.65379759073257
    }
  },
  objects_hit_types = { --[[0x1206a03a0]]
    44 = 1
  },
  projectile_type = 1
}

Using this .proto file:

syntax = "proto3";

package match;

message Salvo {
	int32 weapon_type = 1;
	float euler_z = 2;
	float power_fraction = 3;
	int32 seed = 4;
}

message ObjectTtls {
	map<string, float> value = 1;
}

message Damage {

	map<string, ObjectTtls> objects_hit_ttls = 1;
	map<string, float> objects_hit_types = 2;
	int32 projectile_type = 3;
}

Most data is preserved, except the indexed array:

{ --[[0x1206d3510]]
  objects_hit_ttls = { --[[0x1206d3560]]
    44 = { --[[0x1206d36b0]]
      value = { } --[[0x1206d3700]]
    }
  },
  objects_hit_types = { --[[0x1206d35f0]]
    44 = 1
  },
  projectile_type = 1
}

Yea, sure protobuf allow you use nested tables. In your example here is mismatch in protobuf structure and real data structure. You describe objects_hit_ttls as

objects_hit_ttls = { --[[0x1206d3560]]
    44 = { --[[0x1206d36b0]]
      value = { } --[[0x1206d3700]]
    }
  }

But pass values without value field. They are trimmed after encode/decode as you can see.

You can try pass data like this:

objects_hit_ttls = { --[[0x1206a0350]]
    44 = {
       value = { --[[0x1206a03f0]]
          1 = 0.61397633105516,
          2 = 0.62163049429655
        }
     }
}

Note that you use string as a key, but you pass numbers instead

2 Likes

That was what I was missing. Thanks!

Starting to look into this for a personal project of mine.
Does the extension work on Linux too? It’s not mentioned on the github page.

It works on Linux

3 Likes