Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reusing Dictionary objects

Discussion in 'Unity Analytics' started by ZenFri, May 4, 2015.

  1. ZenFri

    ZenFri

    Joined:
    Jul 17, 2014
    Posts:
    8
    I have a few CustomEvent calls that are reasonably complicated (having 7 to 10 different parameters on each), so in an attempt to stop memory allocations from occurring, I reuse the dictionaries.

    I have one Dictionary per CustomEvent type. Example:

    Code (CSharp):
    1.         mMatchCompleteDictionary = new Dictionary<string, object>
    2.         {
    3.             { "matchDefId", 0 },
    4.             { "aeSpent", 0 },
    5.             { "aeEarned", 0 },
    6.             { "nanosEarned", 0 },
    7.             { "nanosEarnedNavita", 0 },
    8.             { "pulsarsPlaced", 0 },
    9.             { "pulsarsSalvaged", 0 },
    10.             { "arraysPlaced", 0 },
    11.             { "arraysSalvaged", 0 },
    12.             { "duration", 0 }
    13.         };
    Then I simply reset all of the data in the Dictionary each time I go to submit the CustomEvent, instead of creating a new Dictionary each time. Example:

    Code (CSharp):
    1.         mMatchCompleteDictionary["matchDefId"]                = matchDef.defId;
    2.         mMatchCompleteDictionary["aeSpent"]                = matchStats.ReadValue(GameStatType.AE_SPENT);
    3.         mMatchCompleteDictionary["aeEarned"]                = matchStats.ReadValue(GameStatType.AE_EARNED);
    4.         mMatchCompleteDictionary["nanosEarned"]            = matchStats.ReadValue(GameStatType.NANOS_EARNED);
    5.         mMatchCompleteDictionary["nanosEarnedNavita"]        = matchStats.ReadValue(GameStatType.NANOS_EARNED_NAVITA);
    6.         mMatchCompleteDictionary["pulsarsPlaced"]            = matchStats.ReadValue(GameStatType.PULSARS_BUILT);
    7.         mMatchCompleteDictionary["pulsarsSalvaged"]        = matchStats.ReadValue(GameStatType.PULSARS_SALVAGED);
    8.         mMatchCompleteDictionary["arraysPlaced"]            = matchStats.ReadValue(GameStatType.EDC_ARRAYS_BUILT);
    9.         mMatchCompleteDictionary["arraysSalvaged"]            = matchStats.ReadValue(GameStatType.EDC_ARRAYS_SALVAGED);
    10.         mMatchCompleteDictionary["duration"]                = matchStats.ReadValue(GameStatType.MATCH_PLAYTIME);
    11.  
    12. UnityAnalytics.CustomEvent("matchComplete", mMatchCompleteDictionary);
    I just want to make sure it's safe to do that - are the Dictionary objects that get passed to CustomEvent cached internally in UnityAnalytics, or are they immediately written out/copied somewhere?

    Thanks!
     
  2. SeashellsRacer

    SeashellsRacer

    Joined:
    Oct 16, 2014
    Posts:
    13
    Eh, I wouldn't do this. Even if they are copying the dictionary or the info in the dictionary (allowing you free access to edit it after the function call) you don't know if a patch will come out in the future that will change this. Leading to a very hard to debug (but it use to work!) bug.
     
  3. kentunity

    kentunity

    Unity Technologies

    Joined:
    Sep 16, 2014
    Posts:
    55
    @ZenFri - it is safe to do pooling/reusing of the Dictionary passed to CustomEvent. Since we immediately serialize this object once we receive it.
     
  4. ZenFri

    ZenFri

    Joined:
    Jul 17, 2014
    Posts:
    8
    Awesome, thanks for the reply and confirmation.

    We've managed to get our game down to allocating very little memory while it's running, so I'm happy that the analytics will work this way. :)