Welcome to
Mod The Sims
Online: 1359
Have an account? Sign in:
If you don't have an account, why not sign up now? It's free!
Other sites: SimsWiki
Reply  Replies: 2 (Who?), Viewed: 382 times.
Search this Thread
Old 24th Aug 2020, 4:38 PM Default[RESOLVED, added solution details]AddElement not working with custom hidden traits #1
Original Poster

Lab Assistant

Join Date: Jan 2012
Posts: 140
Thanks: 312 in 2 Posts
5 Achievements

View My Journal

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
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:]
            Trait trait = TraitManager.GetTraitFromDictionary(traitName);
            int iTraitsForBabiesAndToddlers = TraitManager.kTraitsForBabiesAndToddlers;
            int iTraitsForChildren = TraitManager.kTraitsForChildren;
            int iTraitsForTeens = TraitManager.kTraitsForTeens;
            int iTraitsForYoungAdultAndOlder = TraitManager.kTraitsForYoungAdultAndOlder;

                // 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)
                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.
Last edited by lizcandor : 24th Aug 2020 at 6:03 PM.
Old 26th Aug 2020, 4:31 PM #2
Virtual gardener

Join Date: Dec 2013
Posts: 3,095
Thanks: 3444 in 22 Posts
24 Achievements

Interesting stuffs! Thanks for sharing it :D*I'll use this for my own research as well!
Old 26th Nov 2020, 11:48 PM #3
Test Subject

Join Date: Aug 2014
Posts: 44

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.

Section jump:

Powered by MariaDB Some icons by http://dryicons.com.