Advertisement
AlewAlow

client side replication part after edit

Apr 13th, 2024 (edited)
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2.  
  3. local Components = require(ReplicatedStorage.Shared.Components)
  4.  
  5. local remoteEvent = ReplicatedStorage:WaitForChild("MatterRemote")
  6.  
  7. local function SetupReplication(world, state)
  8.     local function debugPrint(...)
  9.         if state.debugEnabled then
  10.             print("Replication>", ...)
  11.         end
  12.     end
  13.    
  14.     remoteEvent.OnClientEvent:Connect(function(entities)
  15.         for entityId, componentMap in entities do
  16.             local entityId = tonumber(entityId)
  17.             local exists = world:contains(entityId)
  18.            
  19.             if exists and next(componentMap) == nil then
  20.                 world:despawn(entityId)
  21.                 debugPrint(string.format("Despawn %s", entityId))
  22.                 continue
  23.             end
  24.  
  25.             local componentsToInsert = {}
  26.             local componentsToRemove = {}
  27.  
  28.             local insertNames = {}
  29.             local removeNames = {}
  30.  
  31.             for name, container in componentMap do
  32.                 if container.data then
  33.                     table.insert(componentsToInsert, Components[name](container.data))
  34.                     table.insert(insertNames, name)
  35.                 else
  36.                     table.insert(componentsToRemove, Components[name])
  37.                     table.insert(removeNames, name)
  38.                 end
  39.             end
  40.  
  41.             if not exists then
  42.                 -- the "NonShared" is used for shared systems that run on both sides
  43.                 -- by filtering out by default entities that were created on the server and replicated to the client
  44.                 -- which leads to shared systems running for the entity on the same side they were created on
  45.                 -- an example of a shared system is a system of mine that handles checking collisions
  46.                 -- by doing the same calculations each frame and storing the results which are a table of ids
  47.                 -- in a component, which would have been hard to do because of how ids are not the same on
  48.                 -- both sides so entities created on the server would have replicated false ids compared to the client
  49.                 -- but thanks to this change they work well
  50.                 world:spawnAt(entityId, Components.NonShared(), unpack(componentsToInsert))
  51.                
  52.                 debugPrint(
  53.                     string.format("Spawn %s with %s", entityId, table.concat(insertNames, ","))
  54.                 )
  55.             else
  56.                 if #componentsToInsert > 0 then
  57.                     world:insert(entityId, unpack(componentsToInsert))
  58.                 end
  59.            
  60.                 if #componentsToRemove > 0 then
  61.                     world:remove(entityId, unpack(componentsToRemove))
  62.                 end
  63.  
  64.                 debugPrint(
  65.                     string.format(
  66.                         "Modify %s adding %s, removing %s",
  67.                         entityId,
  68.                         if #insertNames > 0 then table.concat(insertNames, ", ") else "nothing",
  69.                         if #removeNames > 0 then table.concat(removeNames, ", ") else "nothing"
  70.                     )
  71.                 )
  72.             end
  73.         end
  74.     end)
  75. end
  76.  
  77. return SetupReplication
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement