登录
首页 >  Golang >  Go问答

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

来源:Golang技术栈

时间:2023-04-11 17:35:35 324浏览 收藏

一分耕耘,一分收获!既然打开了这篇文章《附加到数组后我无法修改项目》,就坚持看下去吧!文中内容包含golang等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!

问题内容

我正在为 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 

正确答案

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

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
}

今天带大家了解了golang的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:Golang技术栈 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表