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

时间:2023-01-05 21:18:02

I'm developing a multiplayer api for Unity Engine. I have Room and RoomManager struct. When I want create room, I set room name, id (all of variable), after that I append this new room to allRoom array in RoomManager struct. After that when I modified to room variable, I can't change any variable. I dont know why ?

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

Here is my structs and methods :

这是我的结构和方法:

RoomManager struct

type RoomManager struct {
   allRooms    []Room
   roomCounter int
}

Room struct

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

Room Create Method

房间创建方法

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
}

Add User To Room

将用户添加到房间

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)
}

1 个解决方案

#1


3  

Your issue is that your structs have non-pointer slices. In all of your structs define your slices as slices of pointers like so

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

Rooms []*Room

You also need to define your functions to take pointer values, like so

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

func(room *Room) {}

To elaborate. Go is pass-by-value. Anytime you pull something out of one of your original slices and pass it to one of your functions, it gets a copy. Using pointers modifies the actual value in the slice.

详细说明。 Go是按值传递的。每当你从原始切片中取出一些东西并将其传递给你的一个函数时,它就会得到一份副本。使用指针修改切片中的实际值。

See this example. https://play.golang.org/p/ZThHrP0pds

看这个例子。 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
}

#1


3  

Your issue is that your structs have non-pointer slices. In all of your structs define your slices as slices of pointers like so

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

Rooms []*Room

You also need to define your functions to take pointer values, like so

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

func(room *Room) {}

To elaborate. Go is pass-by-value. Anytime you pull something out of one of your original slices and pass it to one of your functions, it gets a copy. Using pointers modifies the actual value in the slice.

详细说明。 Go是按值传递的。每当你从原始切片中取出一些东西并将其传递给你的一个函数时,它就会得到一份副本。使用指针修改切片中的实际值。

See this example. https://play.golang.org/p/ZThHrP0pds

看这个例子。 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
}