Coding Help Exception while trying to add a Buff to the game.

SomeoneEls

Tiller
The idea is that a buff will be added to the game periodically to the bpm of a song. Here is the code for the method itself, but I highlighted with //problem here to show where the problem area is. This is supposed to be run by a thread.

public void OSTBPM(object o)
{
string track = "";
bool running = false;
Action<object> wait = (j) =>
{
string t = track;
Game1.showRedMessage("Great!",false);
for(int tim = 0; tim< 50; tim++)
{
if(t != track)
{
break;
}
Thread.Sleep(1);
}
running = false;
};


buffDataMusic = new BuffAttributesData();
buffDataMusic.AttackMultiplier = 3;
buffDataMusic.LuckLevel = 3;

buffEffectsMusic = new BuffEffects(buffDataMusic);

int val = 0;
while (true)
{
try
{
track = Game1.getMusicTrackName();

while (Game1.getMusicTrackName() != null && BPM.TryGetValue(Game1.getMusicTrackName(), out val) && Game1.player != null && track == Game1.getMusicTrackName())
{
val /= 2;
for(int tim = 0; tim< val-75; tim++)
{
Thread.Sleep(1);
if(Game1.getMusicTrackName() != track)
{
break;
}

}
if(Game1.getMusicTrackName() == track)
{


for(int tim = 0; tim< 150; tim++)
{

// Problem here.
musicAttack = new Buff("1337", "none", "none", 150, null, 20, buffEffectsMusic, false, "MusicBuff", "Only active now")

Game1.player.applyBuff(musicAttack);


if (Game1.getMusicTrackName() != track)
{
break;
}
if(Helper.Input.IsDown(SButton.MouseLeft) && running == false)
{

running = true;
new Thread(new ParameterizedThreadStart(wait)).Start();
}

Thread.Sleep(1);
}


}


}
}
catch (Exception e)
{
while(!(Game1.getMusicTrackName() != null && BPM.TryGetValue(Game1.getMusicTrackName(), out val) && Game1.player != null))
{
Thread.Sleep(1);
//Console.WriteLine("fail");
}
}
}
}

The problem itself is an exception which is here. It happens almost randomly.

[game] An error occurred in the base update loop: ArgumentNullException: Value cannot be null. (Parameter 'key')
at System.Collections.Generic.Dictionary`2.FindValue(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at StardewValley.Buffs.BuffManager.Update(GameTime time) in D:\GitlabRunner\builds\Gq5qA5P4\0\ConcernedApe\stardewvalley\Farmer\Farmer\Buffs\BuffManager.cs:line 266
at StardewValley.Farmer.Update(GameTime time, GameLocation location) in D:\GitlabRunner\builds\Gq5qA5P4\0\ConcernedApe\stardewvalley\Farmer\Farmer\Farmer.cs:line 7301
at StardewValley.Game1.UpdateCharacters(GameTime time) in D:\GitlabRunner\builds\Gq5qA5P4\0\ConcernedApe\stardewvalley\Farmer\Farmer\Game1.cs:line 10676
at StardewValley.Game1._update(GameTime gameTime) in D:\GitlabRunner\builds\Gq5qA5P4\0\ConcernedApe\stardewvalley\Farmer\Farmer\Game1.cs:line 4324
at StardewValley.Game1.Update(GameTime gameTime) in D:\GitlabRunner\builds\Gq5qA5P4\0\ConcernedApe\stardewvalley\Farmer\Farmer\Game1.cs:line 3454
at StardewModdingAPI.Framework.SCore.OnPlayerInstanceUpdating(SGame instance, GameTime gameTime, Action runUpdate) in /home/pathoschild/git/SMAPI/src/SMAPI/Framework/SCore.cs:line 1094

For some reason, musicAttack has to be defined in the for loop it in or the game thinks that the milliseconds duration is null. I'm adding a new key that isn't in the buffs file, so this might be an issue?
Ill see if I can add it, but for now, any help would be great :D
 
Top