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!
Quick Reply
Search this Thread
Field Researcher
Original Poster
#1 Old 19th Mar 2023 at 5:21 AM
Default Failing to add a Sim's SimDescriptionID to a dictionary.
Regrettably, I am having a hard time working with a dictionary. I will do my best to explain. So I have an alarm that triggers a function. When the alarm is triggered, it is suppose to check which sims are in the dictionary. If the sims are in the dictionary, then the function will trigger for those sims. Otherwise if the sim is not on the dictionary it will not trigger. I will now share my code and explain to the best of my ability what I was trying to do. //In the InRabbitHole function, the Sim's SimDescriptionID should be added to the Dictionary via the anInstanceofMyClass.AddArmsDeal(Actor.SimDescription.SimDescriptionId, true); function anytime a sim performs the custom rabbithole interaction.
Code:
public override bool InRabbitHole()
        {
            StartStages();
            BeginCommodityUpdates();
            bool flag = DoLoop(ExitReason.Default | ExitReason.Replan);

            EndCommodityUpdates(flag);

            var anInstanceofMyClass = new SavedData();

            if (flag)
            {
                anInstanceofMyClass.AddArmsDeal(Actor.SimDescription.SimDescriptionId, true);
                Helpers.ArmsDealPaymentAlarm(Actor);
                Helpers.ArmsDealReceivinAlarm(Actor);

            }
            return flag;
        }
//This is my AddArmsDeal function that the code above references. Which I believe it should add the SimDescriptionID of the sim to the Dictionary.
Code:
public void AddArmsDeal(ulong SimID, bool ArmsDealSet)
        {
            
            ArmsDeal[SimID] = ArmsDealSet;
            
        }
//Here are my two alarms that are also added when the sim performs the custom RabitHole interaction.
Code:
public static void ArmsDealPaymentAlarm(Sim sim)
        {
            AlarmManager.Global.AddAlarmDay(12f, DaysOfTheWeek.Sunday, AlarmCallBack ,"ArmsDeal Alarm", AlarmType.AlwaysPersisted, null);
        }

public static void ArmsDealReceivinAlarm(Sim sim)
        {
            AlarmManager.Global.AddAlarmDay(12f, DaysOfTheWeek.All, AlarmCallBack2, "ArmsDealReceiving Alarm", AlarmType.AlwaysPersisted, null);
        }
//When the Alarms trigger, they both check whether each individual sim in a active household is in the dictionary. Before, I had it as a condition where the code would only run if the sim was in the dictionary, but then the code was not triggering. So, I changed it to a function that would check whether any of the sims was in the dictionary.
Code:
public static void AlarmCallBack()
        {
            var anInstanceofMyClass = new SavedData();
            foreach (var CurrentSim in Sim.ActiveActor.Household.Sims)
            {

                
                {
                    CurrentSim.ModifyFunds(-2500);
                    anInstanceofMyClass.HasArmsDeal(CurrentSim.SimDescription, true);
                }
            }

        }

public static void AlarmCallBack2()
        {

            var anInstanceofMyClass = new SavedData();
            foreach (var CurrentSim in Sim.ActiveActor.Household.Sims)
            {
               
                {
                    LoopForGunItems2(CurrentSim);
                    anInstanceofMyClass.HasArmsDeal(CurrentSim.SimDescription, true);
                }
            }
        }
//This is the code that checks whether the Sim's SimDescriptionID is in the dictionary. If it is then it should shoot a notification saying Key Found. Otherwise it will return Key Not Found.
Code:
public bool HasArmsDeal(ulong SimID, bool ArmsDealSet)
        {
            if (ArmsDeal.ContainsKey(SimID) && ArmsDeal[SimID] == ArmsDealSet)
            {
                StyledNotification.Show(new StyledNotification.Format("Key Found", StyledNotification.NotificationStyle.kDebugAlert));
                return true;
            }
            else
            {
                StyledNotification.Show(new StyledNotification.Format("Key Not Found", StyledNotification.NotificationStyle.kDebugAlert));
                return false;
            }
        }

        public bool HasArmsDeal(SimDescription SD1, bool ArmsDealSet)
        {
            return HasArmsDeal(SD1.SimDescriptionId, true);
        }
    }
