MetaForge Documentation
Fab Store PageDiscord
  • MetaForge
    • Getting Started
    • Plugin Configuration
    • Runtime Configuration
    • Project Configuration
    • Model Objects
    • Model Managers
    • Registering Models
    • Conditionals
    • Validators
    • References
    • References (Advanced)
    • Property Drawers
    • List Properties
    • IDE File Templates
    • Runtime Usage
  • Contact Us
Powered by GitBook
On this page
  • Overview
  • Defining Reference Links
  • Example Code
  • Loading the Reference Objects Once
  • Summary
  1. MetaForge

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:

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

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:

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.

PreviousReferencesNextProperty Drawers

Last updated 5 months ago