Global Variable (simple+complex types) and how to save/get them!

This is just a quick thrown-together guide on how to save and also get global variables.

Simple Get/Save Global Variable

The normal way that you should already be familiar with, also seen in the docs:

string myString = CPH.GetGlobalVar<string>("test",true);

In that case, we are getting the value of test as a string into a string variable called myString. Sounds simple enough, right? Basically, you have a variable of type “xy”, so you also want to get the global variable of type “xy”.

For not so complex type it’s fairly easy to save the variables as well. Seen again here.

CPH.SetGlobalVar("test", myString, true);

Remember the last parameter in the get and set methods is whether you are getting/saving to a persisted global variable or not. Persisted (true) are variables that will still exist even after you close Non-persisted (false) will be automatically deleted when closing

How do I know if a global variable even exists in my code?

If you are unsure if a global variable, when trying to get it, even exists you can check whether it’s a null value or not.

string myString = CPH.GetGlobalVar<string>("test",true);
if(myString == null)
   //Do whatever would happen when the variable "test" would not exist

To immediately give a variable a default value when the global var does not exist you can do the following:

string type is always nullable in c# so you can do the following

string myString = CPH.GetGlobalVar<string>("test", true) ?? "My default string";

Most of the types are not nullable however so what do I do? You can add a ? behind the type in the method, to make it nullable. Here are two examples:

int myInt = CPH.GetGlobalVar<int?>("test", true) ?? 0;
List<int> myIntList = CPH.GetGlobalVar<List<int>?>("testList",true) ?? new List<int>();

Getting/Saving more complex data structures

At a certain point of data structure complexity, you won’t be able to just simply get and save your global variables anymore. From personal experience, it seems that anything that goes deeper than 4 or more enumerable types will cause issues. Why is that? When saving a global variable, it will get converted to a JSON string first and then it will be saved. The same issues arise when you’re trying to save custom classes, as the converted does not directly see how it should be converted. You will have to try and see what can be saved normally and what not. Check your logs while saving/getting global variables!
Here is just an example of what can still work to safely save and get:

Dictionary<string,List<List<string>>> test = new Dictionary<string,List<List<string>>>();
CPH.SetGlobalVar("complex", test, false);
Dictionary<string,List<List<string>>> testReturn= CPH.GetGlobalVar<Dictionary<string,List<List<string>>>>("complex", false)

Now the question would be what do I do with those more complex types/classes if I want to save them? The answer would be to first serialize them into a string and save them like that. Then when getting the global variable, use a string and deserialize it into your needed type/class. This can be done with the included Newtonsoft.Json library.
Example with custom class:

//Use the Newtonsoft.Json library
using Newtonsoft.Json;	 
//Custom class
public class TestClass
  private string userName {get;set;}
  private Dictionary<string,string> userThings {get;set;}
//... Let's say I have a list of TestClass objects
List<TestClass> testList = new List<TestClass>(){...};
string jsonSaveString = JsonConvert.SerializeObject(testList);
CPH.SetGlobalVar("savingClass", jsonSaveString, true);

With that, the List has now been saved as a JSON string into the global variable without any issues.
How to get the List out of the global variable would be the following:

//Like mentioned before, first you get the global variable as a string
string testListString = CPH.GetGlobalVar<string>("savingClass", true);
//Then you convert it back to the original List<TestClass>
List<TestClass> savedClass = JsonConvert.DeserializeObject<List<TestClass>>(testListString);

That should pretty much give you the ability to store any kind of data structures in your global variables. Have fun!

1 Like