![]() ![]() Trying to call the corresponding mutate method for mana on such data will return false, and the value won't actually be modified! For example, in the creation code above, the mana field is equal to 150, which is the default value, so it was never stored in the buffer. Fields are not present if they weren't set, or even if they happen to be equal to the default value. Note that any mutate functions on a table will return a boolean, which is false if the field we're trying to set is not present in the buffer. ![]() We use the somewhat verbose term mutate instead of set to indicate that this is a special use case, not to be confused with the default way of constructing FlatBuffer data.Īfter the above mutations, you can send on the FlatBuffer to a new recipient without any further work! Monster.mutate(inventory: 6, at index: 0) // mutates a value in an Scalar array Thus we could also write the Weapon table as: The scalar types can also use alias type names such as int16 instead of short and float32 instead of float. In our case, the root type is our Monster table. The root type declares what will be the root table for the serialized data. The last part of the schema is the root_type. It is also the only table referenced by the Equipment union. For our Monster, it is used to populate a vector of tables via the weapons field within our Monster. ![]() It is used twice: once within the Monster table and once within the Equipment union. The Weapon table is a sub-table used within our FlatBuffer. Be careful when using deprecated, however, as it may break legacy code that used this accessor. Since you cannot delete fields from a table (to support backwards compatability), you can set fields as deprecated, which will prevent the generation of accessors for this field in the generated code. Another thing to note is the line friendly:bool = false (deprecated). If unspecified, scalar fields (like int, uint, or float) will be given a default of 0 while strings and tables will be given a default of null. We specify some default values for fields, such as mana:short = 150. This will be used as the template to store our orc monster. The Monster table is the main object in our FlatBuffer. We use a struct here, over a table, because structs are ideal for data structures that will not change, since they use less memory and have faster lookup. If we had created multiple tables that we would want the union to be able to reference, we could add more elements to the union Equipment.Īfter the union comes a struct Vec3, which represents a floating point vector with 3 dimensions. The union in this example is not very useful, as it only contains the one table (named Weapon). Since the behavior of an enum is to increment if unspecified, Green will receive the implicit value of 1.įollowing the enum is a union. We specify Red = 0 and Blue = 2, but we do not specify an explicit value for Green. We have three values in this enum: Red, Green, and Blue. In this example, we have an enum of type byte, named Color. In our example, we have the Sample namespace inside of the MyGame namespace. This determines the corresponding package/namespace for the generated code. The schema starts with a namespace declaration. Let's examine each part of this schema to determine what it does. As you can see, the syntax for the schema Interface Definition Language (IDL) is similar to those of the C family of languages, and other IDL languages.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |