GlobalDataStore||doc136725

September 20, 2022

GlobalDataStore

Only the server can access

This class must passGetglobaldatastore for DataService classMethod acquisition

Because the database needs to be operated, the operations of setting and obtaining data are performed asynchronously and will not take effect immediately. When the network conditions are poor, it may take longer to wait. The data in the editor environment will only exist locally, so it is not necessary to There are the above problems

Brief Introduction

Global data storage can be used to store some data irrelevant to players. For example, if we want to make a message board, clearance players can leave a message. At this time, we can use the global data storage service to store the message data in the database. If we do not use the data storage service, but only store the information in a variable of the code, these information will be lost as the game room is closed, The message boards seen in different game rooms are not the same.

Function

void SetDatastring key, variant value )
Set the value on the corresponding key
void RemoveDatastring key )
Remove the value on the corresponding key
void RequestDatastring key, function callback )
Get the value on the corresponding key
void UpdateDatastring key, function callback )
Update the value on the corresponding key

Code example

Making a message board with global data storage

-- Get the data storage under the "Message Board" space
local msgDataStore = Engine.DataService:GetGlobalDataStore("MessageBoard")

-- set some data
msgDataStore:SetData("PlayerA", "I'm the king!!!")
msgDataStore:SetData("PlayerB", 10)

-- Get the data of PlayerB
msgDataStore:RequestData("PlayerB", function(value)
     print("PlayerB's msg is : ", value)
end)

-- Update PlayerB's data
msgDataStore:UpdateData("PlayerB", function(value)
     value = value * 2
     return value
end)

-- Get PlayerB's data again
msgDataStore:RequestData("PlayerB", function(value)
     print("PlayerB's msg is : ", value)
end)