Code Generation (MonoBehaviour classes)

Description

Main purpose of these classes- is to be added as components to GameObjects, thus hooking 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 chose which row should be connected, the correct table(meta) will be automatically selected

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 chose 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 casted to be the type, generated by ExtensionClasses , e.g.

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

How to enable/disable

1) Open BGDatabase window from Unity menu Window->BGDatabase

2) Chose Settings->CodeGen

3) Toggle enabled on and fill all parameters:

  1. Code Generator - leave it as it is
  2. Source folder - unlike other types of CodeGeneration, these classes requires separate source .cs file to be used for each class. Chose the folder to put these files in
  3. Package - package (namespace) for you classes.
  4. Classes names prefix - prefix for your classes names. For example you can use prefix like D_ to distinct your database classes from the rest of the project classes
  5. Field names prefix - the same as classes names prefix, but for fields.
  6. Use Addons classes - Use classes, generated by ExtensionClasses generator. This is highly recommended to enable this generator and use those classes, the explanation why is : Using classes, generated by ExtensionClasses

After you fill all parameters, press Generate button to generate classes.

Note, you should regenerate these classes after each change to database structure (e.g. if you add/remove tables or fields, or changing their names)