AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Plugins (https://forums.alliedmods.net/forumdisplay.php?f=108)
-   -   [TF2] TF2Attributes (v1.7.2, 2022/09/18) (https://forums.alliedmods.net/showthread.php?t=210221)

FlaminSarge 03-08-2013 04:08

[TF2] TF2Attributes (v1.7.2, 2022/09/18)
 
39 Attachment(s)
⚠️For latest fixes/gamedata/etc., see GitHub.⚠️

Update - 2022/09/18: Pulled in all changes from @nosoop's fork.

The description line up there should be pretty self-explanatory.

This basically offers a bunch of natives for setting, getting, and modifying attributes on any entity that has m_AttributeList (GetEntSendPropOffs(entity, "m_AttributeList") > 0).

Available natives are all listed in the .inc file, so definitely go read that! It's redundant to copy all of them here, but just know that you're probably looking for:
Code:

TF2Attrib_SetByName
TF2Attrib_GetByName
TF2Attrib_RemoveByName
TF2Attrib_RemoveAll
TF2Attrib_ListDefIndices
TF2Attrib_GetStaticAttribs
TF2Attrib_GetSOCAttribs

There are other natives that exist to modify the attributes whose addresses are returned by the Get functions.

TF2Attrib_IsIntegerValue(iDefIndex) takes in an attribute definition index (as returned by TF2Attrib_GetDefIndex(Address:pAttrib)), and returns true if the attribute interpreted as an integer rather than as a float (items_game's definitions of this aren't accurate, so I hardcoded the native for the attributes that I found were stored as ints, such as "kill eater"). Let me know if the internal list needs updating.

Any attribute index that returns true for TF2Attrib_IsIntegerValue should be used with a Float: tag or view_as<float>() rather than float() for TF2Attrib_SetByName, TF2Attrib_SetValue, and TF2Attrib_SetInitialValue, as in the following code block, if you want the number to match up to what appears/happens in game.
Code:

int value = 20;
TF2Attrib_SetByName(entity, "kill eater", view_as<float>(value)); //sets strange kill count to 20, DO NOT USE 20.0
value = 30;
TF2Attrib_SetValue(pAttrib, view_as<float>(value)); //sets strange kill count to 30
int result = view_as<int>(TF2Attrib_GetValue(pAttrib));
PrintToChatAll("%d", result); //prints "30" to chat

One last note, attributes get wiped if the weapon/wearable entity gets removed, but attributes added to players will not get reset, and you will have to remove them on your own. However, set bonuses that you remove will get reapplied when the player regenerates.

Installation (SM1.10+):
1) Place tf2attributes.smx ("Get Plugin" for tf2attributes.sp) in the server's sourcemod plugins folder (normally "addons/sourcemod/plugins/").
2) Place tf2.attributes.txt in the server's sourcemod gamedata folder (normally "addons/sourcemod/gamedata/").
3) Write or install a plugin that uses these natives. If writing the plugin, you will need to #include <tf2attributes>, which means you will need to place tf2attributes.inc in the "include" folder of wherever you develop/compile SourceMod plugins.

tf2attributes_example.sp is an example plugin that offers several root-level admin commands to test out the various natives. You can ignore it if you want to.

Cvars:
tf2attributes_version -
do not touch.

This registers a plugin library (via RegPluginLibrary) named "tf2attributes".

Changelog (outdated, see GitHub below):
Code:

07/02/2015 - v1.2.1
* GunMettle gamedata
* Changed error behavior of the Get*Attribs natives (errors on call if gamedata failed to load, but does not SetFailState on plugin)

06/23/2015 - v1.2.0
* Added two new natives: TF2Attrib_GetStaticAttribs and TF2Attrib_GetSOCAttribs. See the .inc file for more info.
* Updated gamedata; TF2Attrib_ClearCache should work now
* Changed TF2Attrib_IsIntegerValue to a native
* Added TF2Attrib_IsReady native, which basically just says whether  all the gamedata loaded properly. I don't know what anybody would use it  for, but I remember being asked for it at some point.     

08/27/2013 - v1.1.1
*TF2Attrib_ClearCache now functional again. Use it if you add an attribute but don't see the effects immediately on the player (or whenever you want to see the effects). If it ever breaks, it'll just go back to failing silently.
*Added Set/RemoveByDefIndex.
*Fixed handling of "counts as assister is some kind of pet this update is going to be awesome".

