Code Generation (MonoBehaviour classes)


Main purpose of these classes - is to be added as components to GameObjects, thus connecting these GameObjects to table's rows.

These classes are Unity standard components and can be added to any GameObject in the Inspector. After you added one of such classes to GameObject, you can choose which row should be connected, the correct table(meta) will be automatically selected


Using these components is not the only option to create a serializable reference to the database row. For other alternatives, read this section Referencing tables, fields, rows and cells from your own MonoBehaviour scripts

Difference from BGEntityGo

Without these classes you would use generic BGEntityGo component to hook up GameObject to database.

So, what is the difference between BGEntityGo and generated class?

  1. You don't have to chose meta- it'll be automatically chosen
  2. Instead of using basic API for accessing fields like this: var intValue = Get<int>("intField"); you have additional property generated for each field, so you can use code like this var intValue = intField;
  3. Additionally, you can choose to use classes, generated by ExtensionClasses generator, so you can easily navigate by relational fields. More information about it below.

Using classes, generated by ExtensionClasses

Question: why bother to use those classes? Answer: because of relational fields.

There are 3 fields in BGDatabase, that are relations (relationSingle,relationMultiple and nested). As a value they return the reference(s) to another row(s) (BGEntity).

Here is the simple example: Table1 has a relationSingle field called Relation, that points to a row from Table2.

Without using ExtensionClasses you can access Relation field value with such code

    BGEntity t2Value = t1.Relation;

So, if t1 - is first row of Table1, t2Value will be a first row of Table2, However it's type - is basic type: BGEntity. So you can access Table2.intField only with basic API like so: t2Value.Get<int>("intField")

That is certainly not what we want. So using ExtensionClasses makes relational value to be cast to be the type, generated by ExtensionClasses , e.g.

    Table2 t2Value = t1.Relation;
    var intValue = t2Value.intField;

How to generate

  1. Open BGDatabase window from Unity menu Window->BGDatabase
  2. Choose Settings->CodeGen
  3. Fill in parameters and press Generate button
Once you chose your naming scheme (Classes name prefix), generated the classes(components) and attached them to GameObjects, you should not change this setting, otherwise the references to these components will be lost. The same applies to tables names.

What is generated, exactly?

We will use MyTable as a table name (generated class name) and 2 fields (name string, gold int) for illustration purpose.

Method/property name Static Description
{field_prefix}{FieldName} No Properties are generated for each table field. Each property has the same type as corresponding database field. Use these properties to read/write values from/to database
MetaDefault Yes Use this property to get access to the table (BGMetaEntity class)
_{field_prefix}{FieldName} Yes Use this property to get access to the field (column) (BGField class)
Related {MetaName} ListUsing {RelationName} Relation No For each incoming relation a property is generated for traversing relation in reverse direction.


Parameter name Description
Code Generator Class to use for code generation (use default one)
Source Folder The folder to place generated C# classes (components) to. The folder must be inside you project
Package Package (C# namespace) for generated classes. For example MySpace. You can safely leave this setting blank.
Class names prefix Prefix to use for each class name. So if you use M_ prefix, the final class for MyTable table will be named M_MyTable. We have multiple code generators, so using different prefixes for different code generators helps to differentiate generated classes and avoid naming collisions
Field names prefix Prefix to use for each generated property name. So if you use f_ prefix, the final name for intField field will be f_intField. You can safely leave this setting blank.
Use classes, generated by CodeGen addon Turn it on if you use CodeGen addon. More information about this setting