What is the proper way to send an RPC to every entity in a query?
My code example is the iteration over all entities that had a connection state change occurrence, though my question need not apply to that in particular.
//================================================================
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var ecb = new EntityCommandBuffer(Allocator.Temp);
foreach (var (connectionState, connectedEntity) in SystemAPI.Query<ConnectionState>().WithEntityAccess()) {
bool isPlayer = SystemAPI.HasComponent<PlayerComponent>(connectedEntity);
bool isAdmin = SystemAPI.HasComponent<AdminComponent>(connectedEntity);
bool isGameMaster = SystemAPI.HasComponent<GameMasterComponent>(connectedEntity);
PlayerInfoComponent playerInfo = SystemAPI.GetComponent<PlayerInfoComponent>(connectedEntity);
var sendLocalizedTextMessageEntity = ecb.CreateEntity();
LocalizableTextMessageRPC message = get_localizable_text_message(playerInfo, connectionState);
ecb.AddComponent(sendLocalizedTextMessageEntity, message);
ecb.AddComponent(sendLocalizedTextMessageEntity, new SendRpcCommandRequest {
TargetConnection = m_AdminQuery
});
if (isPlayer || isGameMaster) {
ecb.AddComponent(sendLocalizedTextMessageEntity, new SendRpcCommandRequest {
TargetConnection = m_GameMasterQuery
});
}
if (connectionState.CurrentState == ConnectionState.State.Connected) {
Debug.Log($"NetworkId: {connectionState.NetworkId} {connectionState.CurrentState.ToFixedString()}");
}
if (connectionState.CurrentState == ConnectionState.State.Disconnected) {
Debug.Log($"NetworkId: {connectionState.NetworkId} {connectionState.CurrentState.ToFixedString()} Reason: {connectionState.DisconnectReason.ToFixedString()}");
//clean up
ecb.RemoveComponent<ConnectionState>(connectedEntity);
if (isAdmin) {
ecb.RemoveComponent<AdminComponent>(connectedEntity);
}
if (isGameMaster) {
ecb.RemoveComponent<GameMasterComponent>(connectedEntity);
}
if (isPlayer) {
ecb.RemoveComponent<PlayerComponent>(connectedEntity);
}
ecb.RemoveComponent<PlayerInfoComponent>(connectedEntity);
}
}
ecb.Playback(state.EntityManager);
}
Obviously, assigning a query to TargetConnection isn’t going to work. Normally I’d send to a single Entity in that spot. Is there a cleaner (more code compact way, or method I didn’t notice) way to specify a query’s worth of entities as the target connection? Or will I have to iterate over every entity in the query and target each one explicitly? (The m_AdminQuery and m_GameMasterQuety are all connected entities that have the Admin and GameMasterComponent tags respectively. This will presumably result in sending the rpc to itself also, preferably I’d not send the message to itself, but I don’t really care if it does.)