07/20/2013 - v1.1.0
*TF2Attrib_ClearCache will silently fail (it will do absolutely nothing) while I figure out whether it still works and whether it works as intended.
*Added TF2Attrib_ListDefIndices, which will list the attributes on an entity.
*Removed TF2Attrib_Set/GetIsSetBonus and TF2Attrib_Set/GetInitialValue, since those aren't on attributes anymore.
*Fixed everything else
*Please note that because of the way attributes are now applied to weapons, you probably cannot remove the attributes that come with a weapon (same reason as TF2Items). You can only add and remove extra attributes (those applied by TF2Items and this plugin).

04/24/2013 - v1.0.1
*Added TF2Attrib_ClearCache native, must be called after calls to TF2Attrib_Set*(Address:pAttrib, *) in order for the values to update properly. Does not need to be called after calls to SetByName/Remove/RemoveAll. May need to be called on m_hOwnerEntity of weapons/wearables to properly update things like max health. Read the .inc for info.
*Added TF2Attrib_GetByDefIndex(iEntity, iDefIndex), gets the attribute address by the attribute's definition index (the arbitrary numbers we know and love). For instance, iDefIndex 150 is "turn to gold", so TF2Attrib_GetByDefIndex(iEntity, 150) would return the same thing as TF2Attrib_GetByName(iEntity, "turn to gold").
*All five files were updated. Added a new command to the example plugin.

03/08/2013 - v1.0.0
*Initial release

Planned Features:
TF2Attrib_GetByDefIndex(iEntity, iDefIndex), to get an attribute off an item by its defindex rather than by its name. It's nearly done, will be in the next update, I just wanted to push this first. Done.

Known Weirdness:
I would not recommend using TF2Attrib_SetDefIndex() at all. It doesn't behave as you'd expect it to, and while it does set the definition index of a given attribute, the game still treats it as the original attribute, though when it goes to look up the attribute by name it uses the 'new name' (as if it were actually the new attribute index). This messes up calls to GetByName and SetByName, so just... don't do it.
HOWEVER... if you call TF2Attrib_ClearCache() after SetDefIndex, the game will properly update that attribute to be the new attribute... I think...

... Did I forget anything?

asherkin gets all my cookies

Previous plugin views - 60760 - up to 2022/09/18


Download: GitHub
Source: GitHub

FlaminSarge 03-08-2013 04:19

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Reserved for things.

FlaminSarge 03-08-2013 07:54

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Also, heads up, I'm already writing a replacement for the tf2items_manager/tf2items config using this. If anybody else decides to undertake that task as well, let me know.

NameUser 03-08-2013 10:35

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Whoo-hoo! I've been waiting for this for a while. Time to develop!

Leonardo 03-08-2013 14:18

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Sweet.
But too late~

Powerlord 03-08-2013 14:41

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Just to be clear, which entities will this work on?

I'm thinking players, weapons, hats, miscs, and most action items (except Dueling Minigames, Secret Saxtons, and Piles o' Gifts).

FlaminSarge 03-08-2013 15:36

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Quote:

Originally Posted by FlaminSarge (Post 1908731)
This basically offers a bunch of natives for setting, getting, and modifying attributes on any entity that has m_AttributeList (GetEntSendPropOffs(entity, "m_AttributeList") > 0).

