Code Examples

Examples

You can copy examples below to Start() method of some MonoBehaviour class and run it.

Working with meta data

        //number of tables
        int numberOfTables = BGRepo.I.CountMeta;


        //print table names
        BGRepo.I.ForEachMeta(meta => print(meta.Name));


        //create new table
        BGMetaEntity newTable = new BGMetaRow(BGRepo.I, "NewTable");


        //create new field
        BGField newField = new BGFieldInt(newTable, "NewField");


        //number of columns (fields) for table "Table"
        int numberOfFields = BGRepo.I["NewTable"].CountFields;


        //print field names for table "NewTable"
        BGRepo.I["NewTable"].ForEachField(field => print(field.Name));


        //type of field "Field" from Table "NewTable" (Int32)
        Type fieldType = BGRepo.I["NewTable"].GetField("NewField").ValueType;

Working with data


        //populate new table with 10 records
        for (var i = 0; i < 10; i++) newTable.NewEntity().Name ="Entity #" + i;


        //number of rows for table "NewTable"
        int numberOfRows = BGRepo.I["NewTable"].CountEntities;


        //find entities, which names contains '5'
        List entities = newTable.FindEntities(entity => entity.Name.IndexOf('5') != -1);


        //find entities, which names contains '5' and sort them by NewField
        List entities2 = newTable.FindEntities(entity => entity.Name.IndexOf('5') != -1, null,
                (e1, e2) => e1.Get("NewField").CompareTo(e2.Get("NewField")));

        //the same as above, (faster version)
        BGField fieldWithType = (BGField) BGRepo.I["NewTable"].GetField("NewField");
        List entities3 = newTable.FindEntities(entity => entity.Name.IndexOf('5') != -1, null,
                (e1, e2) => fieldWithType[e1.Index].CompareTo(fieldWithType[e2.Index]));

        //the same as above with code generation, (faster and better version)
        //List entities4 = NewTable.FindEntities(entity => entity.Name.IndexOf('5') != -1, null,
        //        (e1, e2) => e1.NewField.CompareTo(e2.NewField));


        //print all entities names
        newTable.ForEachEntity(entity => print(entity.Name));


        //print all entities names, which contains "5"
        newTable.ForEachEntity(entity => print(entity.Name), entity => entity.Name.IndexOf('5') != -1 );


        // query new table and increase NewField by 1, for each NewField value which is lesser than 5,
        // iteration is sorted by NewField
        newTable.ForEachEntity(entity =>
        {
            entity.Set("NewField", entity.Get("NewField") + 1);
        },  entity => entity.Get("NewField") < 5,
           (e1, e2) => e1.Get("NewField").CompareTo(e2.Get("NewField")));

        //the same as above (faster version)
        BGField fieldWithType2 = (BGField) BGRepo.I["NewTable"].GetField("NewField");
        newTable.ForEachEntity(entity =>
        {
            var val = fieldWithType2[entity.Index] + 1;
            fieldWithType2[entity.Index] = val;
        }, entity => fieldWithType2[entity.Index] < 5,
           (e1, e2) => fieldWithType2[e1.Index].CompareTo(fieldWithType2[e2.Index]));

        //the same as above (version with code generation, faster and better)
        // NewTable.ForEachEntity(entity => entity.NewField++, entity => entity.NewField < 5,
        //    (e1, e2) => e1.NewField.CompareTo(e2.NewField));