#1
24th Aug 2020 at 3:38 PM
Last edited by lizcandor : 24th Aug 2020 at
5:03 PM.
Posts: 313
Thanks: 2021 in 13 Posts
10 Achievements
View My Journal
[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.