Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If an object is sitting in your Scene ahead of time, you won’t be able to do any Property/Synchronization work with it until you are connected to a Space. A call to AwaitAnySpaceConnection gives you the Space Connection needed to begin the actual work.

using CavrnusSdk.API; using UnityEngine; private CavrnusSpaceConnection spaceConn; private void Start() {
Code Block
languagecpp
void APropertySyncActorClass::BeginPlay()
{
  Super::BeginPlay();
  
  // Member is delegate FCavrnusSpaceConnected SpaceConnected;
  SpaceConnected.BindUFunction(this, GET_FUNCTION_NAME_CHECKED(APropertySyncActorClass, OnSpaceConnected));
  
  CavrnusFunctionLibrary.UCavrnusFunctionLibrary::AwaitAnySpaceConnection(spaceConn => OnConnectedToSpace(spaceConn)OnSpaceConnected);
}

private
void OnConnectedToSpace(CavrnusSpaceConnection spaceConnAPropertySyncActorClass::OnSpaceConnected(FCavrnusSpaceConnection SpaceConnection)
{
  UE_LOG(LogTemp,  this.spaceConn = spaceConnLog, TEXT("Connected to Space ID %d."), SpaceConnection.SpaceConnectionId);
}

Initialize Default Value

...

Without this step, the Bind() call will provide a “null” value (such as an empty-string or a [0,0,0,0] vector), until a proper value is posted to the server.

Code Block
languagecpp
private void OnConnectedToSpace(CavrnusSpaceConnection spaceConn)
{
    ...
    
    spaceConn.DefineColorPropertyDefaultValue(APropertySyncActorClass::OnSpaceConnected(FCavrnusSpaceConnection SpaceConnection)
{
  FLinearColor CurrentValue = FLinearColor::White;
  MaterialInstance->GetVectorParameterValue("Color", CurrentValue);

  // ContainerName is usually assigned a unique value at runtime within the Cavrnus journal by using a CavrnusPropertiesContainer component
  // PropertyName should be something unique within this object. Could just be "Color", or something more specific if there are multiple synched color properties on this actor. (e.g. "ShirtBaseColor" and "ShirtAccentColor")
  UCavrnusFunctionLibrary::DefineColorPropertyDefaultValue(SpaceConnection, ContainerName, PropertyName, GetComponent<MeshRenderer>().material.colorCurrentValue);
}

Bind Current Journal Value & Update Application on Changes

Once we have set up our default value, we can now Bind() its changes. Whenever someone Posts a change to the server and we receive it, the Bind() call will trigger, allowing us to update the local Application to reflect the server’s value.

private
Code Block
language
cpp
void OnConnectedToSpace(CavrnusSpaceConnection spaceConnAPropertySyncActorClass::OnSpaceConnected(FCavrnusSpaceConnection SpaceConnection)
{
    ...
  
  // Member is delegate FColorPropertyUpdated SpaceConnected;
 spaceConn OnMaterialColorPropertyUpdated.BindColorPropertyValueBindUFunction(ContainerNamethis, PropertyName, c => SetMyMaterialColor(c)GET_FUNCTION_NAME_CHECKED(APropertySyncActorClass, SetMyMaterialColor));
  MaterialColorBinding = UCavrnusFunctionLibrary::BindColorPropertyValue(SpaceConnection, ContainerName, PropertyName, MaterialColorPropertyUpdated);
}

public
void APropertySyncActorClass::SetMyMaterialColor(LinearColor Color color)
{
    GetComponent<MeshRenderer>().material.color = colorMaterialInstance->SetVectorParameterValue("Color", Color);
}

If you were building an application that was “readonly” (say, a Digital Twin viewer where the data was being provided to the Journal elsewhere) then this is all you would need.

...