Hey guys, i've another problem...
I wanna copy a whole SWEP table, but the weapons.Get("...") and the table.Copy(..., ...) function don't work properly, because they have problems with copying functions (i believe).
I created the following code:
function plymeta:RegisterNewWeapon(wep)
local newWep = wep .. "tttc"
local wepTbl = table.Copy(weapons.Get(wep)) - no matter whether i use it with or without "table.Copy"
if not wepTbl then return end
wepTbl.CanBuy = {} -- change whether t/d can buy it in the shop
wepTbl.Kind = -1 -- pickup-able
wepTbl.Slot = 10 -- dont block other weapons in the shop
wepTbl.ClassName = newWep -- not necessary but prevention...
weapons.Register(wepTbl, newWep) -- register new class
return newWep -- return it
end
I tried the following function to copy a SWEP:
function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
This function works to copy the SWEP table, but after few minutes, a stackoverflow error occurs.
Do you have any ideas?
weapons.Get is a copy, no reason to use table.Copy. It's not a normal table, it's a meta table. Try to set __index of your new table to your new table. (Metatables and Metamethods)
Nice, thank you! Great knowledge!
I solved this in this way (like you said):
local wepTbl = weapons.Get("...") -- the weapon class table
wepTbl.__index = wepTbl -- set the metatable
Sorry, you need to Log In to post a reply to this thread.