table.remove - Doesn't actually remove the key to my table.
23 replies, posted
This is the table.remove() debug log that i have:
[CODE]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[/CODE]
This is the table.remove() code:
[lua]
local clear = table.KeyFromValue(self.inventory, itemClass)
self:Give(itemClass)
PrintTable(self.inventory)
print("\n")
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.
[QUOTE=Donkie;36620285]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.[/QUOTE]
[url]http://pastebin.com/RumbnUKV[/url]
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 [B][I]want[/I][/B].
[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.
[QUOTE=SeveredSkull;36621178]Now that I have had some sleep... :v:
-revised-
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 [B][I]want[/I][/B].
[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.[/QUOTE]
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...
[QUOTE=SeveredSkull;36621408]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...[/QUOTE]
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 :P
[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
[QUOTE=SeveredSkull;36621426]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 :P
[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[/QUOTE]
Okay I can see how it can be loaded.
If you were using the actual class name... no wonder it didn't work :P
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
[QUOTE=SeveredSkull;36621541]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[/QUOTE]
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]
[QUOTE=brandonj4;36621586]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]
:)[/QUOTE]
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:)
[QUOTE=SeveredSkull;36621905]Not a clue. Theoretically, you did everything correctly (Now that I know you just have numerical indexes... :dance:)[/QUOTE]
"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 [I]Local [/I]means it is removed from memory once it comes out of [I]scope [/I]of the script. Since it is not in a function, it will [I]never [/I]come out of scope. It will however, only be accessed by code in that file, or anything that includes it.
[QUOTE=SeveredSkull;36621999]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 [I]Local [/I]means it is removed from memory once it comes out of [I]scope [/I]of the script. Since it is not in a function, it will [I]never [/I]come out of scope. It will however, only be accessed by code in that file, or anything that includes it.[/QUOTE]
I was told by Ralle in this post [URL="http://facepunch.com/showthread.php?t=1195457"]http://facepunch.com/showthread.php?t=1195457[/URL]:
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. :D
[editline]4th July 2012[/editline]
[QUOTE=brandonj4;36622029]I was told by Ralle in this post [URL="http://facepunch.com/showthread.php?t=1195457"]http://facepunch.com/showthread.php?t=1195457[/URL]:
Declare inventory outside the function.[/QUOTE]
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.
[QUOTE=SeveredSkull;36622119]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.[/QUOTE]
Are there any consequences to ONLY having a global table. What was the point in having a local table before..?
[QUOTE=brandonj4;36622059]
The code you posted is the exact same as my old one just the table name was changed to temp_inv.[/QUOTE]
No... If you look carefully... I changed it:
self.inventory[MyGun] = self:GetACtiveWeapon():GetModel()
I omitted the use of a "temp_inv" completely, just as I forgot to do with my last suggestion.
[editline]4th July 2012[/editline]
[QUOTE=brandonj4;36622146]Are there any consequences to ONLY having a global table. What was the point in having a local table before..?[/QUOTE]
well when you do self.inventory, you are storing it on the player, in that aspect, it is globally accessable, but it will only affect that one player, which is exactly what you want. Each player will have their respective tables. What you were doing before, was sharing EVERYONE's inventory in the same table.
[QUOTE=SeveredSkull;36622155]No... If you look carefully... I changed it:
self.inventory[MyGun] = self:GetACtiveWeapon():GetModel()
I omitted the use of a "temp_inv" completely, just as I forgot to do with my last suggestion.
[editline]4th July 2012[/editline]
well when you do self.inventory, you are storing it on the player, in that aspect, it is globally accessable, but it will only affect that one player, which is exactly what you want. Each player will have their respective tables. What you were doing before, was sharing EVERYONE's inventory in the same table.[/QUOTE]
How was I sharing EVERYONE's inventory in the same table?
[editline]4th July 2012[/editline]
[QUOTE=brandonj4;36622200]How was I sharing EVERYONE's inventory in the same table?[/QUOTE]
Lmfao I just saw what you did. If I did inv = {} there would be no variable for the player okay I get it now. :suicide:
because it was not stored to the meta table
You had this: local temp_inv = {}
This would be read by the server as "Make a new variable called temp_inv, create it in the scope of the script. My scope of the script is the main serverside [I](since this is included in init.lua or shared.lua. I am not sure which one you are using it in. I am assuming init.lua)[/I]" as in, you can access this anywhere serverside that has included this file.
Just because you are creating variables within a file that is meant for meta tables, doesn't mean any variable declared there will be IN the meta table. In order for you to bind it to the meta table you must do this:
local Player.temp_inv = {}
This is read by the server as follows: "Take the players meta table, and create a new variable called temp_inv and store it inside the players table. The scope of this variable is of the players meta table."
Hope that clears it up.
[editline]4th July 2012[/editline]
Allrighty, based on the new information with our little conversation, here is the exact code you want:
[lua]
local meta = FindMetaTable("Player")
if !meta then return end
function meta:SaveCurrentWeapon()
-- Holds the guns that you are not allowed to save
local restricted_guns = {"weapon_physgun", "weapon_357"}
local MyGun = self:GetActiveWeapon():GetClass() -- Our Gun
-- Is the gun restricted?
for k, v in pairs(restricted_guns) do
if (MyGun != v) then
-- Gun is not restricted and we are allowed to save it
self.inventory[#self.inventory + 1] = MyGun -- take our gun
self:StripWeapon(MyGun) -- Strip it
self:SaveInventory() -- Save the inventory with the new set
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()
-- Setup our inventory table
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]) -- Holds the classname of the gun
for k, v in pairs(self.inventory) do -- Look in our inventory and find the gun
if (arg1 == v) then
-- The argument is the gun we want
self:Give(v) -- Give the player the gun
table.remove(self.inventory, k )-- Remove the index associated with the gun
-- Here, K is a number, so we dont have to use the
-- ValueToKey() function anymore
-- We do need to do anything else. Save and quit
self:SaveInventory()
return
end
end
end)
concommand.Add("self_strip", self:SaveCurrentWeapon())
[/lua]
Sorry, you need to Log In to post a reply to this thread.