How to use flatbuffers in cocos2d-x?

Hi all,
how can i compile flatbuffers schemas with cocos2d-x flatbuffers external library?
do i have to use actual flatbuffers library, i mean cloning the real flatbuffers repo and building that and then use it.

i’m lost here please help me.

Thanks.

Hi cocos2d-x already have fb library (v1.0) you can use it without any problem. Next cocos2d-x version will have v1.5. Or you can update cocos2d-x fb library to 1.5 (after that you need modify some cocos2d-x file that call fb functions with different params, no huge changes). To compile your schema you need flatc installed. Simply clone repo and compile or use brew (for osx) to install flatbuffers on your computer. Compile your schema (try to read fb official tutorials ans monster samples) they help you to understand all options. Include generated header for your schema in your project and start playing :wink: There is many options how to use them in cocos2d-x, it depend what you need. (ex. Comunicate with server, fill them with incoming json data, use them as data storage save/load for your game etc.) I use this to compile my schema ./flatbuffers/flatc --cpp --gen-object-api -o saveto/folder schematocompile.fbs (–gen-object-api I use v1.5, i think taht 1.0 doesnt have this option, this option is very usefull, easy object creation…)

Hi @elensar
Thanks for the info, i could compile my schema and made the monster_generated.h but i can’t make any monster or object with the header. visual studio shows me these errors :

classes\monster_generated.h(13): error C2079: 'FLATBUFFERS_FINAL_CLASS' uses undefined struct 'MyMonster::Monster'
classes\monster_generated.h(13): error C2143: syntax error: missing ';' before ':'
classes\monster_generated.h(13): error C2059: syntax error: ':'
classes\monster_generated.h(13): error C2059: syntax error: 'private'
classes\monster_generated.h(13): error C2143: syntax error: missing ';' before '{'
classes\monster_generated.h(13): error C2447: '{': missing function header (old-style formal list?)
classes\monster_generated.h(42): error C2027: use of undefined type 'MyMonster::Monster'
classes\monster_generated.h(13): note: see declaration of 'MyMonster::Monster'
classes\monster_generated.h(42): error C2065: 'VT_MANA': undeclared identifier
classes\monster_generated.h(45): error C2027: use of undefined type 'MyMonster::Monster'
classes\monster_generated.h(13): note: see declaration of 'MyMonster::Monster'
classes\monster_generated.h(45): error C2065: 'VT_HP': undeclared identifier
classes\monster_generated.h(48): error C2027: use of undefined type 'MyMonster::Monster'
classes\monster_generated.h(13): note: see declaration of 'MyMonster::Monster'
classes\monster_generated.h(48): error C2065: 'VT_NAME': undeclared identifier
classes\monster_generated.h(92): error C2672: 'flatbuffers::Verifier::VerifyBuffer': no matching overloaded function found
classes\monster_generated.h(92): error C2780: 'bool flatbuffers::Verifier::VerifyBuffer(void)': expects 0 arguments - 1 provided
cocos2d\external\flatbuffers\flatbuffers.h(798): note: see declaration of 'flatbuffers::Verifier::VerifyBuffer'

Can you please show me step by step how to upgrade flatbuffers in cocos2dx and use it?
I really need this feature for my game.

Thanks.

Hi, yes this error is due schema was generated with newer version and your cocos fb is v1.0.
Simply replace flatbuffers sources in your project cocos2d-x/external/flatbuffers. Find the compile errors, some fb calls in cococos2d-x sources need to be little changed. If you compile for android you need to change flatbuffers/Android.mk some files are added. You may look into to https://github.com/cocos2d/cocos2d-x/pull/17349 what is changed or compare changed files with your project. The best will be download the latest cocos2d-x from the sources, already have fb v1.5 and try in your VS.

1 Like