arrays - 使用 roblox 上的数据存储保存游戏数据而不是玩家数据
问题描述
这是使用 lua 和 roblox 工作室的一般问题。我在我的游戏中需要保存一些信息,以便与加入的玩家分享。特别是,我正在编写一个平台游戏,其中玩家跳上平台,当他们跳上时,平台会变成他们团队的颜色。我需要为所有玩家更新平台颜色,但如果玩家加入游戏中期,已经“征服”的平台需要已经是该颜色。为此,我使用 getAsync 和 setAsync。就我所见,人们大多使用数据存储来保存游戏之间的数据,但就我而言,我希望在游戏期间检索保存的数据。游戏结束后,数据将设置为零。我假设数据存储是要走的路(...?)。我遇到的问题是,据我所知,roblox 中的数据存储只允许为每个键保存一个值,并且它以播放器作为键来保存它。但我需要三个键值。它们是:被征服的平台的行、列和颜色。我也不一定需要知道玩家的 id。然后,我希望每个玩家在加入或更新时都能访问此数据存储。理想情况下,我想要这样的东西:
设置:
local dataStoreService = game:GetService("DataStoreService")
local PlatformsScored = dataStoreService:GetDataStore('PlatformsScored')
玩家得分时的函数内部:
PlatformsScored:SetAsync({col, row}, platformColor)
然后,检索并解雇所有客户端和/或当新玩家加入时:
local pScoredNow = PlatformsScored:GettAsync({col, row}, platformColor)
for k, platform in (myPlatforms) do
if platform.col == col and platform.row = row do
platform.BrickColor = BrickColor.new(platformColor)
end
这个想法是这个 for 循环遍历所有平台并设置颜色。我一直在网上寻找,我不确定这是否可能。您可以将表保存到 Roblox Studio 中的数据存储区吗?数据存储可以是“非个人的”,不一定与作为键的玩家 ID 相关联吗?谢谢。
解决方案
你问了很多问题,所以我会尽力解决所有问题
您可以将表保存到 Roblox Studio 中的数据存储区吗?...我需要三个键值。它们是:被征服的平台的行、列和颜色。
无法保存表格,但如果您的数据可以序列化,则可以使用 HttpService:JSONEncode() 将表格转换为字符串,然后使用HttpService :JSONDecode()将表格转换回表格。
local HttpService = game:GetService("HttpService")
local dataString = HttpService:JSONEncode({
row = 5,
column = 10,
color = "Red",
})
print(dataString)
local reconstructedData = HttpService:JSONDecode(dataString)
print(reconstructedData.row, reconstructedData.column, reconstructedData.color)
数据存储是否可以是“非个人的”,不一定与作为键的玩家 ID 相关联
当然,您可以将信息存储在您想要的任何键下。但是您应该非常小心地选择密钥,因为每个游戏服务器都会写入这些密钥。这就是为什么总是推荐 playerID 的原因,因为它是一个保证唯一的密钥,玩家不能同时在两个服务器中,所以不会有两个服务器意外同时写入同一个密钥的风险。
如果您有多个游戏服务器写入同一个密钥,则两台服务器很有可能会覆盖彼此的数据并且某人的东西会丢失。
我希望在游戏过程中检索在游戏过程中保存的数据......我希望每个玩家在加入或更新时访问此数据存储。
这不是 DataStore 的好用例。数据存储应该用于持久存储有关玩家或世界的数据,这些数据必须跨越多个服务器。例如,考虑一个全球排行榜,或玩家的游戏进度,或多人挖的坑有多深(假设您希望该坑在下次游戏启动时继续存在)。
如果您尝试访问游戏状态信息并且您仍处于活动游戏中,您可以让新玩家根据当前状态构建他们的游戏板。无需通过 DataStore 进行通信。
处理活动游戏状态信息的一个好方法是在服务器脚本中创建一个“GameManager”对象,并使其对游戏中发生的更改具有权威性。球员得分?告诉 GameManager,它将更新记分牌。有玩家加入?询问 GameManager 平台/游戏板的当前状态是什么。
您可以使用简单的 lua 表、RemoteEvents 和 RemoteFunctions 来完成所有这些工作。我喜欢使用 ModuleScript 来制作我的 GameManager 类。我粗略的架构轮廓会是这样的......
local PlayerService = game:GetService("Players")
-- keep a list of RemoteFunctions and RemoteEvents that are fired by players when they do something
local GetState = game.ReplicatedStorage.Functions.GetState
local PlayerAction = game.ReplicatedStorage.Events.PlayerAction
-- keep a list of RemoteEvents that are fired by the GameManager when something should be communicated to the players
local StartGame = game.ReplicatedStorage.Events.StartGame
local UpdateBoard = game.ReplicatedStorage.Events.UpdateBoard
local EndGame = game.ReplicatedStorage.Events.EndGame
-- make an authority over the game state
local GameManager = {}
GameManager.__index = GameManager
function GameManager.new()
local gm = {
-- keep a list of players
teams = { "red" = {}, "blue" = {} },
-- keep a list of scores per team
scores = { "red" = 0, "blue" = 0 },
-- keep track of the board colors
platforms = {},
-- keep track of the state of the game
currentGameState = "WaitingForPlayers", --InGame, PostGame
-- as good housecleaning, hold onto connection tokens
__tokens = {},
}
setmetatable(gm, GameManager)
-- if anyone ever asks what the current state of the game is, let them know!
GetState.OnServerInvoke = function()
return gm.scores, gm.platforms, gm.currentGameState, gm.teams
end
return gm
end
function GameManager:waitForPlayers()
-- wait for players to join to start the game
while #PlayerService:GetPlayers() < 1 do
wait(5)
end
wait(5)
-- tell everyone the round is starting!
self:startNewGame()
end
function GameManager:startNewGame()
-- start listening to game events
local playerActionToken = PlayerAction:OnServerEvent(function(player, ...)
-- the player has done something, update the game state!
local args = { ... }
print("PlayerAction : ", player.Name, unpack(args))
-- if a platform was taken, alert all players so they can update their stuff
UpdateBoard:FireAllClients(self.platforms)
end)
table.insert(self.__tokens, playerActionToken)
-- assign players to teams...
-- tell all the players that the game has begun
StartGame:FireAllClients()
-- rather than code a game loop, just kill the game for now
spawn(function()
wait(30)
-- tell everyone the game is over
self:endGame()
end)
end
function GameManager:endGame()
self.currentGameState = "PostGame"
-- communicate to all players that the game is over, and let them know the score
EndGame:FireAllClients(self.scores)
-- stop accepting actions from the game and clean up connections
for _, token in ipairs(self.__tokens) do
token:Disconnect()
end
-- start the game over again!
spawn(function()
wait(30)
self:waitForPlayers()
end)
end
return GameManager
然后在服务器脚本中,只需创建 GameManager ...
local GameManager = require(script.Parent.GameManager) -- or wherever you've put it
local gm = GameManager.new()
gm:waitForPlayers()
然后在 LocalScript 中,让玩家在加入游戏时请求游戏状态...
-- connect to all the game signals that the server might send
game.ReplicatedStorage.Events.StartGame:Connect(function(args)
-- construct the local game board
print("Game has started!")
end)
game.ReplicatedStorage.Events.UpdateBoard:Connect(function(newBoardState)
print("Board state has changed!")
end)
game.ReplicatedStorage.Events.EndGame:Connect(function(scores)
print("Game has ended!")
end)
-- request the current game state with a RemoteFunction
local GetState = game.ReplicatedStorage.Functions.GetState
local scores, platforms, currentGameState, teams= GetState:InvokeServer()
-- parse the current game state and make the board...
for k, v in pairs(scores) do
print(k, v)
end
for k, v in pairs(platforms) do
print(k, v)
end
print(currentGameState)
for k, v in pairs(teams) do
print(k, v)
end
推荐阅读
- apache-spark-sql - 如何在 SparkSQL 中注册临时表
- python - 如何使用 python 与打印框交互
- recursion - Ocaml 中的列表递归
- ios - Xcode 9.4 Build(Debug, Release) OK,但存档失败,“找不到选项的目录..”
- python - np.shuffle 比 np.random.choice 慢得多
- dns - 客户的 IT 拒绝设置 SPF
- spring - Cas and Spring example: I don't understand "setUserDetailsService"
- python - How do I denormalize the sklearn diabetes dataset?
- go - Serving a map of strings without 3rd party libraries using http.filesystem in Go (Golang)
- facebook - 如何为 Facebook 访问令牌和用户帐户建模