Killikario
Killikario4mo ago

Syncing GameObject enabled

How would i go about syncing the enabled state of a gameobject?
3 Replies
kapa.ai
kapa.ai4mo ago
Hi @Killikario. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To synchronize the enabled state of a GameObject in Unity using Normcore, you would need to create a custom RealtimeComponent. This component would be responsible for synchronizing the enabled state of the GameObject across all clients. Here's a step-by-step guide on how you can do this: 1. Create a RealtimeModel subclass: This model will hold the data you want to synchronize. In this case, it would be a boolean representing the enabled state of the GameObject. You can create a new script, say EnabledSyncModel.cs, and define a boolean property in it. Remember to add the [RealtimeModel] attribute to the class and the [RealtimeProperty] attribute to the property.
using Normal.Realtime.Serialization;

[RealtimeModel]
public partial class EnabledSyncModel {
[RealtimeProperty(1, true, true)]
private bool _enabled;
}

using Normal.Realtime.Serialization;

[RealtimeModel]
public partial class EnabledSyncModel {
[RealtimeProperty(1, true, true)]
private bool _enabled;
}

After creating the model, you need to compile it in Unity. Normcore will automatically generate the necessary code to keep this model in sync with the datastore. 2. Create a RealtimeComponent subclass: This component will be responsible for keeping the GameObject's enabled state in sync with the model. You can create a new script, say EnabledSync.cs, and override the OnRealtimeModelReplaced() method. In this method, you can update the GameObject's enabled state based on the model's value and also register for changes in the model's value.
using Normal.Realtime;

public class EnabledSync : RealtimeComponent<EnabledSyncModel> {
private void Awake() {
// Get a reference to the GameObject
_gameObject = GetComponent<GameObject>();
}

protected override void OnRealtimeModelReplaced(EnabledSyncModel previousModel, EnabledSyncModel currentModel) {
if (previousModel != null) {
// Unregister from events
}

if (currentModel != null) {
// Update the GameObject's enabled state based on the model's value
_gameObject.SetActive(currentModel.enabled);

// Register for changes in the model's value
currentModel.enabledDidChange += EnabledDidChange;
}
}

private void EnabledDidChange(EnabledSyncModel model, bool value) {
// Update the GameObject's enabled state when the model's value changes
_gameObject.SetActive(value);
}
}

using Normal.Realtime;

public class EnabledSync : RealtimeComponent<EnabledSyncModel> {
private void Awake() {
// Get a reference to the GameObject
_gameObject = GetComponent<GameObject>();
}

protected override void OnRealtimeModelReplaced(EnabledSyncModel previousModel, EnabledSyncModel currentModel) {
if (previousModel != null) {
// Unregister from events
}

if (currentModel != null) {
// Update the GameObject's enabled state based on the model's value
_gameObject.SetActive(currentModel.enabled);

// Register for changes in the model's value
currentModel.enabledDidChange += EnabledDidChange;
}
}

private void EnabledDidChange(EnabledSyncModel model, bool value) {
// Update the GameObject's enabled state when the model's value changes
_gameObject.SetActive(value);
}
}

3. Add the RealtimeComponent to the GameObject: Finally, you can add the EnabledSync component to the GameObject whose enabled state you want to synchronize. Normcore will automatically add a RealtimeView component and add the EnabledSync component to the list of observed components. Remember that only primitives (int, float, Vector3, string, etc.) can be serialized in Normcore, so you cannot directly sync a GameObject or any other complex type (source (https://normcore.io/documentation/room/common-questions#when-i-try-to-sync-a-specific-object-to-the-datastore-i-get-an-error-that-says-it-cannot-be-serialized-whats-the-issue)). For more information on creating custom RealtimeComponents and RealtimeModels, you can refer to the Normcore documentation (https://normcore.io/documentation/realtime/synchronizing-custom-data). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@580413914228195328> kapa.ai is still learning and improving, please let us know how it did by reacting below
Killikario
KillikarioOP4mo ago
alright