一尘不染

我无法在追加到数组后修改项目

go

我正在为Unity
Engine开发多人api。我有Room和RoomManager结构。当我要创建房间时,我设置房间名称id(所有变量),然后将这个新房间附加到RoomManager结构中的allRoom数组中。之后,当我修改为房间变量时,便无法更改任何变量。我不知道为什么?

这是我的结构和方法:

RoomManager结构

type RoomManager struct {
   allRooms    []Room
   roomCounter int
}

房间结构

type Room struct {
   tag               string
   name              string
   password          string
   id                int
   cappacity         int
   maxVariableCount  int
   userList          []User
   roomVariables     []RoomVariable
   extensionHandlers []ExtensionRequest
   InitializeMethod  roomInitializeFunc
}

房间创建方法

func (roomManager *RoomManager) CreateRoom(settings RoomSettings, arwServer *ARWServer) *Room {

   var newRoom Room
   newRoom.InitializeMethod = settings.InitializeMethod

   newRoom.name = settings.name
   newRoom.password = settings.password
   newRoom.tag = settings.tag
   newRoom.cappacity = settings.cappacity
   newRoom.maxVariableCount = settings.maxRoomVariableCount

   newRoom.id = roomManager.roomCounter
   roomManager.roomCounter++

   newRoom.userList = make([]User, 0, newRoom.cappacity)
   newRoom.roomVariables = make([]RoomVariable, 0, newRoom.maxVariableCount)

   if newRoom.InitializeMethod != nil {
     newRoom.InitializeMethod(arwServer, &newRoom)
   }

   roomManager.allRooms = append(roomManager.allRooms, newRoom)
   return &newRoom
}

将用户添加到房间

func (room *Room) AddUserToRoom(arwServer *ARWServer, u User) {
   room.userList = append(room.userList, u)

   var arwObj ARWObject

   arwObj.requestName = Join_Room
   arwObj.eventParams.PutString("RoomName", room.name)
   arwObj.eventParams.PutString("RoomTag", room.tag)
   arwObj.eventParams.PutInt("RoomId", room.id)
   arwObj.eventParams.PutInt("RoomCappacity", room.cappacity)

   usersData := ""
   for ii := 0; ii < len(room.userList); ii++ {
     if room.userList[ii].name != u.name {
       usersData += room.userList[ii].GetDataForOtherUser(u) + "''"
     }
   }

   usersData = strings.TrimRight(usersData, "''")

   arwObj.eventParams.PutString("Users", usersData)
   arwServer.SendRequestToUser(u, arwObj)

   var arwObjforTheOthers ARWObject
   arwObjforTheOthers.requestName = User_Enter_Room
   arwObjforTheOthers.eventParams.PutString("RoomName", room.name)
   arwObjforTheOthers.eventParams.PutString("userName", u.name)
   arwObjforTheOthers.eventParams.PutInt("userId", u.id)
   arwObjforTheOthers.eventParams.PutString("isMe", "false")

   room.SendRequestAllUserWithoutMe(*arwServer, arwObjforTheOthers, u)
   fmt.Println("User join Room - User Name : ", u.name+" Room : "+u.lastRoom.name)
}

阅读 251

收藏
2020-07-02

共1个答案

一尘不染

您的问题是您的结构具有非指针切片。在所有结构中,像这样将切片定义为指针切片

Rooms []*Room

您还需要定义函数以获取指针值,如下所示

func(room *Room) {}

详细说明。Go是按值传递。每当您从原始切片之一中拉出某些东西并将其传递给您的功能之一时,它都会得到一个副本。使用指针会修改切片中的实际值。

请参阅此示例。https://play.golang.org/p/ZThHrP0pds

package main

import (
    "fmt"
)

type Thing struct {
    Name string
}

func main() {
    things := []Thing{}
    thing := Thing{"thing1"}

    // add to slice
    // note that this is a function call
    things = append(things, thing)

    // attempt to change
    thing.Name = "thing2"
    fmt.Println(things[0].Name) // prints thing1
    fmt.Println(thing.Name)     // prints thing2

    fmt.Println("------")

    // try again
    thing3 := things[0]
    thing3.Name = "thing3"
    // fail again
    fmt.Println(things[0].Name) // prints thing1
    fmt.Println(thing3.Name)     // prints thing3

    fmt.Println("------")

    // do this instead
    betterThings := []*Thing{} // slice of pointers
    betterThing := &Thing{"thing2"} // betterThing is of type *Thing

    // add to slice
    betterThings = append(betterThings, betterThing)

    // successfully change
    betterThing.Name = "thing2"

    fmt.Println(betterThings[0].Name) // prints thing2
    fmt.Println(betterThing.Name)     // prints thing2
}
2020-07-02