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!

Dynamic Item Name

Discussion in 'Programming' started by Sophia, Aug 25, 2014.

  1. Sophia

    Sophia Guest

    I was wondering if it's possible to create an item so that when I use it, it doesn't add/remove that item but infact changes the ID/Name of the item? E.G ID Card - 0 when used might become ID Card - 2439 and would stay at that name.
     
  2. duck

    duck Phant0m Legend

    Code:
    ITEM:AddQueryProxy("name", "name");
    ITEM:AddData("name", "Whatever");
    Then, ITEM:SetData("name", "New Name");

    return false in the OnUse function if you want them to keep the item.
     
  3. You could use AddQueryProxy to do this.

    Code:
    ITEM:AddData("ID", -1)
    ITEM:AddQueryProxy("name", ITEM.GetName);
    
    function ITEM:GetName()
        return "ID Card"..self:GetData("ID", -1);
    end;
    
    if (SERVER) then
        function ITEM:OnInstantiated()
            ITEM:SetData("ID", PLUGIN:GetNextIDCardID();
        end;
    end;
    
    Then just make PLUGIN:GetNextIDCardID() return the next ID (so you can have the plugin save the last ID it generated through restarts).
     
  4. Sophia

    Sophia Guest

    Many thanks!

    Edit - And then I find out I suck at coding and I can't seem to use these at all...

    Tried ducks version, couldn't get it to work. (It would work to some degree, adding the data etc.. but it would change the name to what was in ITEM:AddData("name", "Whatever"); E.G "Whatever" so I tried using that but it came up with the same nil value error.

    Tried Gr4Ss version, can't get that to work.

    Code:
    function ITEM:OnUse()
        ITEM:AddData("ID", -1)
        ITEM:AddQueryProxy("name", ITEM.GetName);
        return false;
    end;
    
    function ITEM:GetName()
        return "Keys - "..self:GetData("ID", -1);
    end;
    
    if (SERVER) then
        function ITEM:OnInstantiated() -- Tried both () and adding in (player)
            ITEM:SetData("ID", player:GetCharacterData("citizenid"));
        end;
    end;
    Except I get an issue that it's getting a nil value from GetCharacterData. I changed my mind from having it create a random number as I'd prefer that it used the citizenid from the character that used the item...

    [member=5482]duck[/member]
    [member=1476]Gr4Ss[/member]

    Halp ;_;
     
  5. The OnItemInstantiated function has no player argument.
    By doing player:GetCharacterData you're trying to call an existing function from an unexisting player entity.
     
  6. Sophia

    Sophia Guest

    O-oh... Sometimes when I break my code or it's not working; it's the most obvious thing that I didn't see... Thanks.
     
  7. You're welcome
     
  8. Sophia

    Sophia Guest

    So I just realised that if you check my original coding post that I did use both () and (player) on this

    When I use this code -

    Code:
    local ITEM = Clockwork.item:New();
    ITEM.name = "Keys";
    ITEM.cost = 100;
    ITEM.model = "models/gibs/metal_gib4.mdl";
    ITEM.weight = 0.1;
    ITEM.access = "I";
    ITEM.category = "Citizen Keys";
    ITEM.business = true;
    ITEM.description = "A pair of jingling keys.";
    
    function ITEM:OnUse()
        ITEM:AddData("ID", -1);
        ITEM:AddQueryProxy("name", ITEM.GetName);
        return false;
    end;
     
    function ITEM:GetName()
        return "Keys - "..self:GetData("ID", -1);
    end;
     
    if (SERVER) then
        function ITEM:OnInstantiated(player)
            ITEM:SetData("ID", player:GetCharacterData("citizenid"));
        end;
    end;
    
    -- Called when a player drops the item.
    function ITEM:OnDrop(player, position) end;
    
    ITEM:Register();
    It doesn't let me have the item because it throws an error in the console; something to do with player having a nil value or something like that. Any ideas why?

    edit - I've also tried this -

    Code:
    local ITEM = Clockwork.item:New();
    ITEM.name = "Keys";
    ITEM.cost = 100;
    ITEM.model = "models/gibs/metal_gib4.mdl";
    ITEM.weight = 0.1;
    ITEM.access = "I";
    ITEM.category = "Citizen Keys";
    ITEM.business = true;
    ITEM.description = "A pair of jingling keys.";
    
    function ITEM:OnUse(player)
        ITEM:AddQueryProxy("name", "name");
        ITEM:AddData("name", -1);
        ITEM:SetData("name", player:GetCharacterData("citizenid"));
        return false;
    end;
    
    -- Called when a player drops the item.
    function ITEM:OnDrop(player, position) end;
    
    ITEM:Register();
    And other variations of it.
     
  9. NightAngel

    NightAngel Fuck off Lev

    Send the exact error, pretty please.
     
  10. Sophia

    Sophia Guest

    Code:
    [Clockwork] The 'CharGiveItem' command has failed to run.
    gamemodes/cwhl2rp/plugins/citizenkeylocks/plugin/items/sh_keys_lock.lua:23: 
    attempt to index local 'player' <a nil value>
     
  11. Kurochi already told you, the owner (player) isn't passed as an argument to OnInstantiated.

    Also I just realized you can make name tags for items with this. I'm too lazy, but someone should totally do it so we can have TF2RP (/joke).
     
  12. When Clockwork calls that function, it doesn't feed it any player at all.
    You're defining an argument in the function that is destined to stay nil.

    You have to think about an other way of doing this, an idea would be editing (via plugin possibly) a function inside the item that happens as soon as the item enters in a player's possession for the first time, and only for the first time.

    However, a better way would be to create an instance of the item, set its data to the player-who-must-receive-it's player ID, and then give him the instance, and you should get rid of the OnInstantiated completely.

    Example of what I mean, given "target" is our player.

    Code:
    local item = Clockwork.item:CreateInstance("keys");
    item:SetData("ID", target:GetCharacterData("citizenid"));
    target:GiveItem(item);
    Another shorter example:

    Code:
    local item = Clockwork.item:CreateInstance("keys", nil, {ID = target:GetCharacterData("citizenid")});
    target:GiveItem(item);
     
  13. Sophia

    Sophia Guest

    Yeah, I only just realised what he meant by that. I thought he meant that I simply hadn't put (player) on the function; not that the function itself simply cannot have a (player) argument.

    I'm still pretty amateur to code and I'm trying my best where I can though.
     
  14. Only if even the item base is set to have query proxies.
    Clockwork however does already make use of query proxies in most cases, except I've found some places where it doesn't.
    The way to use a query proxy is to never do itemTable.data, but itemTable("data").
    The item's metatable __call function is set to take the string you place in the brackets, and find the query proxy associated with it. If none is found, it just falls back to default itemTable.data

    You're doing fine.
    Atebite sounded sort of passive-aggressive there, but I'm sure he didn't mean it, he's one of the friendliest fucks out there.
     
  15. How about this?

    Code:
    for k,v in pairs(Clockwork.item:GetAll()) do
    	v:AddQueryProxy("name", "tagname")
    end
    And no, sounding passive-agressive wasn't intentional.
     
  16. That's actually a good idea there.
     

Previous Readers (Total: 0)