Global Constant

12/05/2021 by Firdiansyah Ramadhan (firdi.ansyah20@gmail.com)

What is Global Constant?

Global Constant is a feature semi-package in Rocky Rampage, GC is a singleton which contains constant value (which can't be edited during run time) that can be accessed from anywhere during runtime to make programmer esier to manage values.

Feature

  • GC allows programmer access value of from anywhere during runtime.

  • GC can be overrided by Firebase Remote Config.

  • GC can be edited using Scriptable Object

  • GC separated by category which easier to manage

  • GC category can be released from memory, make memory usage more efficient.

Depedencies

Feature Usage

Get Values

you could use GC values from anywhere in the script by using this code

GlobalConstants.GetValue<T>(GCEnum.CategoryName.Variable);
GlobalConstants.GetValueDefault<T>(GCEnum.CategoryName.Variable);

Examples

You could use this to retrive value from GC. This value will be overrided by RC if there's exist Variable name in RC-Firebase. in this case any previous version will be overrided by current version variable.

float f = GlobalConstants.GetValue<float>(GCEnum.Gameplay.jumpChargePoints);

Otherwise in somecase you dont want the value overrided by RC whatever it cases. you could use sample below

float f = GlobalConstants.GetValueDefault<float>(GCEnum.Gameplay.jumpChargePoints); 

Clear a Certain Category

This feature allows developer to clear memory usage by clearing some category. you could use this feature using this code

GlobalConstants.ClearCategory(GCEnum.Category.CategoryName);

Examples

For example to use this feature you could use snippet below

GlobalConstants.ClearCategory(GCEnum.Category.AgeGate);

Clear All Category

This feature allows developer to clear memory usage by clearing all memory of Global Constants using this code.

GlobalConstants.ClearCache();

Then what will happen when we call GetValue() function ? the answer is, it will re-load all the data from the scriptable object again.

Overriding Value by Remote Config

To use this feature you need to open FirebaseConsole. then open project that connected with package joyseed-firebase-tools and go to Sidebar > Engage > Remote Config (see image below).

After the page opened you need to click button Add Parameter then add key as variables and your desired value as value.

Lastly, you need to make sure that the key in parameter is same with your key in GCEnum.

Example

As you can see, we have value called enable_age_gate .

Then you have to make sure that the keyCode and datatype are similar.

Implementing New Values

Adding new key to category

To Adding new key in a category you need to open script GCEnum.cs . then goto Category that you want then add new key to it.

public class GCEnum
{
    //...
    public enum Mamals
    {
        Crocodile,
        Lizard,
        Cat,
        Pinguin,
        Human,
        
        //add new key here
        //for example we add dog
        Dog
    }
    //..
}

Secondly you need to put the value of the key. in this case you can go edit Resources/ScriptableObjects/GlobalConstants/YourCategory Then add list and it should be adding new un-existed key.

Lastly , feelfree to choose data-type and values according to your needs.

Adding new category

Creating Enum

To add new Group/Category you need to create new Enum in GCEnum.cs and add CategoryName in GCEnum.Category inside GCEnum.cs

public class GCEnum
{
   public enum Category
   {
      Mamals,
   
      AgeGate,
      Gameplay,
      SuperPower,
      Systems,
      Lootbox,
      Quest,
      Menu,
      Vehicle,
      Tutorial,
      
      // add this
      Reptils
   }
   //...
}
public class GCEnum
{
   //...

   //add this
   public enum Reptils
   {
       Crocodile,
       Lizard
   }
   
   //...
}

Create ScriptableObject Generator

After creating enum, the next step is to create scriptable object. to do that you need to create new C# script inherited by GlobalConstants.CoreConfig<T> .

it is complicated to explain so just go straight to example. in Rocky Rampage project you should use format name GlobalConstant[CategoryName]Config.cs in this example we make it like thisGlobalConstantReptilsConfig.cs .

then go to you editor and paste this script inside your new config script.

using UnityEngine;

namespace Joyseed.BOD
{
    [CreateAssetMenu(fileName = "[CategoryName]", menuName = "BoD/GlobalConfig/[CategoryName]")]
    public class GlobalConstant[CategoryName]Config : GlobalConstants.CoreConfig<KeyAndValuePair[CategoryName]>
    {
    }

    [System.Serializable]
    public class KeyAndValuePair[CategoryName] : GlobalConstants.KeyAndValuePair<GCEnum.[CategoryName]>
    {
    }
}

Replace all [CategoryName] with your real category name. in this example we use Reptils . so it should be like this.

using UnityEngine;

namespace Joyseed.BOD
{
    [CreateAssetMenu(fileName = "Reptils", menuName = "BoD/GlobalConfig/Reptils")]
    public class GlobalConstantReptilsConfig : GlobalConstants.CoreConfig<KeyAndValuePairReptils>
    {
    }

    [System.Serializable]
    public class KeyAndValuePairReptils : GlobalConstants.KeyAndValuePair<GCEnum.Reptils>
    {
    }
}

Important Notes : you need to make sure the file name are equal with CategoryName otherwise it will not loaded.

Generate Scriptable Object

After you passed the previous step, you should be able to create new asset Scriptable object by using right-click>Create>BoD>GlobalConfig>Reptils .

as you can see the list should be empty on the first, but no worries to click + button as many as you want. because the're will be no over-list nor duplicated-list will be created.

Data Type

When you put values on global constant you might realize that you need to select data type. you might wondering what the meaning of the type?

such as "what is the meaning of Resources Queries ?" and how it works? what the return values

Data Table

Data Type

Explanation

Return Value

Integer

Immutable value type that represents

signed integers

int

Float

Floating-point number, which means

it is a number that has a decimal place

float

Boolean

a binary variable, having two possible

values called “true” and “false.”

bool

String

a set of characters

string

Vector2

Unity default class that contains x and y

Vector2

Vector3

Unity default class that contains x , y and z

Vector3

List String

Array of String

string[ ]

Date Time

value type represents dates with values

DateTime

Timespan

value type represents times with values

Timespan

Resources Queries

Queries asset with name path that contains cond in its name

List<string>

Convert Function

you probably still wondering some unclear explanation. such "how it works?" in this case you need to look directly to the script called GlobalConstantCoreConfig.cs

class GlobalConstant.PrimitiveValues : function GetValue() .

Further Development

Adding new Data Type

to add new datatype you need to edit script on 2 script

GlobalConstantCoreConfig.cs

First you need to add type on enum PrimitiveType

public enum PrimitiveType
{
    Integer,
    Float,
    Boolean,
    String,
    Vector2,
    Vector3,
    ListString,
    DateTime,
    Timespan,
    ResourcesQueries,
    
    //add new type
    Char
}

second you need to edit add convertion function inside class PrimitiveValues

public class PrimitiveValues
{   
    //char
    public char characterValue;
    
    //..
    
    public object GetValue()
    {
        switch (type)
        {
            //..
            case PrimitiveType.Char:
                return characterValue;
            //..
        }
        return null;
    }
}

GlobalConstantEditor.cs

Lastly, because GC package use PropertyDrawer you have to add switch case option and show Editor GUI property field to show property characterValue on the inspector.

public class PrimitiveValuesDrawer: PropertyDrawer
{
    //...
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {

        //...
        switch (primitiveType)
        {
            case GlobalConstants.PrimitiveType.Char:
                EditorGUI.PropertyField(position, property.FindPropertyRelative("characterValue"), GUIContent.none);
                break;
            
        }
        //...

    }
    //...
}

Other

...

Last updated