Skip to content

Instantly share code, notes, and snippets.

@hasanbayatme
Last active November 17, 2023 09:14
Show Gist options
  • Save hasanbayatme/f3ddf56cf261a6b40efc14ecb9881a98 to your computer and use it in GitHub Desktop.
Save hasanbayatme/f3ddf56cf261a6b40efc14ecb9881a98 to your computer and use it in GitHub Desktop.

Revisions

  1. hasanbayatme revised this gist Mar 12, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    This is a simple static class to get and set globally accessible variables through a key-value approach.
    This is a simple C# static class to get and set globally accessible variables through a key-value approach.

    ## Installation

  2. hasanbayatme revised this gist Mar 12, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,8 @@ It works in any C# project such as Unity.
    It can be a great **cross-scene persistent data storage for Unity**,
    just import the script and use the `GlobalVariables` class to set and get data regardless of scenes.

    > It is also available on [UnityCommunity/UnityLibrary](https://github.com/UnityCommunity/UnityLibrary) repository at [Assets/Scripts/Utilities/GlobalVariables.cs](https://github.com/UnityCommunity/UnityLibrary/blob/master/Assets/Scripts/Utilities/GlobalVariables.cs).
    ## Usage

    Take a look at the below usage example to learn how to use the provided API.
  3. hasanbayatme revised this gist Mar 11, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GlobalVariables.cs
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@ public static T Get<T>(string key)
    /// <remarks>It uses a lock under the hood to ensure consistensy between threads</remarks>
    /// <param name="key">The variable name/key</param>
    /// <param name="value">The variable value</param>
    public static void Set(string key, string value)
    public static void Set(string key, object value)
    {
    lock (lockObject)
    {
  4. hasanbayatme created this gist Mar 11, 2021.
    65 changes: 65 additions & 0 deletions GlobalVariables.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    using System.Collections.Generic;

    /// <summary>
    /// A simple static class to get and set globally accessible variables through a key-value approach.
    /// </summary>
    /// <remarks>
    /// <para>Uses a key-value approach (dictionary) for storing and modifying variables.</para>
    /// <para>It also uses a lock to ensure consistency between the threads.</para>
    /// </remarks>
    public static class GlobalVariables
    {

    private static readonly object lockObject = new object();
    private static Dictionary<string, object> variablesDictionary = new Dictionary<string, object>();

    /// <summary>
    /// The underlying key-value storage (dictionary).
    /// </summary>
    /// <value>Gets the underlying variables dictionary</value>
    public static Dictionary<string, object> VariablesDictionary => variablesDictionary;

    /// <summary>
    /// Retrieves all global variables.
    /// </summary>
    /// <returns>The global variables dictionary object.</returns>
    public static Dictionary<string, object> GetAll()
    {
    return variablesDictionary;
    }

    /// <summary>
    /// Gets a variable and casts it to the provided type argument.
    /// </summary>
    /// <typeparam name="T">The type of the variable</typeparam>
    /// <param name="key">The variable key</param>
    /// <returns>The casted variable value</returns>
    public static T Get<T>(string key)
    {
    if (variablesDictionary == null || !variablesDictionary.ContainsKey(key))
    {
    return default(T);
    }

    return (T)variablesDictionary[key];
    }

    /// <summary>
    /// Sets the variable, the existing value gets overridden.
    /// </summary>
    /// <remarks>It uses a lock under the hood to ensure consistensy between threads</remarks>
    /// <param name="key">The variable name/key</param>
    /// <param name="value">The variable value</param>
    public static void Set(string key, string value)
    {
    lock (lockObject)
    {
    if (variablesDictionary == null)
    {
    variablesDictionary = new Dictionary<string, object>();
    }
    variablesDictionary[key] = value;
    }
    }

    }
    91 changes: 91 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    This is a simple static class to get and set globally accessible variables through a key-value approach.

    ## Installation

    Just download the script and import to your project or copy paste it to a script file.

    It works in any C# project such as Unity.

    It can be a great **cross-scene persistent data storage for Unity**,
    just import the script and use the `GlobalVariables` class to set and get data regardless of scenes.

    ## Usage

    Take a look at the below usage example to learn how to use the provided API.

    In the `FirstClass` we set the variables values:

    ```csharp
    public class FirstClass {

    public void SomeMethod() {
    GlobalVariables.Set("myStringVariable", "test");
    GlobalVariables.Set("myIntVariable", 123);
    GlobalVariables.Set("myObjectVariable", new List<string>());
    }

    }
    ```

    And then in the `SecondClass` we retrieve the values of the variables:

    ```csharp
    public class SecondClass {

    public void AnotherMethod() {
    string myStringVariable = GlobalVariables.Get<string>("myStringVariable");
    int myIntVariable = GlobalVariables.Get<int>("myIntVariable");
    List<string> myObjectVariable = GlobalVariables.Get<List<string>>("myObjectVariable");
    }

    }
    ```

    ## API

    ### Getting a Variable

    #### Description

    Gets the variable by the given key and casts it to the provided type argument `T`.

    #### Parameters

    - **key**: The variable name/key.

    #### Returns

    Returns the variable value casted to the provided type.

    ```csharp
    T GlobalVariables.Get<T>(string key);
    ```

    ### Setting a Variable

    #### Description

    Sets the variable by the given key.

    #### Parameters

    - **key**: The variable name/key.
    - **value**: The variable value.

    ```csharp
    void GlobalVariables.Set(string key, object value);
    ```

    ### Getting All Variables

    #### Description

    Gets all the variables.

    #### Returns

    Returns a dictionary of variables.

    ```csharp
    Dictionary<string, object> GlobalVariables.GetAll();
    ```