I have a dictionary containing several items, which are never removed through code.
However, at random times, dictionary loses all its items.
I am very puzzled what could be causing this.
Edit: based on feedback from comments.
-
Action is not Monobehaviour. Does that make a difference?
-
When items are lost, all items are lost, i.e. dictionary is empty, but not null.
-
The issue occurs randomly, that is, not every game run, not at exact same time, not after a specific
action. -
Actions should not destroy themselves by their code. They are just classes that do math and move gameobjects.
-
Before the issue, actions perform as intended, i.e. are called by BehaviouSystem by key and do respective calculations and actions.
-
The NoneAction is just an inmplementation of the interface, with all functions being empty, that means logic should not be removing anything from the dictionary for sure.
Dictionary [ActionDictionary] is not referenced anywhere else, except for this class:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum Action
{
Eat,
None,
}
public class BehaviourSystem : MonoBehaviour
{
private float reactionPeriod;
private float timeWithoutReaction;
private Action CurrentStrategy = Action.None;
private Dictionary<Action, IAction> ActionDictionary = new Dictionary<Action, IAction>();
void Awake()
{
InitStats();
InitActions();
}
#region Initialization
void InitStats()
{
reactionPeriod = 1.0F;
timeWithoutReaction = 0.0F;
}
void InitActions()
{
ActionDictionary.Add(Action.None, new NoneAction());
List<List<ObjectTag>> edibleTagCombinations = new List<List<ObjectTag>>()
{
new List<ObjectTag>(){ ObjectTag.Edible, ObjectTag.Small, ObjectTag.Plant }
};
ActionDictionary.Add(Action.Eat, new EatAction(gameObject.transform, edibleTagCombinations));
}
#endregion
void Update()
{
timeWithoutReaction += Time.deltaTime;
if(timeWithoutReaction > reactionPeriod)
{
timeWithoutReaction -= reactionPeriod;
CurrentStrategy = GetBestAction();
ActionDictionary[CurrentStrategy].MakeDecision();
}
}
private void FixedUpdate()
{
ActionDictionary[CurrentStrategy].PerformAction();
}
private Action GetBestAction()
{
Action bestAction = Action.None;
float bestActionScore = float.MinValue;
foreach (var potentialAction in ActionDictionary)
{
float actionScore = potentialAction.Value.GetActionPriorityScore();
if (actionScore > bestActionScore)
{
bestActionScore = actionScore;
bestAction = potentialAction.Key;
}
}
return bestAction;
}
}