Hello there, i need help with inventory. Im trying to add new function to it like HasSwep, RemoveSwep.
It already has this function but the are not comfortable to use them in custom scripts.
Please help me or give advice how to make this.
[LUA]
function meta:AddSwep(tbl)
local key = #self.inv.sweps + 1
self.inv.sweps[key] = tbl
self:SaveInv()
self:SendSwep(tbl, key)
end
function meta:RemoveSwep(key)
self.inv.sweps[key] = nil
self:SaveInv()
self:SendSwepRemove(key)
end
function meta:RemoveSwep2(tbl)
self.inv.sweps[tbl] = nil
self:SaveInv()
self:SendSwepRemove(key)
end
function meta:SendSwep(tbl,key)
umsg.Start("swep", self)
umsg.Short(key)
umsg.String(tbl.model)
umsg.String(tbl.class)
umsg.End()
end
function meta:SendSwepRemove(key)
umsg.Start("swepgone", self)
umsg.Short(key)
umsg.End()
end
[/LUA]
Here my examples:
[LUA]
ply:HasSwep("deagle")
ply:RemoveSwep("deagle")
[/LUA]
I don't see your point here, you already have the functions you want to add?
Oh and by the way, this is not "write me some code" forum, you have coderhire for that.
[QUOTE=Robotboy655;42887024]I don't see your point here, you already have the functions you want to add?
Oh and by the way, this is not "write me some code" forum, you have coderhire for that.[/QUOTE]
Yea the old function is fine.
But its not comfortable to use them in custom scripts.
I tried myself to solve this to make new one, but sweps saves by key number and i don't understand how to check or remove by swepclass.
I just want some advice!
i'm confused? what do you want
Loop through all of them and compare class from table to the string from argument. If it matches then remove it/return true.
However you gotta handle somehow multiple instances of same weapon class, so there's that.
[QUOTE=LauScript;42887167]i'm confused? what do you want[/QUOTE]
I want to add two new function.
First HasSwep to check for swep
Second RemoveSwep to remove swep
But i want make it easy for example:
ply:RemoveSwep("deagle")
[editline]17th November 2013[/editline]
[QUOTE=Robotboy655;42887199]Loop through all of them and compare class from table to the string from argument. If it matches then remove it/return true.
However you gotta handle somehow multiple instances of same weapon class, so there's that.[/QUOTE]
[LUA]
for k,v inpairs(self.inv.sweps) do
end
[/LUA]
But hot to compare class name with key number?
I assume v has a member that is class of the weapon. I really can't know because I have no idea what you put into the table in AddSwep
AddSwep and SendSwep
[LUA]
function meta:AddSwep(tbl)
local key = #self.inv.sweps + 1
self.inv.sweps[key] = tbl
self:SaveInv()
self:SendSwep(tbl, key)
end
function meta:SendSwep(tbl,key)
umsg.Start("swep", self)
umsg.Short(key)
umsg.String(tbl.model)
umsg.String(tbl.class)
umsg.End()
end
[/LUA]
Table
[LUA]
weps = {
weapon_deagle = {
name = "Deagle",
esc = ""
}
}
[/LUA]
Well, v.class it is by the looks of it. I trust you can manage to compare two values.
Here, but seems not work
[LUA]
function meta:HasSwep(name)
for k,v in pairs(self.inv.sweps) do
local check = v.class or ""
return (check == (name or "") and check)
end
end
[/LUA]
Make sure the tbl in AddSwep has "class" member. Also the code you wrote only checks the first weapon.
[code]
function meta:HasSwep(name)
if ( !name ) then return false end
for k,v in pairs(self.inv.sweps) do
if ( !v.class ) then continue end
if ( v.class:lower() == name:lower() ) then return true end
end
return false
end[/code]
[QUOTE=Robotboy655;42887541]Make sure the tbl in AddSwep has "class" member. Also the code you wrote only checks the first weapon.
[code]
function meta:HasSwep(name)
if ( !name ) then return false end
for k,v in pairs(self.inv.sweps) do
if ( !v.class ) then continue end
if ( v.class:lower() == name:lower() ) then return true end
end
return false
end[/code][/QUOTE]
Thanks seems work!
[editline]17th November 2013[/editline]
[LUA]
function meta:RemoveSwep(key)
self.inv.sweps[key] = nil
self:SaveInv()
self:SendSwepRemove(key)
end
function meta:SendSwepRemove(key)
umsg.Start("swepgone", self)
umsg.Short(key)
umsg.End()
end
[/LUA]
It work like ply:RemoveSwep(id)
Some advice how to make it ply:RemoveSwep("deagle")?
You do the same, only you delete the swep instead of returning true.
Yea the main i understand but how to delete in what way?
[editline]17th November 2013[/editline]
Everywhere the key
[LUA]
local function recsweprem(um)
local key = um:ReadShort()
cl_sweps[key] = nil
end
usermessage.Hook("swepgone", recsweprem)
function meta:RemoveSwep(key)
self.inv.sweps[key] = nil
self:SaveInv()
self:SendSwepRemove(key)
end
function meta:SendSwepRemove(key)
umsg.Start("swepgone", self)
umsg.Short(key)
umsg.End()
end
[/LUA]
[editline]17th November 2013[/editline]
Here someone use tonumber
[LUA]
local item = tonumber(args[1])
if !item or ply.inv.sweps[item] == nil then
return
end
ply:RemoveSwep(item)
[/LUA]
I don't undestand how to get swep key to delete it
[editline]17th November 2013[/editline]
Made this, but it remove all weapon with name deagle
[LUA]
function meta:DeleteSwep(name)
if ( !name ) then return false end
for k,v in pairs(self.inv.sweps) do
if ( !v.class ) then continue end
if ( v.class:lower() == name:lower() ) then
self:RemoveSwep(k)
end
end
return false
end
[/LUA]
[QUOTE=lua_error;42887668]
Made this, but it remove all weapon with name deagle
[LUA]
function meta:DeleteSwep(name)
if ( !name ) then return false end
for k,v in pairs(self.inv.sweps) do
if ( !v.class ) then continue end
if ( v.class:lower() == name:lower() ) then
self:RemoveSwep(k)
end
end
return false
end
[/LUA][/QUOTE]
[LUA]
function meta:DeleteSwep(name)
if ( !name ) then return false end
for k,v in pairs(self.inv.sweps) do
if ( !v.class ) then continue end
if ( v.class:lower() == name:lower() ) then
self:RemoveSwep(k)
break
end
end
return false
end
[/LUA]
That will remove just one of the deagles then it will end the loop.
Sorry, you need to Log In to post a reply to this thread.