> For the complete documentation index, see [llms.txt](https://rockon-games.gitbook.io/metaforge-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rockon-games.gitbook.io/metaforge-documentation/readme/references-advanced.md).

# 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:

```cpp
#pragma once

#include "CoreMinimal.h"
#include "UDataModel.h"
#include "MyOtherObjectManager.h"
#include "UMyReferencedModel.generated.h"

UCLASS()
class UMyReferencedModel : public UObject, public IDataModel
{
    GENERATED_BODY()

public:
    UPROPERTY(EditAnywhere, meta = (Component="SReferenceProperty", LoadFunction="GetReferences"), Category = "Stat")
    FName* MyOtherObjectReference;

    UFUNCTION()
    TArray<MyOtherObject*> GetReferences() const;
    
    UFUNCTION()
    MyOtherObject* GetMyRefProperty() {
        return MyOtherObejctManager::GetInstance()->FindByName(MyOtherObjectReference);
    }
};
```

In this example:

* **`MyOtherObjectReference`** is the ID of the `MyOtherObject`
* **`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>:

```cpp
void UMyReferencedModel::LoadReferenceObject()
{
    if (MyOtherObjectReference != NAME_None)
    {
        MyOtherObjectReferenceObj = MyOtherObjectManager::GetInstance()->FindById(MyOtherObjectReference);
    }
}
```

In addition, the manager class (`UMyReferencedModelManager`) can override the `OnLoaded()` method to automatically resolve references after the object is loaded:

```cpp
void UMyReferencedModelManager::OnLoaded()
{
    MyOtherObjectManager* refManager = MyOtherObjectManager::GetInstance();
    for (UMyReferencedModel* LoadedModel : GetAll())
    {
        if (LoadedModel)
        {
            LoadedModel->MyOtherObjectReference = refManager->FindByName(LoadedModel->MyOtherObjectReferenceName);
        }
    }
} 
```

## 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rockon-games.gitbook.io/metaforge-documentation/readme/references-advanced.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
