1. This forum is ARCHIVED! Visit the new Cloud Sixteen forums, codename Eden, at https://eden.cloudsixteen.com. These forums will remain archived for a few months before being closed down, so try to encourage plugin developers to begin moving their content across to the new forums.
Dismiss Notice
Hi Guest, you need a Steam account to register and post on these forums. Login with Steam at the top of the forums to get started!
Dismiss Notice
Hi Guest, do you want to buy HL2RP or another Clockwork schema? Visit the Cloud Sixteen Store to get started!

Modifying Inventory Weight If Item Is Equipped?

Discussion in 'Development' started by Lone, Jul 5, 2013.

  1. Lone

    Lone i'm the coolest kid on the block

    Trying to do something similar to:


    -snip, replaced example-
    Code:
    local ITEM = Clockwork.item:New();
    ITEM.name = "Backpack";
    ITEM.model = "models/eltaco/backpack.mdl";
    ITEM.weight = 1;
    ITEM.uniqueID = "backpack";
    ITEM.useText = "Wear";
    ITEM.category = "Storage";
    ITEM.description = "A black, military styled MOLLE backpack.";
    ITEM.isAttachment = true;
    ITEM.attachmentBone = "ValveBiped.Bip01_Spine";
    ITEM.attachmentOffsetAngles = Angle(0, 0, 0);
    ITEM.attachmentOffsetVector = Vector(-3.96, 4.95, -2.97);
    
    -- A function to get the maximum amount of weight the local player can carry.
    function Clockwork.player:GetMaxWeight()
    	local addInvSpace = v("addInvSpace");
    	local weight = Clockwork.Client:GetSharedVar(
    		"InvWeight", Clockwork.config:Get("default_inv_weight"):Get()
    	);
    
    	if (Clockwork.Client:GetSharedVar("backPack")) then
    		player.addInvSpace = 4;
    
    		for k, v in pairs(itemsList) do
    			local addInvSpace = v("addInvSpace");
    		
    			if (addInvSpace) then
    				weight = weight + addInvSpace;
    			end;
    		end;
    	end;
    end;
    
    -- Called when the attachment offset info should be adjusted.
    function ITEM:AdjustAttachmentOffsetInfo(player, entity, info)
    	if (string.find(player:GetModel(), "female")) then
    		info.offsetVector = Vector(0, 3, 1.75);
    	end;
    end;
    
    -- A function to get whether the attachment is visible.
    function ITEM:GetAttachmentVisible(player, entity)
    	if (player:GetSharedVar("backPack")) then
    		return true;
    	end;
    end;
    
    -- Called when the item's local amount is needed.
    function ITEM:GetLocalAmount(amount)
    	if (Clockwork.Client:GetSharedVar("backPack")) then
    		return amount - 1;
    	else
    		return amount;
    	end;
    end;
    
    -- Called to get whether a player has the item equipped.
    function ITEM:HasPlayerEquipped(player, arguments)
    	return player:GetSharedVar("backPack");
    end;
    
    -- Called when a player has unequipped the item.
    function ITEM:OnPlayerUnequipped(player, arguments)
    	local skullMaskGear = Clockwork.player:GetGear(player, "BackPack");
    		
    	if (player:GetSharedVar("backPack") and IsValid(skullMaskGear)) then
    		player:SetCharacterData("backpack", nil);
    		player:SetSharedVar("backPack", false);
    			
    		if (IsValid(skullMaskGear)) then
    			skullMaskGear:Remove();
    		end;
    	end;
    		
    	player:GiveItem(Clockwork.item:CreateInstance(self.uniqueID));
    end;
    
    -- Called when a player drops the item.
    function ITEM:OnDrop(player, position)
    	if (player:GetSharedVar("backPack") and #player:GetItemsByID(self.uniqueID) == 1) then
    		Clockwork.player:Notify(player, "You cannot drop this while you are wearing it!");
    			
    		return false;
    	end;
    end;
    
    -- Called when a player uses the item.
    function ITEM:OnUse(player, itemEntity)
    	if (player:Alive() and !player:IsRagdolled()) then
    		Clockwork.player:CreateGear(player, "BackPack", self);
    			
    		player:SetCharacterData("backpack", true);
    		player:SetSharedVar("backPack", true);
    		player:GiveItem(Clockwork.item:CreateInstance(self.uniqueID));
    			
    		if (itemEntity) then
    			return true;
    		end;
    	else
    		Clockwork.player:Notify(player, "You don't have permission to do this right now!");
    	end;
    	
    	return false;
    end;
    
    ITEM:Register();
    Trying to get it to call if a backpack is being worn in the equipment tab, to add additional inventory weight.

    Any suggestions on how to do it?
     
  2. You provided incomplete code.
     
  3. Lone

    Lone i'm the coolest kid on the block

    Sorry, I was messing around with stuff.

    Here's the full item.

    Code:
    local ITEM = Clockwork.item:New();
    ITEM.name = "Backpack";
    ITEM.model = "models/eltaco/backpack.mdl";
    ITEM.weight = 1;
    ITEM.uniqueID = "backpack";
    ITEM.useText = "Wear";
    ITEM.category = "Storage";
    ITEM.description = "A black, military styled MOLLE backpack.";
    ITEM.isAttachment = true;
    ITEM.attachmentBone = "ValveBiped.Bip01_Spine";
    ITEM.attachmentOffsetAngles = Angle(0, 0, 0);
    ITEM.attachmentOffsetVector = Vector(-3.96, 4.95, -2.97);
    
    -- A function to get the maximum amount of weight the local player can carry.
    function Clockwork.player:GetMaxWeight()
    	local addInvSpace = v("addInvSpace");
    	local weight = Clockwork.Client:GetSharedVar(
    		"InvWeight", Clockwork.config:Get("default_inv_weight"):Get()
    	);
    
    	if (Clockwork.Client:GetSharedVar("backPack")) then
    		player.addInvSpace = 4;
    
    		for k, v in pairs(itemsList) do
    			local addInvSpace = v("addInvSpace");
    		
    			if (addInvSpace) then
    				weight = weight + addInvSpace;
    			end;
    		end;
    	end;
    end;
    
    -- Called when the attachment offset info should be adjusted.
    function ITEM:AdjustAttachmentOffsetInfo(player, entity, info)
    	if (string.find(player:GetModel(), "female")) then
    		info.offsetVector = Vector(0, 3, 1.75);
    	end;
    end;
    
    -- A function to get whether the attachment is visible.
    function ITEM:GetAttachmentVisible(player, entity)
    	if (player:GetSharedVar("backPack")) then
    		return true;
    	end;
    end;
    
    -- Called when the item's local amount is needed.
    function ITEM:GetLocalAmount(amount)
    	if (Clockwork.Client:GetSharedVar("backPack")) then
    		return amount - 1;
    	else
    		return amount;
    	end;
    end;
    
    -- Called to get whether a player has the item equipped.
    function ITEM:HasPlayerEquipped(player, arguments)
    	return player:GetSharedVar("backPack");
    end;
    
    -- Called when a player has unequipped the item.
    function ITEM:OnPlayerUnequipped(player, arguments)
    	local skullMaskGear = Clockwork.player:GetGear(player, "BackPack");
    		
    	if (player:GetSharedVar("backPack") and IsValid(skullMaskGear)) then
    		player:SetCharacterData("backpack", nil);
    		player:SetSharedVar("backPack", false);
    			
    		if (IsValid(skullMaskGear)) then
    			skullMaskGear:Remove();
    		end;
    	end;
    		
    	player:GiveItem(Clockwork.item:CreateInstance(self.uniqueID));
    end;
    
    -- Called when a player drops the item.
    function ITEM:OnDrop(player, position)
    	if (player:GetSharedVar("backPack") and #player:GetItemsByID(self.uniqueID) == 1) then
    		Clockwork.player:Notify(player, "You cannot drop this while you are wearing it!");
    			
    		return false;
    	end;
    end;
    
    -- Called when a player uses the item.
    function ITEM:OnUse(player, itemEntity)
    	if (player:Alive() and !player:IsRagdolled()) then
    		Clockwork.player:CreateGear(player, "BackPack", self);
    			
    		player:SetCharacterData("backpack", true);
    		player:SetSharedVar("backPack", true);
    		player:GiveItem(Clockwork.item:CreateInstance(self.uniqueID));
    			
    		if (itemEntity) then
    			return true;
    		end;
    	else
    		Clockwork.player:Notify(player, "You don't have permission to do this right now!");
    	end;
    	
    	return false;
    end;
    
    ITEM:Register();
     
  4. Lone

    Lone i'm the coolest kid on the block

    Also, currently results in:

    [Clockwork] The 'ScoreboardShow' hook failed to run.
    gamemodes/outlands/schema/items/sh_storage_backpack.lua:21: attempt to call global 'v' (a nil value)

    When you attempt to open the tab menu.
     
  5. Arbiter329

    Arbiter329 Owner of Arc-Factory Roleplay

    v Isn't defined for 'local addInvSpace = v("addInvSpace");'
     
  6. He made a new post.
     
  7. Arbiter329

    Arbiter329 Owner of Arc-Factory Roleplay

    Eh?
     
  8. Lone

    Lone i'm the coolest kid on the block

    I looked through both of the max weight functions. Where would v be defined?

    cl_player:
    Code:
    -- A function to get the maximum amount of weight the local player can carry.
    function Clockwork.player:GetMaxWeight()
    	local itemsList = Clockwork.inventory:GetAsItemsList(
    		Clockwork.inventory:GetClient()
    	);
    	local weight = Clockwork.Client:GetSharedVar(
    		"InvWeight", Clockwork.config:Get("default_inv_weight"):Get()
    	);
    	
    	for k, v in pairs(itemsList) do
    		local addInvSpace = v("addInvSpace");
    		
    		if (addInvSpace) then
    			weight = weight + addInvSpace;
    		end;
    	end;
    	
    	return weight;
    end;
    sh_kernel:
    Code:
    -- A function to get the maximum weight a player can carry.
    function playerMeta:GetMaxWeight()
    	local itemsList = Clockwork.inventory:GetAsItemsList(self:GetInventory()); 
    	local weight = self:GetSharedVar("InvWeight");
    	
    	for k, v in pairs(itemsList) do
    		local addInvSpace = v("addInvSpace");
    		if (addInvSpace) then
    			weight = weight + addInvSpace;
    		end;
    	end;
    	
    	return weight;
    end;
     
  9. Arbiter329

    Arbiter329 Owner of Arc-Factory Roleplay

    It's defined a few lines down. Just delete the first instance of local addInvSpace = v("addInvSpace");
     
  10. kurozael

    kurozael Cloud Sixteen Director Staff Member Administrator Investor

    ITT: People try and hack together something by overwriting framework functions instead of just using the functionality that the framework method is ironically providing for you.

    Here:

    Code:
    --[[
    	Also if you define the item from the accessory_base in 0.88a you make your life a hell of a lot
    	easy for this kind of thing. Because the item then becomes an Accessory and you don't have to
    	handle it yourself.
    --]]
    
    local ITEM = Clockwork.item:New("accessory_base");
    ITEM.name = "Backpack";
    ITEM.model = "models/eltaco/backpack.mdl";
    ITEM.weight = 1;
    ITEM.uniqueID = "backpack";
    ITEM.useText = "Wear";
    ITEM.category = "Storage";
    ITEM.description = "A black, military styled MOLLE backpack.";
    ITEM.isAttachment = true;
    ITEM.attachmentBone = "ValveBiped.Bip01_Spine";
    ITEM.attachmentOffsetAngles = Angle(0, 0, 0);
    ITEM.attachmentOffsetVector = Vector(-3.96, 4.95, -2.97);
    
    -- Called when the attachment offset info should be adjusted.
    function ITEM:AdjustAttachmentOffsetInfo(player, entity, info)
    	if (string.find(player:GetModel(), "female")) then
    		info.offsetVector = Vector(0, 3, 1.75);
    	end;
    end;
    
    -- A callback which is called when the item's addInvSpace property is wanted.
    -- Using Item Proxy system.
    function ITEM:GetCustomWeightAdd()
    	for k, v in ipairs(player.GetAll())
    		if (self:HasPlayerEquipped(v)) then
    			return 5; -- How many kg shall we increase the inventory by when wearing it?
    		end;
    	end;
    	
    	return 0;
    end;
    
    ITEM:AddQueryProxy("addInvSpace", ITEM.GetCustomWeightAdd);
    
    -- A function to get whether the attachment is visible.
    function ITEM:GetAttachmentVisible(player, entity)
    	if (player:GetSharedVar("backPack")) then
    		return true;
    	end;
    end;
    
    -- Called when a player wears the accessory.
    function ITEM:OnWearAccessory(player, bIsWearing)
    	if (bIsWearing) then
    		Clockwork.player:CreateGear(player, "BackPack", self);
    		player:SetSharedVar("backPack", true);
    	else
    		local gearEntity = Clockwork.player:GetGear(player, "BackPack");
    		
    		if (IsValid(gearEntity)) then
    			gearEntity:Remove();
    		end;
    		
    		player:SetSharedVar("backPack", false);
    	end;
    end;
    
    ITEM:Register();
    
     
  11. Lone

    Lone i'm the coolest kid on the block

    So I tried out the code you sent, but you were missing a do on line 30, so I added one in. When I equipped the backpack, the server started experiencing a god-awful lag, and I checked the server's console and found this:

    http://4stor.com/3inXS

    However, it does seem to increase weight when worn. It just nearly crashes the server as a side effect. \o/

    I was having this same problem when trying to work on this earlier, using something along the lines of "if item is equipped then ITEM:addInvWeight 4;" or something similar.
     
  12. kurozael

    kurozael Cloud Sixteen Director Staff Member Administrator Investor

    Are you using 0.88a though? It contains the fix for the SaveData error.
     

Previous Readers (Total: 0)