Adding Attributes by script

Shadowing

Member
I'm trying to do this below

Code:
        m_AttributeManager.Attributes = new Attribute[4];
        Attribute[] Attributes = m_AttributeManager.Attributes;

        m_AttributeManager.Attributes[0].Name = "Health";
        m_AttributeManager.Attributes[1].Name = "Shield";
        m_AttributeManager.Attributes[2].Name = "Stamina";
        m_AttributeManager.Attributes[3].Name = "Provisions";

        m_CharacterHealth.ShieldAttributeName = m_AttributeManager.Attributes[1].Name;

        foreach(Attribute Att in Attributes) {
            Att.AutoUpdateValueType = Attribute.AutoUpdateValue.Increase;
        }

This throws a null exception though.
Code:
m_AttributeManager.Attributes[0]
 
Was able to get this to work.


Code:
        Attribute[] m_Attributes = m_AttributeManager.Attributes = new Attribute[] {
            new Attribute("Health", 100),
            new Attribute("Shield", 0),
            new Attribute("Stamina", 0),
            new Attribute("Provisions", 0)
            };
          
        foreach(Attribute Att in m_Attributes) {
            Att.AutoUpdateValueType = Attribute.AutoUpdateValue.Increase;
        }

        m_Health = m_Attributes[0];
        m_Shield = m_Attributes[1];
        m_Stamina = m_Attributes[2];
        m_Provisions = m_Attributes[3];

But I"m having issues. Getting hit with this error when I try to do

Code:
m_Health.Value =100;


EventHandler.GetActionList error: target object cannot be null.
UnityEngine.Debug:LogError(Object)
Opsive.UltimateCharacterController.Events.EventHandler:GetActionList(Object, String) (at Assets/Opsive/UltimateCharacterController/Scripts/Events/EventHandler.cs:124)
Opsive.UltimateCharacterController.Events.EventHandler:ExecuteEvent(Object, String, Attribute) (at Assets/Opsive/UltimateCharacterController/Scripts/Events/EventHandler.cs:386)
Opsive.UltimateCharacterController.Traits.Attribute:set_Value(Single) (at Assets/Opsive/UltimateCharacterController/Scripts/Traits/AttributeManager.cs:56)
PlayerCharacterSettings:SetPlayerStats() (at Assets/Scripts/PlayerCharacterSettings.cs:359)
<_PlayerSetup>d__15:MoveNext() (at Assets/Scripts/PlayerCharacterSettings.cs:282)
MEC.Timing:Update() (at Assets/Plugins/Trinary Software/Timing.cs:466)



The error doesn't happen when I don't try to create my own Attributes by script. So i'm guessing this isn't possible.
 
if i change Awake to Start on AttributeManager it fixes my issue.
I'll do that for now until you give me some ideas


Code:
        private void Start()
        {
            for (int i = 0; i < m_Attributes.Length; ++i) {
                m_NameAttributeMap.Add(m_Attributes[i].Name, m_Attributes[i]);
                m_Attributes[i].Initialize(gameObject);
            }
        }
 
Last edited:
The Attribute Manager isn't designed to be able to add attributes at runtime. I'll add this to the next update. It's likely most of the way there but the dictionary doesn't get updated with any newly added attributes.
 
You might be able to create X number in the editor and then deal with them at runtime. Temporary hack until the next update...
 
Yes, I believe the AttributeManager has since been updated - you can directly access AttributeManager.Attributes (the setter which automatically call Initialize when you do). I tested Shadowing's script from the 2nd post and it worked fine.
 
I can confirm this aswell. Here is an example of interacting and deducting currency (attribute named "currency") at runtime!
 

Attachments

  • unknown-2.png
    unknown-2.png
    22.3 KB · Views: 10
Top