- Home /
Frame Drop on a function dealing with an updated List type (Only once after new element Added)
Having an issue with a function running through an updated list and causing a stutter. My scripts are as follows:
private void OnGround() {
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, 0.2f, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject && isGrounded == false) {
currentJumps = playerStats["playerJumps"];
GetComponent<PlayerItems>().RunFunctionKey(gameObject, "OnGround"); //The cause
isGrounded = true;
}
}
}
Above, the Player touches ground here and updates the number of jumps it has based on its base stats and items. Ran in FixedUpdate.
public List<Dictionary<string, dynamic>> Inventory = new List<Dictionary<string, dynamic>>();
public void RunFunctionKey(GameObject player, string functionKey) {
foreach (var item in Inventory) {
if(item["function"].ContainsKey(functionKey)) {
item["function"][functionKey](player);
}
}
}
Above, the function handles checking if the item's functions have a key that triggers upon the condition's request. Some naive debugging has given me reason to believe the problem is the foreach bit. Commenting out item["function"][functionKey]... still causes the stutter but commenting out the foreach seemed to solve it. Of course I would want to figure out what the issue is more than have a patch.
public Dictionary<string, dynamic> Carrot = new Dictionary<string, dynamic>() {
{"id", 1},
{"name", "Carrot"},
{"description", "A healthy snack. Provides a second jump!"},
{"function", new Dictionary<string, Action<GameObject>>() {
{"OnGround", (player) => {player.GetComponent<PlayerMovement>().currentJumps+=1;}}
}
}
};
This is just the template of what an object in my List of Dictionaries looks like. Doesn't seem to be the cause of anything but thought it might be useful to give.
According to the profiler, it doesn't seem to like my RunFunctionKey either but I cannot find any documentation nor help on this error call of Updating delegates. Any help would be appreciated!
Edit: I forgot to mention what's happening within my game. My scene (2D), has a character that can pick up objects. If he picks up an object, which allows him to double jump, there is a frame drop when the player refreshes his jumps by touching the ground, OnGround(). This causes a stutter which occurs only a single time when the player touches the ground, or if he's already on the ground. As mentioned prevously, the debugging suggests it is something to do with the RunFunctionKey()'s foreach stuttering the first time.