diff --git a/lib/client/player.lua b/lib/client/player.lua index b28bd6c5..68c2a13c 100644 --- a/lib/client/player.lua +++ b/lib/client/player.lua @@ -2,15 +2,22 @@ ---@class OxPlayerClient : OxClass local OxPlayer = lib.class('OxPlayer') --- Support for `player.method` rather than self (:) syntax +-- Support both obj.method() and obj:method() calling conventions. +-- The closure captures self, so : syntax passes an extra self as first arg. function OxPlayer:__index(index) local value = OxPlayer[index] --[[@as any]] if type(value) == 'function' then - self[index] = value == OxPlayer.__call and function(...) - return value(self, index, ...) - end or function(...) - return value(self, ...) + self[index] = value == OxPlayer.__call and function(first, ...) + if first == self then + return value(self, index, ...) + end + return value(self, index, first, ...) + end or function(first, ...) + if first == self then + return value(self, ...) + end + return value(self, first, ...) end return self[index] diff --git a/lib/server/player.lua b/lib/server/player.lua index 44504de9..ef048406 100644 --- a/lib/server/player.lua +++ b/lib/server/player.lua @@ -6,10 +6,18 @@ function OxPlayer:__index(index) local value = OxPlayer[index] --[[@as any]] if type(value) == 'function' then - self[index] = value == OxPlayer.__call and function(...) - return value(self, index, ...) - end or function(...) - return value(self, ...) + -- Support both obj.method() and obj:method() calling conventions. + -- The closure captures self, so : syntax passes an extra self as first arg. + self[index] = value == OxPlayer.__call and function(first, ...) + if first == self then + return value(self, index, ...) + end + return value(self, index, first, ...) + end or function(first, ...) + if first == self then + return value(self, ...) + end + return value(self, first, ...) end return self[index] diff --git a/lib/server/vehicle.lua b/lib/server/vehicle.lua index 01b2ea33..d5e688c9 100644 --- a/lib/server/vehicle.lua +++ b/lib/server/vehicle.lua @@ -6,10 +6,18 @@ function OxVehicle:__index(index) local value = OxVehicle[index] --[[@as any]] if type(value) == 'function' then - self[index] = value == OxVehicle.__call and function(...) - return value(self, index, ...) - end or function(...) - return value(self, ...) + -- Support both obj.method() and obj:method() calling conventions. + -- The closure captures self, so : syntax passes an extra self as first arg. + self[index] = value == OxVehicle.__call and function(first, ...) + if first == self then + return value(self, index, ...) + end + return value(self, index, first, ...) + end or function(first, ...) + if first == self then + return value(self, ...) + end + return value(self, first, ...) end return self[index]