I'm creating a weapon modding addon and i have gotten the modding to work its just I can figure out how to store a var like increased damage then call it when using the EntityTakeDamage hook.
I want to make it so when the weapon is modded it can change the damage but again I don't know how to store it like on the entity so even when the gun is put into an inventory or the player disconnects it keeps the modded damage till they come back
How is the weapon identified in the inventory?
This is an iteration of a PData iteration that I have:
-- easier way to create a simple sql table inside the built in sqlite db
function CreateTable(name, sql_values)
sql.Query("CREATE TABLE IF NOT EXISTS " .. name .. " (" .. sql_values .. ")")
end
-- creating a table for the weapon data functions
CreateTable("universaldata", "id TEXT NOT NULL PRIMARY KEY, value TEXT")
-- sets the value attached to that data_id that belongs to said weapon id
function SetWeaponData(weapon_id, data_id, value)
data_id = Format("%s[%s]", weapon_id, data_id)
sql.Query("REPLACE INTO universaldata (id, value) VALUES (" .. SQLStr(data_id) .. ", " .. SQLStr(value) .. ")")
end
-- returns the value attached to that data_id that belongs to said weapon id
function GetWeaponData(weapon_id, data_id, default)
data_id = Format("%s[%s]", weapon_id, data_id)
local value = sql.QueryValue("SELECT value FROM universaldata WHERE id = " .. SQLStr(data_id) .. " LIMIT 1")
if value == nil then return default end
return value
end
How you'd use this:
SetWeaponData(weapon_id, "damage", damage)
GetWeaponData(weapon_id, "damage", 1)
btw if you can, I'd strongly suggest putting any function you make into a general table for your script. Example:
script_name = {}
script_name.section = {}
script_name.section.doSomething = function(value)
end
script_name.section.doSomething(12)
I'm using IDinventory and there is an SWEP for picking up items that calls a function that runs this huge script but I think it shows how the item is stored. Heres a pastebin of the function Pastebin.com
Also how would I set up the PData because i have never used it before so I'm not sure how to do the file structure or stuff like that
Thanks for uploading that bit of a gmod store script, now I know that you don't actually need organized and well optimized code to sell something on there.
Anyways, just from looking at it I can't find any way for us to be able to identify the weapon, if you don't care about each specific inventory item having a certain amount of damage spec, you can try using the following when setting it all up..
-- this one sets the damage
function setCustomDamage(ply, weapon, damage)
if not (IsValid(ply) and IsValid(weapon) then return end
-- the way pdata works is it basically uses the gmod built in sqlite database to store a specific piece of data
-- that's attached to a player's id, which can be retrieved using a specific identifier
ply:SetPData(weapon:GetClass(), damage)
end
-- this one returns it so you can use it in your hook or whatever
function getCustomDamage(ply, weapon)
if not (IsValid(ply) and IsValid(weapon) then return end
-- same as above but uses the weapon class(like above when we used the weapon class to save it) to retrieve the piece of data
-- that is "attached" to the player
-- the false just means that in case it can't find any data on the custom damage, it'll return false instead of damage
-- so you can make it so if it returns false then the damage should not be changed
return ply:GetPData(weapon:GetClass(), false)
end
I'm starting to understand this and yea I have got a few scripts from gmod store that were not organized at all. But say I want to have like different like damages for the same gun is there a way to get like a certain ID of that weapon?
Sorry, you need to Log In to post a reply to this thread.