Table.Remove or Setting table = nil

I recently read this:
http://forum.facepunch.com/showthread.php?t=1194493

This thread didn’t help much with my table. I need the table of the weapons that are stored in a specific way to be Completely Removed when the player uses a console command.

This is the table storing method:
[lua]
for k, item in pairs(temp_guns) do
if (activeWep == k) then
table.insert(inv, {[self:GetActiveWeapon():GetClass()] = {model = self:GetActiveWeapon():GetModel(),class = self:GetActiveWeapon():GetClass()}})
PrintTable(inv)
self:StripWeapon(k)
[/lua]

This is the console command and where the issue kicks in:
[lua]
local data = glon.decode(file.Read(“invalpha/” … self:UniqueID() … “_weapons.txt”)) or {Inventory = {}}
local arg1 = tostring(args[1])
for k, v in pairs(data.Inventory) do
for index, inv_data in pairs(data.Inventory[k]) do
print(index)
–returns index of weapon class name: THIS IS THE TABLE OF SELF:GETACTIVEWEAPON():GETCLASS()
for weapon,info in pairs(inv_data) do
print(data.Inventory[k][index])
– print(weapon) returns the string model
– print(info) returns model path
if (self:GetActiveWeapon():GetClass() == info) then
return print(“you cant”)
elseif arg1 == info then
self:Give(info)
– data.Inventory[k][index] = nil: THIS DIDN’T WORK.
PrintTable(data.Inventory)
file.Write(“invalpha/”…self:UniqueID()…"_weapons.txt", glon.encode({Inventory = inv}))
end
end
end
end
[/lua]

data.Inventory[k][index] = nil returned this:


1:
2:
3:
4:

–okay nil^


1:
		weapon_physgun:
				class	=	weapon_physgun
				model	=	models/weapons/v_superphyscannon.mdl
2:
		weapon_physgun:
				class	=	weapon_physgun
				model	=	models/weapons/v_superphyscannon.mdl
3:
		weapon_physgun:
				class	=	weapon_physgun
				model	=	models/weapons/v_superphyscannon.mdl
4:
		weapon_physgun:
				class	=	weapon_physgun
				model	=	models/weapons/v_superphyscannon.mdl
5:
		weapon_physgun:
				class	=	weapon_physgun
				model	=	models/weapons/v_superphyscannon.mdl

When updated^

I’m getting really frustrated since it’s my first attempt at an inventory system and I can’t grasp the concept of tables after all I read on the different wiki’s.

Could someone please explain how… to fix this issue with removing the specific table, which refers back to the class name that is being stored?

I don’t really understand your table structure…
why do you use number indexes? Why use the class of the weapon as the key of the table and then the amount of weapons as the value…

Then you could use any entity really with that system…

Then show me how you would do it. This is the only way I could think of doing it while implementing the functions.

What do you mean implementing the functions? :S

The functions of giving/stripping the weapon and deleting it from the table. My format of this table is the only way I could think of doing it.

Give me a few minutes and I’ll whip something up, okay? :slight_smile:

Sounds unpleasant but alright. :quagmire:

Sorry facepunch was being slow for me.

[lua]
–Construction of the inventory… probably best to call after a player gets a wepaon or a change is made to their set.
function playerMeta:ReconstructInventory()
local inventory = {}
for k, v in pairs(self:GetWeapons()) do
local class = v:GetClass()
inventory[class] = true
end
self.inventory = inventory
self:SaveInventory()
end

–A function to save the inventory to the file
function playerMeta:SaveInventory()
file.Write(“inventories/”…self:SteamID()…"_weapons.txt", glon.encode(self.inventory))
end

–Loading the inventory from the file, and giving them the weapons.
function playerMeta:LoadInventory()
self:StripWeapons()
self.inventory = glon.decode(file.Read(“inventory/”…self:SteamID()…"_weapons.txt")) or {}
for k, v in pairs(self.inventory) do
self:Give(k)
end
end

–A function to remove a specific weapon class
function playerMeta:RemoveItem(itemClass)
self:StripWeapon(itemClass)
self.inventory[itemClass] = nil
self:SaveInventory()
end
[/lua]

Something like that should make a lot more sense and be a lot easier.
playerMeta is the player’s meta table, I’m sure you know what that is already.

Thank you so much. I hate to be even more of a burden but what you wrote doesn’t quite align with what I meant to be an inventory. Do you want me to send you all my code from my previous inventory and see if you can simplify it if you have free time? (I’ll explain it better if you agree)

Well I can quickly adapt it to do what you need…
Just explain to me what you mean by inventory

I PMed you all the info :slight_smile:

i’ll take a look tomorrow i’m going to bed

Anyone have any other solutions?

Might be a good idea to tell us what the hell you need.

Its inside the opening post, but I PMed you exactly what is needed.

[editline]4th July 2012[/editline]

-Lock this thread-