` test('Protobuf roundtrip', () { income.value = 20000; income.onTick(); final…
- User
test('Protobuf roundtrip', () { income.value = 20000; income.onTick(); final beforeRoundtrip = income.earned; final pb = income.toProtobuf(); income = Income(composite: composite)..imbueData(pb); final afterRoundtrip = income.earned; expect(afterRoundtrip, beforeRoundtrip); }); }); - Assistant
This code snippet is a test case for a protobuf roundtrip functionality.
The test case sets the value of an
incomeobject to 20000 and calls theonTick()method. Then, it stores theearnedvalue of theincomeobject before the roundtrip in a variable calledbeforeRoundtrip.Next, the
toProtobuf()method is called on theincomeobject to convert it to a protobuf representation, which is assigned to a variable calledpb.Then, a new
incomeobject is created using thecompositeconstructor parameter and the data from the protobuf representation is applied to it using theimbueData()method.The
earnedvalue of the newincomeobject after the roundtrip is stored in a variable calledafterRoundtrip.Finally, an assertion is made to check if the
afterRoundtripvalue is equal to thebeforeRoundtripvalue.This test case ensures that the roundtrip functionality correctly preserves the
earnedvalue of theincomeobject when converting it to a protobuf representation and then converting it back. - User
Is it a good thing to make game objects constructable through generative constructors without required protobuf object to be passed in immediately? I use imbueData(/protobuf object/) function to parse deserialized data into already created game objects
- Assistant
Using generative constructors to create game objects without requiring a protobuf object to be passed in immediately can have both advantages and disadvantages.
Advantages:
-
Flexibility: Generative constructors allow you to create game objects without immediately requiring the protobuf object. This can be useful for cases where you need to create the object first and then populate it with data later from a deserialized protobuf object. It provides flexibility in how and when you parse and populate the data.
-
Separation of concerns: By separating the creation of game objects using generative constructors and the parsing of data using a separate function like , you can better compartmentalize and organize your code. This separation can improve readability and maintainability by keeping the responsibility of object creation separate from data parsing.
-
- User
In my game library, the game objects are created as they would appear if the player started new game, and then they can be imbued with deserialized data, which itself is guaranteed to be correct, as otherwise the errors will be caught well before parsing at the stage of deserialization into protobuf objects
- Assistant
In that case, if you have confidence that the deserialized data is guaranteed to be correct and validated before parsing into protobuf objects, the approach of using generative constructors and the
imbueData()function can be a reasonable choice.Advantages:
-
Separation of concerns: The separation of creating game objects and parsing data can help organize and modularize your code. It allows you to clearly distinguish between the creation of game objects and the process of applying external data to them.
-
Reusability: By creating game objects with generative constructors and later populating them with data using
imbueData(), you can reuse the same game object creation logic for different scenarios. This can be useful in situations where you need to create objects based on various data sources or at different stages of the game.
-