sm_dump_netprops netprops.txt, CTRL+F "m_AttributeList" (though I suppose the netclasses aren't very useful for finding the actual entity classnames, sm_dump_datamaps has you covered for classname - netclass conversion needs).

This modifies the actual entities, not the item server info, so items that pull from the item server (which I presume are gifts, duels, etc) wouldn't be affected, presumably.

Brb writing Randomizer MvM and UpgradeAnytime.

I'll also get to work on changing all the code in VSH to use this instead of TF2Items.

FlaminSarge 03-10-2013 07:36

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
I updated the .inc file on March 10th, 2013 to not cause tag mismatches whenever you tried to use the return of SetByName properly. Woops. Also updated the tagging on Remove/RemoveAll.

Bitl 03-10-2013 11:58

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Quote:

Originally Posted by FlaminSarge (Post 1909997)
I updated the .inc file on March 10th, 2013 to not cause tag mismatches whenever you tried to use the return of SetByName properly. Woops. Also updated the tagging on Remove/RemoveAll.

The funny thing is, the previous version did not cause tag mismatches, but the new version does.

EDIT: Actually, after some testing, it does it rarely.

FlaminSarge 03-11-2013 00:27

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
The thing was, "attrib" returned by Get and Set are Address: tagged, and I bet you're forgetting that tag. Initially, I had forgotten the tag on Set, and the bool: tags on Remove/RemoveAll. Make sure you're tagging the variables you store your returns in properly.

excalibur 03-11-2013 15:16

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Gonna play with this see what fun stuff can be done. Thnx as always for your work

FlaminSarge 03-15-2013 23:25

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Some of you may have noticed that certain attributes added to weapons that really take effect on the player, such as "max health additive bonus", don't work when the attribute is added to the weapon. The reason is that the player's attribute manager cache isn't cleared when the weapon's attributes are updated (Valve corrects for this in their own code, calling ClearCache on the player's attributes right after they make calls to add attributes to weapons), and it only updates when they regen/respawn.

So I'm adding TF2Attrib_ClearCache(entity), which would perform ClearCache on an entity's attributes.
Note, however, that Set/Remove/RemoveAll already call ClearCache on the entity that is being added to/removed from (internally), and the only issue is the entity's OWNER (a weapon's player, for instance) not being updated. Thus, should I even add this native, or should I simply clear the attribute cache of m_hOwnerEntity of whatever entity is being used in Set/Remove/RemoveAll?

Long story short, we don't NEED TF2Attrib_ClearCache(entity), and I can simply just call ClearCache on the m_hOwnerEntity of any weapons/wearables passed into Set/Remove/RemoveAll. Should I add the native anyways?

Bitl 03-15-2013 23:45

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Quote:

Originally Posted by FlaminSarge (Post 1913525)
Some of you may have noticed that certain attributes added to weapons that really take effect on the player, such as "max health additive bonus", don't work when the attribute is added to the weapon. The reason is that the player's attribute manager cache isn't cleared when the weapon's attributes are updated (Valve corrects for this in their own code, calling ClearCache on the player's attributes right after they make calls to add attributes to weapons), and it only updates when they regen/respawn.

So I'm adding TF2Attrib_ClearCache(entity), which would perform ClearCache on an entity's attributes.
Note, however, that Set/Remove/RemoveAll already call ClearCache on the entity that is being added to/removed from (internally), and the only issue is the entity's OWNER (a weapon's player, for instance) not being updated. Thus, should I even add this native, or should I simply clear the attribute cache of m_hOwnerEntity of whatever entity is being used in Set/Remove/RemoveAll?

Long story short, we don't NEED TF2Attrib_ClearCache(entity), and I can simply just call ClearCache on the m_hOwnerEntity of any weapons/wearables passed into Set/Remove/RemoveAll. Should I add the native anyways?

I don't see why not, it would be useful to most people to minimize their code.

FlaminSarge 03-16-2013 03:53

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
I'm not entirely sure if I'm understanding what your reply means, but it seems to me like the first half contradicts the second half: "I don't see why not" = "I don't see why you wouldn't add the native"; "it would be useful to most people to minimize their code" = not having the native, but making the automatic owner cache change a built-in part of the plugin would be useful since then plugin authors don't have to do it in their own code.

Which of the two do you mean?

Bitl 03-16-2013 21:51

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Quote:

Originally Posted by FlaminSarge (Post 1913581)
I'm not entirely sure if I'm understanding what your reply means, but it seems to me like the first half contradicts the second half: "I don't see why not" = "I don't see why you wouldn't add the native"; "it would be useful to most people to minimize their code" = not having the native, but making the automatic owner cache change a built-in part of the plugin would be useful since then plugin authors don't have to do it in their own code.

Which of the two do you mean?

I'm just saying that it's a good idea to put it in.

FlaminSarge 03-17-2013 02:11

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
The next update won't be adding the TF2Attrib_ClearCache native, as it's probably just better to call ClearCache on an entity's m_hOwnerEntity whenever add/remove/removeall are called, just to make sure attributes are properly updated each time they're given/removed. As such, it'll just do that call internally (with proper checks) instead of forcing plugin devs to call it whenever they need the attribute manager cache updated.

FlaminSarge 03-21-2013 22:35

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Scratch that last bit, ClearCache is sometimes necessary after editing an attribute's value via TF2Attrib_SetValue(), etc., so that native's going to exist.

Side note, DO NOT USE THE RETURN OF TF2Attrib_SetByName(). It's not the address of the created attribute. I botched that. It's some arbitrary other address that seems to be related to the entity or client who got the attribute. It IS, however, nonzero if the attribute creation was successful, so the next update will change that to bool:TF2Attrib_SetByName(). Hopefully I'm not breaking/misaligning anything by using SetReturnInfo when there isn't supposed to be a return (SetOrAddAttributeByName returns void, apparently).

To get the address of an attribute you just created, you'll have to call Get right after Set and hope it grabs the address.

Powerlord 03-22-2013 15:10

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Should I assume there's no function to set attributes by attribute index rather than by name?

Freak Fortress 2 operates on these not just for boss attributes (and there are hundreds of bosses out now) but also for functions that give new weapons to bosses.

Bitl 03-22-2013 23:28

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Quote:

Originally Posted by Powerlord (Post 1917736)
Should I assume there's no function to set attributes by attribute index rather than by name?

Freak Fortress 2 operates on these not just for boss attributes (and there are hundreds of bosses out now) but also for functions that give new weapons to bosses.

Yeah, it would be a good thing to add.

Then when I was testing, I did not realize that I needed to put the name of the attribute, not the index.

FlaminSarge 03-25-2013 03:01

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Nothing for *cleanly* adding attributes by index rather than name, unfortunately. You could probably wrap something around TF2ItemsInfo for that.

Or I could try constructing CEconItemAttribute in pawn. But apparently that's messy and bad and don't do it.

However, since each attribute index maps to a name, it shouldn't be an issue and should actually make your code more readable than arbitrary numbers would, were you to switch to using this in place of attribs by index.

Wander 03-30-2013 17:29

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Just started using this in my mod, but I ran into a small problem

At some point I want to change the players speed, so I tried this:
TF2Attrib_SetByName(client,"move speed penalty",0.5);

However, it only takes effect after the client switches weapon
I've also tried some other tf2 attributes that affect speed, and they seem to have the same problem

Is there a way to fix it, or should I somehow change the players active weapon slot to something else and back?
This might be happening at a point where the player only has a melee weapon, so I don't know if it'd work then

MasterOfTheXP 03-30-2013 20:15

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Try TF2_AddCondition(client, TFCond_SpeedBuffAlly, 0.3); after you give the attribute. It's what VSH does to re-calculate move speed changes.

Wander 03-30-2013 23:30

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Quote:

Originally Posted by MasterOfTheXP (Post 1923480)
Try TF2_AddCondition(client, TFCond_SpeedBuffAlly, 0.3); after you give the attribute. It's what VSH does to re-calculate move speed changes.

Thanks :) that seems to do the trick (though I changed 0.3 to 0.001 to hide the onscreen indicator)