However, unfortunately, it seems like the sim's SimDescriptionID is not being added to the dictionary, because regarldess of whether the sim performs the custom Rabbithole Interaction. The result has always been Key Not Found. At this moment I am honestly at a loss. It's possible that I either performed a logical error somewhere that I have yet to find or I misinterpreted what a line of code actually does. Any help would be appreciated. Thanks.
Advertisement
Field Researcher
#2 Old 19th Mar 2023 at 11:30 AM
I am going to link you the dictionary description I sent you on Discord again. Read it!
Then you will se that you are not using the code to add a new pair to the dictionary but the code to use if you update an already existing entry.
https://www.tutorialsteacher.com/cs...harp-dictionary
Field Researcher
Original Poster
#3 Old 19th Mar 2023 at 9:22 PM
Quote: Originally posted by KittyTheSnowcat
I am going to link you the dictionary description I sent you on Discord again. Read it! Then you will se that you are not using the code to add a new pair to the dictionary but the code to use if you update an already existing entry. https://www.tutorialsteacher.com/cs...harp-dictionary
I went ahead and changed my AddArmsDeal function to include ArmsDeal.Add(SimID, ArmsDealSet);
Code:
public void AddArmsDeal(ulong SimID, bool ArmsDealSet)
        {
            ArmsDeal.Add(SimID, ArmsDealSet);
            
        }
However, the end result is Key Not found as well. Did I miss something? I believe this is what you were referring to. I'll come back after a break and reread the link to see if I did indeed miss something. Thanks for your input
Test Subject
#4 Old 21st Mar 2023 at 1:41 PM Last edited by BrianMoore : 27th Mar 2023 at 8:15 AM.
Thanks for the link, you made my day. If your children are struggling in maths and you don't know what to do, I recommend this website: https://parccgames.com/ here you may find a variety of games that can assist your youngsters develop an interest in maths.
Field Researcher
#5 Old 21st Mar 2023 at 6:30 PM
You are saving the dictionary in a SavedData, but you did not add your definition of that. Also you make a new one everytime you want to add it. Does it contain a static dictionary? Could you share the code?
Field Researcher
Original Poster
#6 Old 22nd Mar 2023 at 12:59 AM
Quote: Originally posted by KittyTheSnowcat
You are saving the dictionary in a SavedData, but you did not add your definition of that. Also you make a new one everytime you want to add it. Does it contain a static dictionary? Could you share the code?


Hello @KittyTheSnowcat, here is my code. The dictionary should be on MonocoDoll.ArmsDealingMod.Common.SavedData. And then the MonocoDoll.ArmsDealingMod.RabbitHoles.PoliceStationDailySupplyForGunsInteraction.InRabbitHole is the method which tries to add the key to the sim whenever they finish the rabbithole interaction. Please let me know if you need anything else. Again thanks.
Attached files:
File Type: zip  MonocoDoll ArmsDealingProject.zip (7.2 KB, 4 downloads)
Field Researcher
#7 Old 22nd Mar 2023 at 6:57 PM
Everytime you save to that dictionary you make a new SavedData with a new dictionary. And then when you want to read it, you make a new SavedData with a new empty dictionary and read from that.
Make your SavedData class static. And then instead of creating a new instance each time, call the one and only Saved Data with its Dictionary inside.

public static class SavedData
{
[Persistable]
public Dictionary<ulong, bool> ArmsDeal;



public void AddArmsDeal(ulong SimID, bool ArmsDealSet)
{
if (ArmsDeal == null)
{
ArmsDeal = new Dictionary<ulong, bool>();
}
if (!ArmsDeal.ContainsKey(SimID)){
ArmsDeal.Add(SimID, ArmsDealSet);
}
else {
ArmsDeal[SimID] = ArmsDealSet;

}
Field Researcher
Original Poster
#8 Old 23rd Mar 2023 at 3:29 AM
Thank you so much! Making the SavedData static made it work. It gave me errors on other functions of the code. However, after researching the errors and making the adjustments in order to fix the errors. It all made it work in the end. Again thank you so much. I'm not the best at particularly anything here, but if there is anything I can do to repay you. Then please let me know. Have a great rest of your day.
Back to top