Overview | Videos | Documentation | Demos | Integrations | API | Support

Doc Home > Component Communication > Shared Property and Method

In some cases a component may want to get/set a value or execute a function on a separate component without directly knowing about that separate component. As an example, when a weapon is fired it sets a recoil amount so the recoil can be portrayed on the GUI by expanding or moving the crosshairs. Since the framework works with both player-controlled characters and AI-controlled characters, there isn't always guaranteed to be a GUI for every weapon fired. Furthermore, every game is different so a GUI that displays the recoil may not even be necessary. Instead of requiring the Weapon component to have knowledge that the GUI component exists, a SharedProperty can be used instead. SharedMethod is similar to SharedProperty except it works on methods instead of properties.

The first step to setup a SharedProperty is to first define the variable that holds the property value. From the recoil example it would be a simple float:

private float m_RecoilAmount = 0;

The property for this variable then needs to be have a special formatting in order for other components to know that this property exists. This special formatting means that the property needs to have "SharedProperty_" appended before the name of the property:

private float SharedProperty_RecoilAmount { get { return m_RecoilAmount; } set { m_ RecoilAmount = value; } }

After the "SharedProperty_" is the name of the variable that the property gets or sets. Do not include the "m_" as that will be ignored. SharedProperties will not work if this formatting is not applied. For example, the following will property name will NOT work:

SharedProperty_Recoil

This property name will not work because the property doesn't have the full variable name, RecoilAmount.

After the property has been created the component needs to register itself with the SharedManager so other components can find it. This should be done within the Awake method:

private void Awake()
{
    SharedManager.Register(this);
}

This is all that is required to have the property be recognized. In order for another component to find this property it first needs to have a variable which will be used to store the reference to the property:

private SharedProperty m_RecoilAmount = null;

Take note that this variable has the same name as the variable that the SharedProperty modifies. This is also necessary as it maps a particular variable to the SharedProperty. The component then needs to initialize the SharedProperty with the correct property reference. This should be done within the Start method:

private void Start()
{
    SharedManager.InitializeSharedFields(guiGameObject, this);
}

The first parameter is the object that the original SharedProperty has been added to. In this example the object is the GameObject that the GUI has been added to. This is all that is required in order to get the components to share properties without directly knowing about each other!

The RecoilAmount SharedProperty can then be used by calling Get and Set:

float amount = m_RecoilAmount.Get();
m_RecoilAmount.Set(amount);

SharedMethod works in a very similar way as SharedProperty does except it doesn't require as much setup because no values are being stored. The first step is to create the method:

public Vector3 SharedMethod_TargetLookPosition()
{
    // Implementation
}

Notice that this method has a very similar format as SharedProperty: it must start with "SharedMethod_" and the name of the method ("TargetLookPosition") is used to map the variable to the method. The component which contains this method still must register itself within Awake:

Public void Awake()
{
    SharedManager.Register(this);
}

In order for another component to find this method it needs to have a variable which will be used to store the reference to the method:

private SharedMethod TargetLookPosition = null;

Notice that the name of this variable matches the name of the method after the "SharedMethod_" prefix. This component then needs to initialize the SharedMethod with the correct reference:

private void Start()
{
    SharedManager.InitializeSharedFields(targetGameObject, this);
}

The same parameters are used with SharedMethod as SharedProperty. The first parameter is the object that the SharedMethod method exists on. Once this is setup the component is able to execute methods without having direct knowledge on what object it is executing these methods on! As an example, this SharedMethod can then be used by calling invoke:

TargetLookPosition.Invoke();


<- Events
Scheduler ->