Replies: 5 (Who?), Viewed: 207 times.
Lab Assistant
Original Poster
#1 Old 24th Sep 2021 at 7:34 PM
Default Adding a CASP through code
So, I'm currently thinking about how to code a interaction that will add a CASP indentified by a string that will point to the FNV64 id of the CASP in question. The goal is to create two strings, one for the actor and another for the target in the interaction, and associate each with one id. The CASPs would then be added to the actor and target sims. How could I do this?
Advertisement
Virtual gardener
staff: administrator
#2 Old 27th Sep 2021 at 11:15 AM Last edited by Lyralei : 27th Sep 2021 at 11:15 AM. Reason: Woops screwed up the bbcode
Hrm sounds complicated but doable. I actually did something similair for the sewing machine (where gifted items would later be 'worn' by the target).

What I did:

Code:
        public static void SetUpAlarmForGiftedItems()
        {
             // Get CASP resource file from resource key:  (Check whether the type and group is correct for you!!)
             CASPart outfit =  new CASPart(new ResourceKey(ResourceKey.FromString("CLOTHING NAME HERE"), 0x0333406C, 0x7354C1FC));
            // I know I don't have a sim parameter or anything set, since I used an alarm, but basically, compare the age with the current target and the gender 
            if (outfit.Age == sim.Simdescription.mSimDescription.Age && outfit.Gender == sim.Simdescription.mSimDescription.Gender)
            {
                // This works more on an anonymous level, otherwise the target wasn't changing into their clothing. You may want to research the dresser, change outfit function if this isn't really working for you 
                ApplyGiftedSewingsToOutfits(outfit , sim);
                OutfitCategories currentOutfitCategory = sim.CurrentOutfitCategory;
                alarmTarget.RefreshCurrentOutfit(true);
                alarmTarget.SwitchToOutfitWithoutSpin(sim.CurrentOutfitCategory);
            }
        }


        // ClothingCategory is a bit version of the clothing categories (Everyday, formal, etc)
        public static uint[] ClothingCategory =
        {
            2,
            4,
            8,
            16,
            32,
            256,
        };
        // Modified but almost identical to the face paint code
        public static void ApplyGiftedSewingsToOutfits(CASPart casPart, Sim desc)
        {
            List<uint> possibleOutfits = new List<uint>();

            foreach (uint cat in ClothingCategory)
            {
                if ((casPart.CategoryFlags & cat) == cat)
                {
                    //print("Match made! CAT: " + cat.ToString());
                    possibleOutfits.Add(cat);
                }
            }
            // I did this since my top and skirts were possible for almost all categories. if you just want to apply it to one category, then simply remove the switch statement, the 'getrandomobject' function and set 'OutfitCategories cat = OutfitCategories.Everyday' or whatever category it needs to be 
            if (possibleOutfits.Count > 0)
            {
                uint randomed = RandomUtil.GetRandomObjectFromList(possibleOutfits);

                OutfitCategories cat = OutfitCategories.None;
                switch (randomed)
                {
                    case 2:
                        {
                            cat = OutfitCategories.Everyday;
                            break;
                        }
                    case 4:
                        {
                            cat = OutfitCategories.Formalwear;
                            break;
                        }
                    case 8:
                        {
                            cat = OutfitCategories.Sleepwear;
                            break;
                        }
                    case 16:
                        {
                            cat = OutfitCategories.Swimwear;
                            break;
                        }
                    case 32:
                        {
                            cat = OutfitCategories.Athletic;
                            break;
                        }
                    case 256:
                        {
                            cat = OutfitCategories.Makeover;
                            break;
                        }
                    default:
                        {
                            cat = OutfitCategories.Everyday;
                            break;
                        }
                }

                SimBuilder simBuilder = new SimBuilder();
                simBuilder.UseCompression = true; // funnily enough doesn't matter, since the game will always set a false 'UseCompression' back to true :p
                simBuilder.Age = desc.mSimDescription.Age;
                SimOutfit outfit = desc.mSimDescription.GetOutfit(cat, 0);
                OutfitUtils.SetOutfit(simBuilder, outfit, desc.mSimDescription);
                OutfitUtils.SetAutomaticModifiers(simBuilder);
                simBuilder.RemoveParts(casPart.BodyType);
                OutfitUtils.AddPartAndPreset(simBuilder, casPart, false);
                ResourceKey key = simBuilder.CacheOutfit(string.Format("NAMEITWHATEVER_{0}_{1}_{2}", simBuilder.Age, desc.FirstName, cat));
                SimOutfit outfit2;
                if (OutfitUtils.TryGenerateSimOutfit(key, out outfit2))
                {
                    desc.mSimDescription.RemoveOutfit(cat, 0, true);
                    desc.mSimDescription.AddOutfit(outfit2, cat, 0);
                }
                simBuilder.Dispose();
            }
        }


And voila! You got yourself some code that does what you want! of course if you're curious about some part of the code, just lemme know! Since this stuff was pretty confusing for me too when I worked with it :p
Lab Assistant
Original Poster
#3 Old 28th Sep 2021 at 9:35 PM
Thanks for your help again, @Lyralei. So, I've got some questions. This is the "finished" code embedded into the Challenge to Duel interaction:

Code:
CASPart outfit =  new CASPart(new ResourceKey(ResourceKey.FromString("Weapon Name Actor", 0x0333406C, 0x7354C1FC));
		if (outfit.Age == Actor.Simdescription.mSimDescription.Age && outfit.Gender == Actor.Simdescription.mSimDescription.Gender)
		{
			ApplyWeaponsToOutfits(outfit , Actor);
            OutfitCategories currentOutfitCategory = Actor.CurrentOutfitCategory;
            Actor.RefreshCurrentOutfit(true);
            Actor.SwitchToOutfitWithoutSpin(Actor.CurrentOutfitCategory);
		}
		CASPart outfit2 =  new CASPart(new ResourceKey(ResourceKey.FromString("Weapon Name Target"), 0x0333406C, 0x7354C1FC));
		if (outfit2.Age == Target.Simdescription.mSimDescription.Age && outfit2.Gender == Target.Simdescription.mSimDescription.Gender)
		{
			ApplyWeaponsToOutfits(outfit2 , Target);
            OutfitCategories currentOutfitCategory = Target.CurrentOutfitCategory;
            Target.RefreshCurrentOutfit(true);
            Target.SwitchToOutfitWithoutSpin(Target.CurrentOutfitCategory);
		}


So, I'm both wondering how to create interactions that would allow the player to change both strings (Represented by "Weapon Name Targe" and "Weapon Name Actor") and how exactly I could make the Actor/Target relationship be understood by the code, allowing both sims to get assigned their correct CASPs.
Virtual gardener
staff: administrator
#4 Old 30th Sep 2021 at 10:45 AM
Hrm you mean as that the player has to parse in the text?

Because then I'd go as far as introducing a dialogue Initially, what you can then do is make a picker dialogue that has some 'premade' CAS items they can choose from as it might be more confusing for players to find the necessary string? (So what the item is called etc)

Regarding figuring out the actor/target, you can always compare against their SimDescriptionId. So:

Code:
if(actor.SimDescription.mSimDescriptionId == target.SimDescription.mSimDescriptionId)
{
       // If actor is the same as target, do nothing? Or something! up to you!
}
Forum Resident
#5 Old 30th Sep 2021 at 10:49 AM
Not sure if helpful but you may want to take a look at Arsil's Sunglasses mod which is the same idea of allowing a Sim to wear a CASP (in this case, sunglasses) from an object in their inventory. His code is linked to a specific CASP item (aviator sunglasses).
Back to top