Skip to content

Commit

Permalink
- Called addSpatialEntity after the spatial server owns the entity ch…
Browse files Browse the repository at this point in the history
…annel;
  • Loading branch information
indiest committed May 27, 2024
1 parent 483a8d2 commit 08acc5f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
7 changes: 7 additions & 0 deletions pkg/channeld/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ var Event_AuthComplete = &Event[AuthEventData]{}

var Event_FsmDisallowed = &Event[*Connection]{}

type EntityChannelSpatiallyOwnedEventData struct {
EntityChannel *Channel
SpatialChanel *Channel
}

var Event_EntityChannelSpatiallyOwned = &Event[EntityChannelSpatiallyOwnedEventData]{}

type EventData interface {
}

Expand Down
10 changes: 3 additions & 7 deletions pkg/channeld/message_spatial.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,9 @@ func handleCreateEntityChannel(ctx MessageContext) {
newChannel.Logger().Info("set the entity channel owner to the spatial channel's",
zap.Uint32("spatialChId", uint32(spatialChId)))

/* Sub-and-send happens at the end of this function
// Subscribe the owner to the entity channel
sub, shouldSend := ownerConn.SubscribeToChannel(newChannel, msg.SubOptions)
if shouldSend {
ownerConn.sendSubscribed(MessageContext{}, newChannel, ownerConn, 0, &sub.options)
}
*/
Event_EntityChannelSpatiallyOwned.Broadcast(EntityChannelSpatiallyOwnedEventData{newChannel, spatialCh})

// Sub-and-send happens at the end of this function

// Set the messge context so that the CreateChannelResultMessage will be sent to the owner
// instead of the message sender (the master server).
Expand Down
16 changes: 16 additions & 0 deletions pkg/unreal/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
func InitMessageHandlers() {
channeld.RegisterMessageHandler(uint32(unrealpb.MessageType_SPAWN), &channeldpb.ServerForwardMessage{}, handleUnrealSpawnObject)
channeld.RegisterMessageHandler(uint32(unrealpb.MessageType_DESTROY), &channeldpb.ServerForwardMessage{}, handleUnrealDestroyObject)

channeld.Event_EntityChannelSpatiallyOwned.Listen(handleEntityChannelSpatiallyOwned)
}

// Executed in the spatial channels or the GLOBAL channel (no-spatial scenario)
Expand Down Expand Up @@ -116,6 +118,8 @@ type UnrealObjectEntityData interface {
SetObjRef(objRef *unrealpb.UnrealObjectRef)
}

// Add the SpatialEntityState to the spatial channel data. If an entity doesn't exist in the spatial channel data,
// handover will not work properly.
func addSpatialEntity(ch *channeld.Channel, objRef *unrealpb.UnrealObjectRef) {
if ch.Type() != channeldpb.ChannelType_SPATIAL {
return
Expand Down Expand Up @@ -183,3 +187,15 @@ func handleUnrealDestroyObject(ctx channeld.MessageContext) {
channeld.RemoveChannel(entityCh)
}
}

func handleEntityChannelSpatiallyOwned(data channeld.EntityChannelSpatiallyOwnedEventData) {
dataMsgWithObjRef, ok := data.EntityChannel.GetDataMessage().(unrealpb.EntityChannelDataWithObjRef)
if !ok {
data.EntityChannel.Logger().Error("spatial-owned entity channel data doesn't implement EntityChannelDataWithObjRef")
return
}

data.SpatialChanel.Execute(func(ch *channeld.Channel) {
addSpatialEntity(ch, dataMsgWithObjRef.GetObjRef())
})
}

0 comments on commit 08acc5f

Please sign in to comment.