Hi there! You are currently browsing as a guest. Why not create an account? Then you get less ads, can thank creators, post feedback, keep a list of your favourites, and more!
Field Researcher
Original Poster
#1 Old 24th Aug 2020 at 3:38 PM Last edited by lizcandor : 24th Aug 2020 at 5:03 PM.
Default [RESOLVED, added solution details]AddElement not working with custom hidden traits
Has anyone had any luck with adding custom hidden traits programmatically, not just adding them manually in-game? I've created them using Arsil's mod and I can add them with MC, but using
Code:
simDescription.TraitManager.AddHiddenElement((TraitNames)hex-code) 
or
Code:
simDescription.TraitManager.AddElement((TraitNames)hex-code, hidden: true, doFail: false) 
(which should be the same as each other) in a script just doesn't work for some reason (returns false and fails to add the trait). AddElement does work for custom non-hidden traits (including one of the exact same traits I'm having trouble with now, before I changed its category in my traits XML to "none" to make it hidden) and default hidden traits (as a test I added a line right below the one for adding my trait that should add Pizza Appreciator, which worked). So it seems like the issue is specifically with the combination of AddElement and a hidden trait not in the TraitNames enum, not a problem with AddElement, hidden traits, or custom traits in general or with the surrounding code.

I've seen a few older posts by Armise (the only other person who I know for sure has tried this ) about making a mod with custom hidden traits using Arsil's mod, just like I am - it's been a couple years now, but @Armise I'd be interested to know if you also ran into this? No pressure though, I'm just wondering if the problem is something I specifically did or something about custom hidden traits made like this in general - in which case I may make my hidden traits into reward traits instead.

[EDIT: Wait a minute, right after writing this it occurred to me that what I should do is check how MC adds traits, since I know for certain that MC 1) is a script mod and 2) has a method that is able to add these traits to sims. Sometimes the process of asking a question stimulates your mind to find your own answer, I guess. I'll try imitating MC (which also uses AddHiddenElement, but appears to be more careful about getting the trait guid than I am) and update if that works.]

[EDIT 2: Okay, adding a new method that runs this code (borrowed almost verbatim from MC's ChangeTrait class), and using that method to add the traits instead of just AddHiddenElement by itself worked:]
Code:
            Trait trait = TraitManager.GetTraitFromDictionary(traitName);
            int iTraitsForBabiesAndToddlers = TraitManager.kTraitsForBabiesAndToddlers;
            int iTraitsForChildren = TraitManager.kTraitsForChildren;
            int iTraitsForTeens = TraitManager.kTraitsForTeens;
            int iTraitsForYoungAdultAndOlder = TraitManager.kTraitsForYoungAdultAndOlder;

            try
            {
                // Don't use MaxValue as EA adds to this number for [[University]] purposes
                TraitManager.kTraitsForBabiesAndToddlers = 10000000;
                TraitManager.kTraitsForChildren = 10000000;
                TraitManager.kTraitsForTeens = 10000000;
                TraitManager.kTraitsForYoungAdultAndOlder = 10000000;

                if (trait.IsHidden)
                {
                    sim.SimDescription.TraitManager.AddHiddenElement(traitName);
                }
                else
                {
                    sim.SimDescription.AddTrait(trait);
                }
            }
            finally
            {
                TraitManager.kTraitsForBabiesAndToddlers = iTraitsForBabiesAndToddlers;
                TraitManager.kTraitsForChildren = iTraitsForChildren;
                TraitManager.kTraitsForTeens = iTraitsForTeens;
                TraitManager.kTraitsForYoungAdultAndOlder = iTraitsForYoungAdultAndOlder;
            }

From also looking into AddElement in ILSpy, I figured out that what this means is adding the trait failed before because the sims I was using it on already had their trait slots maxed out. I didn't think that would be a problem for a hidden trait, but apparently it is, at least as long as that trait has age species availability flags specified - if I remove the age/species availability restrictions from the traits XML entries for the traits in question, just using AddHiddenElement instead of the code above works fine. Conclusion: trait slot limitations do in fact apply to hidden traits; so in my mod I will just leave the age/species restrictions off, make sure the code that applies the trait doesn't run on non-human or age-inappropriate sims, and use AddHiddenElement.
Advertisement
Virtual gardener
staff: administrator
#2 Old 26th Aug 2020 at 3:31 PM
Interesting stuffs! Thanks for sharing it :D I'll use this for my own research as well!
Test Subject
#3 Old 26th Nov 2020 at 10:48 PM
Yeah so I don't log in here often, so sorry for the late reply.
No, I don't remember running in problems with adding traits, but I had another model to copy : the other occult classes, which all apply the corresponding hidden trait to the sim in each specific way. I don't remember the exact method call of course.
Field Researcher
#4 Old 16th Nov 2022 at 4:54 AM
Thank you so much lizcandor. You are a legit boss. I was looking into adding hidden traits via an immediate interactions and thanks to your findings I was able to pull it off.
Back to top