Fix issue with things from different participants

This commit is contained in:
Pascal Serrarens 2025-06-06 11:52:35 +02:00
parent ac11382479
commit 80c4983b34
4 changed files with 30 additions and 17 deletions

View File

@ -48,7 +48,11 @@ namespace RoboidControl.Unity {
if (coreThing.component == null) {
Thing[] things = FindObjectsByType<Thing>(FindObjectsSortMode.None);
// Debug.Log(things.Length);
Thing thing = things.FirstOrDefault(t => t.core != null && t.core.id == coreThing.id);
Thing thing = things.FirstOrDefault(t =>
t.core != null &&
t.core.owner.networkId == coreThing.owner.networkId &&
t.core.id == coreThing.id
);
if (thing == null)
thing = Thing.Create(coreThing);
coreThing.component = thing;

View File

@ -1,16 +1,14 @@
#if UNITY_5_3_OR_NEWER
using System;
using System.Collections.Generic;
using UnityEngine;
namespace RoboidControl.Unity {
public class SiteServer : Participant {
//public RoboidControl.SiteServer site;
public RoboidControl.SiteServer site => this.coreParticipant as RoboidControl.SiteServer;
//public Queue<RoboidControl.Thing> thingQueue = new();
public string modelURL;
private RoboidControl.Thing model;
protected virtual void Awake() {
#if RC_DEBUG
@ -18,6 +16,15 @@ namespace RoboidControl.Unity {
#endif
this.coreParticipant = new RoboidControl.SiteServer(port);
#if GLTF
if (!string.IsNullOrEmpty(modelURL)) {
model = new() {
name = "Model",
modelUrl = this.modelURL
};
}
#endif
}
void OnApplicationQuit() {
@ -33,8 +40,6 @@ namespace RoboidControl.Unity {
HandleUpdateEvent(e);
site.Update();
// while (thingQueue.TryDequeue(out RoboidControl.Thing thing))
// thing.CreateComponent();
}
private void HandleUpdateEvent(RoboidControl.Participant.UpdateEvent e) {
@ -46,6 +51,10 @@ namespace RoboidControl.Unity {
participant.coreParticipant.component = participant;
participant.ipAddress = e.participant.ipAddress;
participant.port = e.participant.port;
foreach (RoboidControl.Thing thing in this.site.things)
participant.coreParticipant.SendThingInfo(thing);
break;
case ThingMsg.id:
HandleThingEvent(e);

View File

@ -191,6 +191,14 @@ namespace RoboidControl {
return true;
}
public void SendThingInfo(Thing thing) {
this.Send(new ThingMsg(thing.owner.networkId, thing));
this.Send(new NameMsg(thing.owner.networkId, thing));
this.Send(new ModelUrlMsg(thing.owner.networkId, thing));
this.Send(new PoseMsg(thing.owner.networkId, thing));
this.Send(new BinaryMsg(thing.owner.networkId, thing));
}
#endregion Send
#region Participant Registry

View File

@ -232,14 +232,6 @@ namespace RoboidControl {
#region Send
public void SendThingInfo(Participant owner, Thing thing) {
owner.Send(new ThingMsg(this.networkId, thing));
owner.Send(new NameMsg(this.networkId, thing));
owner.Send(new ModelUrlMsg(this.networkId, thing));
owner.Send(new PoseMsg(this.networkId, thing));
owner.Send(new BinaryMsg(this.networkId, thing));
}
public void PublishThingInfo(Thing thing) {
// Console.WriteLine("Publish thing info");
this.Publish(new ThingMsg(this.networkId, thing));
@ -386,7 +378,7 @@ namespace RoboidControl {
if (this.networkId != msg.networkId) {
this.networkId = msg.networkId;
foreach (Thing thing in this.things) //Thing.GetAllThings())
this.SendThingInfo(sender, thing);
sender.SendThingInfo(thing);
}
}