本文介绍了追加到数组后,我无法修改该项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以下是我的结构和方法:

RoomManager结构

  type RoomManager struct {
allRooms [] Room
roomCounter int
}

房间结构

 类型房间结构{
标记字符串
名称字符串
密码字符串
id int
cappacity int
maxVariableCount int
userList []用户
房间变量[] RoomVariable
ExtensionHandlers [] ExtensionRequest
InitializeMethod roomInitializeFunc
}

房间创建方法



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

var newRoom Room
newRoom.InitializeMethod = settings.InitializeMethod

newRoom.name = settings.name
newRoom.password = settings.p密码
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)

如果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)


解决方案

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

 房间[] *房间

您还需要定义函数来获取指针值,如

  func(room * Room){} 

详细说明。 Go是传递价值。无论何时,当您从某个原始切片中提取某些内容并将其传递给您的某个函数时,它将获得一份副本。使用指针修改切片中的实际值。



请参阅此示例。

 包主

导入(
fmt


类型结构{
名称字符串
}

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

//添加到切片
//注意这是一个函数调用
things = append(things,thing)

//尝试更改
thing.Name =thing2
fmt.Println(things [0] .Name)//打印thing1
fmt.Println(thing.Name)//打印thing2

fmt.Println(------)

//再试一次
thing3:=东西[0]
thing3.Name =thing3
//再次失败
fmt.Println(things [0] .Name)//打印thing1
fmt.Println(thing3.Name)//打印thing3

fmt.Println(------)

//代替
betterThings:= [] * Thing {} //片段指针
betterThing:=& Thing {thing2} // betterThing是类型* Thing

//添加到片段
betterThings = append(betterThings ,betterThing)

//成功更改
betterThing.Name =thing2

fmt.Println(betterThings [0] .Name)//打印thing2
fmt.Println(betterThing.Name)//打印thing2
}


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 ?

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)
}
解决方案

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.

See this example. 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
}

这篇关于追加到数组后,我无法修改该项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:30