Skip to content

Commit 75d343f

Browse files
committed
fix registering on_add() callbacks. colyseus/colyseus#915
1 parent e36ce02 commit 75d343f

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

colyseus/serializer/schema/callbacks.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,15 @@ function Callbacks:add_callback_or_wait_collection_available(instance, field_nam
208208
local remove_handler = function() end
209209
local remove_callback = function() remove_handler() end
210210
if instance[field_name] == nil then
211-
remove_handler = _self:listen(instance, field_name, function(collection, _)
212-
remove_handler = _self:add_callback(collection.__refid, operation, callback)
211+
local remove_property_callback
212+
remove_property_callback = _self:listen(instance, field_name, function(collection, _)
213+
if collection ~= nil then
214+
-- Remove the property listener now that collection is available
215+
remove_property_callback()
216+
remove_handler = _self:add_callback(collection.__refid, operation, callback)
217+
end
213218
end)
219+
remove_handler = remove_property_callback
214220
return remove_callback
215221
else
216222
-- immediately trigger callback for each item in the collection

example/main.script

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ local function join_my_room()
3838
end)
3939

4040
callbacks:listen(player, "x", function(value, previous_value)
41-
print("[MyRoom] player.x changed: " .. tostring(value) .. " => " .. tostring(previous_value))
41+
print("[MyRoom] player.x changed: " .. tostring(previous_value) .. " => " .. tostring(value))
4242
end)
4343

4444
callbacks:listen(player, "y", function(value, previous_value)
45-
print("[MyRoom] player.y changed: " .. tostring(value) .. " => " .. tostring(previous_value))
45+
print("[MyRoom] player.y changed: " .. tostring(previous_value) .. " => " .. tostring(value))
4646
end)
4747

4848
callbacks:listen(player, "disconnected", function(value, previous_value)

release.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"version": "0.17.2"
2+
"version": "0.17.3"
33
}

0 commit comments

Comments
 (0)