Skip to content

Commit

Permalink
Merge pull request #351 from wrefgtzweve/Remove-__index-wrapping
Browse files Browse the repository at this point in the history
Remove  __index wrapping
  • Loading branch information
TwistedTail authored Jul 11, 2023
2 parents 4d083ab + 32af7b0 commit ba4b8f6
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 50 deletions.
34 changes: 0 additions & 34 deletions lua/acf/core/utilities/util_sh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -731,37 +731,3 @@ do -- File creation
return util.JSONToTable(file.Read(FullPath, "DATA"))
end
end

do -- Entity metatable method overriding
-- One of the limitation of the entity __index metamethod is that you cannot override methods that exist on it
-- So having, for example, a custom ENT:SetPos method for a single class was impossible since it would never be called
-- With this hook, all you need to do is set the ENT.UseCustomIndex flag to true
-- You might still need to call the original function from the entity metatable inside yours.

local EntMeta = FindMetaTable("Entity")
local Index = EntMeta.__index
local SENTs = scripted_ents
local Custom = {}

function EntMeta:__index(Key, ...)
local Class = Custom[self]

if Class and Key then
local Value = SENTs.GetMember(Class, Key)

if Value ~= nil then return Value end
end

return Index(self, Key, ...)
end

hook.Add("OnEntityCreated", "ACF Custom __index Metamethod", function(Entity)
if not Entity.UseCustomIndex then return end

Custom[Entity] = Entity:GetClass()

Entity:CallOnRemove("ACF Custom Index", function()
Custom[Entity] = nil
end)
end)
end
2 changes: 2 additions & 0 deletions lua/acf/menu/tool_functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ do -- Tool Functions Loader
local Name = net.ReadString()
local Value = net.ReadInt(8)

if not IsValid( LocalPlayer() ) then return end

local Data = Tools[ToolName]
local Tool = LocalPlayer():GetTool(ToolName)

Expand Down
2 changes: 1 addition & 1 deletion lua/entities/acf_ammo/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ do -- Spawning and Updating --------------------

Crate:SetMaterial("phoenix_storms/Future_vents")
Crate:SetPlayer(Player)
Crate:SetModel(Model)
Crate:SetScaledModel(Model)
Crate:SetAngles(Ang)
Crate:SetPos(Pos)
Crate:Spawn()
Expand Down
2 changes: 1 addition & 1 deletion lua/entities/acf_armor/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ do -- Spawning and Updating
Player:AddCount("_acf_armor", Plate)
Player:AddCleanup("_acf_armor", Plate)

Plate:SetModel("models/holograms/cube.mdl")
Plate:SetScaledModel("models/holograms/cube.mdl")
Plate:SetMaterial("sprops/textures/sprops_metal1")
Plate:SetPlayer(Player)
Plate:SetAngles(Angle)
Expand Down
2 changes: 1 addition & 1 deletion lua/entities/acf_fueltank/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ do -- Spawn and Update functions
Entity.ACF = Entity.ACF or {}
Entity.ACF.Model = FuelTank.Model -- Must be set before changing model

Entity:SetModel(FuelTank.Model)
Entity:SetScaledModel(FuelTank.Model)

Entity:PhysicsInit(SOLID_VPHYSICS)
Entity:SetMoveType(MOVETYPE_VPHYSICS)
Expand Down
2 changes: 1 addition & 1 deletion lua/entities/acf_gun/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ do -- Spawn and Update functions --------------------------------

Entity.ACF.Model = Model

Entity:SetModel(Model)
Entity:SetScaledModel(Model)
Entity:SetScale(Scale)

-- Storing all the relevant information on the entity for duping
Expand Down
2 changes: 1 addition & 1 deletion lua/entities/acf_piledriver/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ do -- Spawning and Updating --------------------

Entity.ACF.Model = Class.Model -- Must be set before changing model

Entity:SetModel(Class.Model)
Entity:SetScaledModel(Class.Model)
Entity:SetScale(Scale)

-- Storing all the relevant information on the entity for duping
Expand Down
17 changes: 6 additions & 11 deletions lua/entities/base_scalable/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,13 @@ do -- Network sender and receivers
end)
end

do -- ENT:SetModel override
local EntMeta = FindMetaTable("Entity")
function ENT:SetScaledModel( Model )
self:SetModel( Model )

function ENT:SetModel(Model, ...)
local Data = self.ScaleData

if Model and (Data.Type ~= "Model" or Data.Path ~= Model) then
self:SetScaleData("Model", Model)
self:Restore()
end

return EntMeta.SetModel(self, Model, ...)
local Data = self.ScaleData
if Model and (Data.Type ~= "Model" or Data.Path ~= Model) then
self:SetScaleData("Model", Model )
self:Restore()
end
end

Expand Down

0 comments on commit ba4b8f6

Please sign in to comment.