MasterOfTheXP 03-30-2013 23:48

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
Oh, is 0.3 too much? Whoops. I knew there was a duration you could use that wouldn't cause any visual effects, but I was pretty sure that 0.3 wouldn't show anything. My bad.

FlaminSarge 03-31-2013 07:49

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
The upcoming ClearCache native may or may not also do the trick, but VSH does not in fact use 0.3 (it does for Medic healing speed but that's moot).
Code:

        TF2_AddCondition(client, TFCond_SpeedBuffAlly, 0.01);    //recalc their speed
I have this line copypasted in so many plugins...


... I really should just push that ClearCache change.

Dr. McKay 04-20-2013 22:36

Re: [TF2] TF2Attributes (v1.0.0, 03/08/2013)
 
<3

FlaminSarge 04-24-2013 03:16

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
Updated on 04/24/2013 for ClearCache and GetByDefIndex.

Please read the notes next to ClearCache in the .inc file, as it tells you when you may/may not need to use it.

Unreal1 04-27-2013 18:03

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
I was wondering if it would be possible to make it so that you multiply all the attributes of a weapon by 2. I was thinking of using a large loop but I think that would be too intense. Got any other methods?

Powerlord 04-29-2013 15:40

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
With SteamPipe due tomorrow and me just releasing a plugin that uses TF2Attributes... what's the status on SteamPipe gamedata?

FlaminSarge 04-30-2013 08:28

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
My sig tells all.

I'll look it up some time tomorrow.

Not sure about the x2everything thing.

Powerlord 04-30-2013 11:28

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
Quote:

Originally Posted by FlaminSarge (Post 1942825)
My sig tells all.

I'll look it up some time tomorrow.

Unless Valve throws an additional update in there, it appears that the Linux versions retain their same signaturess. I'm not sure how to check the Windows versions, though.

Ravu al Hemio 04-30-2013 12:06

[TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
I checked with the gdc tools. Linux/OS X locations are given as symbol names, which gdc tries to find in the symbol table. Windows locations are given as byte patterns, which gdc tries to find in the code section of the DLL, making sure it finds each only once.

Both seem to be unchanged for the methods used by TF2Attributes.

FlaminSarge 05-01-2013 06:14

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
Nothing broke with this with SteamPipe on Windows, so we're good to go.

realwx 05-02-2013 23:39

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
Sort of still stuck in a circle of figuring out why item attributes are applying themselves to the "Action" slot despite filtering out item ID's numbers by checking against the string trie. It's currently taking Melee (or, if available, PDA/PDA2 onto Action.) Even if I remove the melee slot before TF2Attributes apply, then it will just apply the secondary slot's attributes onto the Action slot. (I did try replacing "decl" with "new" in several places, Sarge, but it only made a difference on the first round, not any subsequent ones)

Tested by viewing stats in the MVM maps. Any clues on whether I'm doing this all wrong?

Code:

public TF2Items_OnGiveNamedItem_Post(client, String:classname[], itemDefinitionIndex, itemLevel, itemQuality, entityIndex)
{
    new applyOnSpawn = GetConVarInt(cvarApplyOnSpawn);
   
    if(enabled && ( (applyOnSpawn == 1 && !IsFakeClient(client)) || applyOnSpawn == 2) )
    {
        decl String:tmpID[16];
        decl String:strAttrib[64];
        decl String:fullAttr[256];
       
        Format(tmpID, sizeof(tmpID), "%d__%s", itemDefinitionIndex, selectedMod);

        if (!GetTrieString(g_hItemInfoTrie, tmpID, fullAttr, sizeof(fullAttr)))
        {
            Format(tmpID, sizeof(tmpID), "%d__default", itemDefinitionIndex);
            if(!GetTrieString(g_hItemInfoTrie, tmpID, fullAttr, sizeof(fullAttr)))
            {
                PrintToConsole(client, "Could not load value from %s.", tmpID); //debug
                return;
            }
        }
       
        TF2Attrib_RemoveAll(entityIndex);

        decl String:attrArray[MAX_ATTRIBUTES][32];
        new num_attribs = ExplodeString(fullAttr, " ; ", attrArray, MAX_ATTRIBUTES, 32);

        for (new i=0; i < num_attribs+1; i += 2)
        {
            TF2II_GetAttribName(StringToInt(attrArray[i]), strAttrib, sizeof(strAttrib));

            if (TF2Attrib_IsIntegerValue(StringToInt(attrArray[i+1])))
            {
                new value = StringToInt(attrArray[i+1]);
                TF2Attrib_SetByName(entityIndex, strAttrib, Float:value);
            }
            else
            {
                TF2Attrib_SetByName(entityIndex, strAttrib, StringToFloat(attrArray[i+1]));
            }
        }
        TF2_AddCondition(client, TFCond_SpeedBuffAlly, 0.01);
    }
}


FlaminSarge 05-04-2013 07:39

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
Have you tried flat-out rejecting certain indices or classnames instead of relying on GetTrieString to reject them?

realwx 05-05-2013 03:30

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
Oh man. I just... wow. I'm an idiot. I did try that days ago, but when I revisited it just now, I had a && where a || should've been. (Plus, I fixed something odd with ExplodeString returning ghost attributes, so the other problem is fixed.)

Thank you so much Sarge, and really sorry I've been bugging you :P

Oshizu 05-05-2013 06:42

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
Hello i've got a small problem with plugin. Halloween Attributes doesn't seem to work even with 'tf_forced_holiday'

MasterOfTheXP 05-05-2013 06:44

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
Valve intentionally broke those, I believe. (What with Halloween being over, and all...still though :|)

CoolJosh3k 05-09-2013 12:50

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
Ya, Valve broke both Halloween and MvM stuff just for the creative community :(

Should be a work around in the future, somehow.

thesupremecommander 05-12-2013 10:18

Re: [TF2] TF2Attributes (v1.0.1, 04/24/2013)
 
Can a native to get all of the attributes on an entity be implemented (as opposed to having to blindly guess whether an entity even has an attribute)?


All times are GMT -4. The time now is 14:21.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.