Overview
The Generic Inventory System (GIS) offers a flexible mechanism called the Data Mixin System, enabling dynamic runtime data attachment (via FInstancedStruct) to any object (e.g., UObject) with support for serialization (with limitations) and network replication. This design is ideal for extending dynamic data to target objects without modifying their original structure.
For C++ User only
This article is intended only for C++ users who wish to develop other game systems using this system; Blueprint users need not read this article.
Use Cases
The Data Mixin System is suited for scenarios requiring dynamic state management. For example, in a Quest system using different DataAsset objects to define each Task (e.g., objectives, descriptions, rewards), the system can be integrated to allow each task type to manage its internal state with custom data structures.
Example
For a task requiring “killing 5 bandits and 3 wolves,” you can define a struct TaskState_KillEnemy with fields BanditKilled and WolfKilled to track the task’s progress.
Data Mixin Container
GIS provides data mixin functionality through GIS_MixinContainer, which maintains an Object->InstancedStruct key-value pair array. Implemented with FastArraySerializer, it supports efficient network replication and simplified data change notification callbacks.
Steps to Use the Data Mixin Container
Using ItemInstance as an example:
1.Add a GIS_MixinContainer field to your class.
2.In the constructor, set the container’s OwningObject to the current object (for ActorComponent, set it in InitializeComponent).
3.Ensure the field supports network replication, with synchronization conditions customized to project needs.
4.Implement the IGIS_MixinOwnerInterface interface in your class to respond to data changes via its functions.
5.The target object (e.g., Item_Fragment) must implement the IGIS_MixinTargetInterface interface to define runtime data types, default values, and serialization requirements.
API
The GIS_MixinContainer provides APIs for adding, removing, saving, and loading data. Refer to the project source code for detailed usage.
Usage Limitations
While the Data Mixin Container supports attaching runtime state to any object, for save support, note:
- The target object (TargetObject) must be loaded from a Package (e.g., a DataAsset).
- Custom structs must support the SaveGame property, and their field types must be serializable by Unreal Engine.
示例
An example of custom runtime state (notes the tooltip):
Validation and recovery
Keep static data in a Fragment and mutable data in its compatible state struct. Only state meeting persistence requirements should be expected to restore. Test authority-side changes, client replication notifications, and a save/load round trip separately; validate old records when changing custom factories or state structures.