table.remove - Doesn't actually remove the key to my table.

This is the table.remove() debug log that i have:


Strip both guns:
1=weapon_physgun
2=weapon_357
given physics gun:
1=weapon_357

Save physics gun:
1=weapon_physgun
2=weapon_357
3=weapon_physgun
Give 357:
1=weapon_physgun-- as you can see it still exists
2=weapon_physgun

This is the table.remove() code:
[lua]
local clear = table.KeyFromValue(self.inventory, itemClass)
self:Give(itemClass)
PrintTable(self.inventory)
print("
")
table.remove(self.inventory, clear)-- doesnt clear it
PrintTable(self.inventory)
self:SaveInventory()
[/lua]

I debugged it and it says it still exists even though I printed out the table and saw that it was removed.
[lua]
if table.HasValue(temp_inv, k) then
for k,v in pairs(temp_inv) do
print(k,v)
print(“You cannot have doubles of this!”)–returns with this after i try and store the weapon after giving it back to myself
end
else
temp_inv[#temp_inv + 1] = k
self:StripWeapon(k)
– print(temp_inv[#temp_inv + 1])
– print(self.inventory[#self.inventory+1])
end
[/lua]

Any ideas of how to remove the duplicate that was supposed to be deleted in the debug log at the beginning?

Your code doesn’t make sense, your debug message doesn’t make sense, and the fact that you’ve given us 30% of code makes even less sense. What are you trying to do, why are you doing it, show us the whole code.

Sorry forgot about that.

Now that I have had some sleep… :v:

[lua]
local meta = FindMetaTable(“Player”)
if !meta then return end

self.inventory = {}

function meta:SaveCurrentWeapon()
– Holds the guns that you are not allowed to save
local restricted_guns = {
[“weapon_physgun”] = “models/weapons/v_superphyscannon.mdl”,
[“weapon_357”] = “models/weapons/v_superphyscannons.mdl”
}
local MyGun = self:GetActiveWeapon():GetClass() – Our Gun

    -- Is the gun restricted?
    for k, v in pairs(restricted_guns) do
            if (MyGun != k) then
                    self.inventory[MyGun] = self:GetACtiveWeapon():GetModel() -- I am not sure why the hell you are using VIEW models, but w/e
                    self:StripWeapon(MyGun) -- You were doing this wrong! You were trying to strip restricted weapons, not the active one.
                    self:SaveInventory()
                    return
            end
    end

end

– Saves the inventory in a text file
function meta:SaveInventory()
file.Write(“invalpha/”…self:UniqueID()…"_weapons.txt", glon.encode(self.inventory))
end

–Loads the inventory onto the player. This should be done when the player joins the game.
function meta:LoadInventory()
self.inventory = glon.decode(file.Read(“invalpha/” … self:UniqueID() … “_weapons.txt”)) or {}
end

concommand.Add(“self_equip”, function(self,cmd,args)

    local arg1 = tostring(args[1]) -- I am assuming this is the Model based on what you had before. I changed to Class Name.
                                   -- Reason being is... What happens if you have more than 1 gun with the same model? The first
                                   -- will always be returned, rather than the one you chose. FIX THIS.
                                   
    for k, v in pairs(self.inventory) do
            if (arg1 == k) then
                    self:Give(k) -- Why the hell were you using the Model as the argument. Use the CLASS NAME
                    self.inventory[k] = nil -- We are not using Numerical indexes. We must set it to nil, according to Wizard Of Ass.
                    -- We do need to do anything else. Save and quit
                    self:SaveInventory()
                    return
            end
    end

end)

concommand.Add(“self_strip”, self:SaveCurrentWeapon())
[/lua]

If you put out code quality with that (Referring to commented code.), everyone will get along so much better and we can understand what you actually want.

[editline]4th July 2012[/editline]

Also… Thank you for cleaning it up. Made it so much easier to understand what you were attempting to do and how it was intended to work.

Thanks for revising this script, I’ll go through this one piece at a time.

  1. I thought you said I shouldn’t have global tables because they’d be overwritten?
    self.inventory = {}

  2. I noticed the looping error as well while checking the table instead of the current weapon.

  3. This made me laugh how you worded it. I was basing the arguments off the weapon class not the model. :downs: i.e. Why the hell were you using the Model as the argument. Use the CLASS NAME<-- :suicide:

  4. I didn’t see any table.remove arguments anywhere… nvm I had a bug in my eye.

Now you have me doubting myself… You may be right. I am not sure.

To avoid the whole issue, you can actually just remove that line altogether. It is handled by the LoadInventory function anyway…

Umm… what do you mean remove the line? Then there won’t be a table!

3: From the looks of it… I thought you were doing the opposite. Using the Model name as the argument. If you were using the actual class name… no wonder it didn’t work :stuck_out_tongue:

[editline]4th July 2012[/editline]

Ah. Damn you broke my auto merge…

Take a look:

[lua]
function meta:LoadInventory()
self.inventory = glon.decode(file.Read(“invalpha/” … self:UniqueID() … “_weapons.txt”)) or {}
end
[/lua]

When the player connects, call ply:LoadInventory()… thats it

Okay I can see how it can be loaded.

If you were using the actual class name… no wonder it didn’t work :stuck_out_tongue:

What do you mean “didn’t work”?

[lua]
local arg1 = tostring(args[1])
for k, v in pairs(self.inventory) do
print(k,v)

				-- According to your player table and how you have everything set up so far:
				--    This is K    =    This is V
				--  ["weapon_357"] = "models/weapons/v_superphyscannons.mdl"
                if (arg1 == v) then -- This line here. You are asking if the argument is the model of the weapon.
									-- so if Arg1 is "models/weapons/v_superphyscannons.mdl", then it will give an item.
									-- obviously you want if Arg1 is "weapon_357"
                        self:GiveItem(v)
                end
        end

[/lua]

That is what you had before…

I changed it to check the actual class name for 2 reasons: 1) If the models are the same for different guns, no matter what, which ever gun appears first will be equipped. 2) More logical to do so, as it makes actually giving the weapon easier.

Thus, it has been modified to:

[lua]
for k, v in pairs(self.inventory) do
if (arg1 == k) then
self:Give(k) – Why the hell were you using the Model as the argument. Use the CLASS NAME
self.inventory[k] = nil – We are not using Numerical indexes. We must set it to nil, according to Wizard Of Ass.
– We do need to do anything else. Save and quit
self:SaveInventory()
return
end
end
[/lua]

[editline]4th July 2012[/editline]

So… if you argument was a class name instead of the model, you would NEVER get a true case, and you would never been given a weapon

I was never actually storing the models in the table:
[lua]
if (self:GetActiveWeapon():GetClass() == k) then
temp_inv[#temp_inv + 1] = k
end
[/lua]

I think you’re getting confused with the table structure.
When I inserted a weapon the inventory table would look like this:
1 = weapon_physgun
That’s why I used V the model isn’t V if it was it would have to be temp_inv[k][2]

Oh I missed that portion. Before you said you needed models for that Derma menu you talked about in our PMs from before.

Ah. I see. Once again, you were being inconsistent! :suicide:
[lua]
local restricted_guns = {
[“weapon_physgun”] = “models/weapons/v_superphyscannon.mdl”,
[“weapon_357”] = “models/weapons/v_superphyscannons.mdl”
}
[/lua]
You still maintained that data structure for the temp_guns. I assumed that was for the whole structure so I kept it that way in my mindset.

[editline]4th July 2012[/editline]

Yes… I was.

So do you know why table.remove() wasn’t co-operating with my table?

Not a clue. Theoretically, you did everything correctly (Now that I know you just have numerical indexes… :dance:)

“Theoretically, you did everything correctly” This makes me laugh because If I did do it all correctly then it should have worked but whatever if I run into another problem while implementing small parts of your code into mine ill let you know. :rolleyes:

Thanks for helping me! :smile:

Ah I got it:

local temp_inv = {}

That’s a global variable. You defined it in the beginning of the script, and you are assigning stuff to it. However, you never clear it when you call Player:SaveCurrentWeapon(). Thus you are getting all the items from the previous saves, and adding to it. Had you put temp_inv inside the Player:SaveCurrentWeapon() function, you would have been golden.

SO CLOSE MATE!

EDIT:

It is “Global” in the sense that anything can access it and will never be removed. Setting a variable to *Local *means it is removed from memory once it comes out of *scope *of the script. Since it is not in a function, it will *never *come out of scope. It will however, only be accessed by code in that file, or anything that includes it.

I was told by Ralle in this post http://forum.facepunch.com/showthread.php?t=1195457:
Declare inventory outside the function.

Heh. Disagree with me eh? Put it here:

[lua]
function Player:SaveCurrentWeapon()
local temp_inv = {}
local temp_guns = {
[“weapon_physgun”] = “models/weapons/v_superphyscannon.mdl”,
[“weapon_357”] = “models/weapons/v_superphyscannons.mdl”
}

    for k, v in pairs(temp_guns) do
            if (self:GetActiveWeapon():GetClass() == k) then
                    temp_inv[#temp_inv + 1] = k
                    self:StripWeapon(k)
                    -- print(temp_inv[#temp_inv + 1])
                    -- print(self.inventory[#self.inventory+1])
            end
    end
    self.inventory = temp_inv
    self:SaveInventory()

end
[/lua]

put it there and I am willing to bet you will have it working with your original code. :smiley:

[editline]4th July 2012[/editline]

Yes, yes. the INVENTORY. That is not your inventory, that is a table that technically holds all of the saved items from each call… Do what I suggested and see what happens.

I am almost willing to be money on it :v:

It overwrites itself if I put it there, that’s why I made a thread regarding why table.insert() wasn’t working.
The code you posted is the exact same as my old one just the table name was changed to temp_inv.

Ah. Derp! Ok, here:

[lua]
function Player:SaveCurrentWeapon()
local temp_guns = {
[“weapon_physgun”] = “models/weapons/v_superphyscannon.mdl”,
[“weapon_357”] = “models/weapons/v_superphyscannons.mdl”
}

    for k, v in pairs(temp_guns) do
            if (self:GetActiveWeapon():GetClass() == k) then
                    --temp_inv[#temp_inv + 1] = k -- This is what it was
		self.inventory[#self.inventory + 1] = k -- What it needed to be
                    self:StripWeapon(k)
                    -- print(temp_inv[#temp_inv + 1])
                    -- print(self.inventory[#self.inventory+1])
            end
    end
    --self.inventory = temp_inv
    self:SaveInventory()

end
[/lua]

[editline]4th July 2012[/editline]

That way, you arent overwriting anything… just adding to it.