References (Advanced)
Overview
References in MetaForge are always stored by their IDs. However, at runtime (either in game mode or editor mode), it is trivial to get actual object references from these IDs. This approach makes it easier to directly interact with referenced objects without needing to look them up manually by their IDs. By using this system, developers can build more tightly integrated models and simplify interactions between different model objects.
Defining Reference Links
To define reference links, you can modify your model object to store an ID that can dynamically link to the referenced model object at runtime. This involves using metadata to configure how references are established, ensuring they can be resolved easily.
Example Code
Here is an example of defining a reference link in a model object:
In this example:
MyOtherObjectReference
is the ID of theMyOtherObject
MyOtherObjectManager
is used to retrieve the referenced object.LoadFunction
is used to populate an array of possible references that can be selected in the editor.
Loading the Reference Objects Once
To load the reference objects at runtime, you can use the stored ID (MyOtherObjectReference
) to resolve and obtain the actual object reference. This can be done by using a manager class or other lookup methods that map the stored IDs to their corresponding object instances. For example, you could use a function in your manager class that searches for the object by its ID and returns a pointer to the instance.
Here is an example where we have stored the reference in a MyOtherObject* or a TSharedPtr<MyOTherObject>:
In addition, the manager class (UMyReferencedModelManager
) can override the OnLoaded()
method to automatically resolve references after the object is loaded:
Summary
Using reference links in MetaForge allows for flexible relationships between model objects. By storing IDs and dynamically linking to the referenced objects at runtime, you can maintain interconnected data models while ensuring a lightweight and efficient implementation. Careful consideration of garbage collection and Blueprint integration is essential for managing these links effectively.
Last updated