Skip to content
Kameron Brooks edited this page Dec 14, 2018 · 4 revisions

A Simple CCL Engine Demo

This is a simple Unity UI example that changes the color of the screen based on a CCL script. This demo is super basic, but it shows you how to use the engine in your application.

First, Create The Context Class

In unity, create a new c# file

// C#
// Context Class

class ApplicationContext: MonoBehaviour {
   [SerializeField]
   private Image _bgImage;
   
   // Sets the background color of the bgImage object
   void SetBackgroundColor(float x, float y, float z) {
      _bgImage.color = new Color(x,y,z);
   }

   DateTime GetDateTime() {
      return System.DateTime.Now;
   }
}

The Application Class

In unity, create another new c# file

// C#
// Application Class

class MyApplication : MonoBehaviour {
   CompiledScript _compiledScript;

   [SerializeField]
   ApplicationContext _context;
   //...
   private string LoadScript(string filename) {
      // Load a script from the file system at runtime
      //...
   }
   public void Start() {
      // Get the script from somewhere...
      string script = LoadScript("demo_file.ccl");

      // Load the standard libraries into the CCL Assembly
      // The libraries need to be loaded before compiling the scripts that need them
      CCL.ScriptUtility.LoadStandardLibraries();

      // Compile script, it is best to call this in a try catch, in case the script has an error
      try {
         // Compile script
         // Pass in the context class type at compile time
         _compiledScript = CCL.ScriptUtility.CompileScript(script, typeof(ApplicationContext));
         
         // Pass in context
         _compiledScript.SetContext(_context);
      } catch (Exception e) {
         // Compilation Error Handling code...
      }
   }

   public RunScript() {
      try {
         _compiledScript.Invoke();
      } catch (Exception e) {
         // Run Time Error Handling code...
      }
      
   }
}

The CCL File

This script can come from anywhere, it can be in the Resources folder, or saved in the persistent data folder on the end users device, it can even be fetched from a server when the application starts. The point is, it does not have to exist when you build the game/app. It can be fetched dynamically when the user starts the game/app or written on the fly by the user is you like.

Example 1

// CCL

SetBackgroundColor(0.0, 1.0, 0.0);   // Set background color to a green

The script set the background color of the app to green, based on a CCL script that was loaded at run time. The CCL script sets the background color of the app using a method on the context object named SetBackgroundColor.

Example 2

// CCL

int dayOfWeek = (int)GetDateTime().DayOfWeek;
if(dayOfWeek == 0) { // If today is Sunday
   SetBackgroundColor(0.0, 1.0, 0.0);   // Set background color to a green
}
else if(dayOfWeek == 1) { // If today is Monday
   SetBackgroundColor(1.0, 0.2, 0.2);   // Set background color to a red
}
else if(dayOfWeek == 6) { // If today is Saturday
   SetBackgroundColor(0.2, 1.0, 0.2);   // Set background color to a green
}
else {  // If today is Other
   SetBackgroundColor(0.6, 0.6, 0.6);   // Set background color to a grey
}

The script set the background color based on what day it is. The CCL script first gets the day of the week by calling a method on the context called GetDateTime. This method returns a System.DateTime object which has the property DayOfTheWeek The CCL script sets the background color of the app by calling SetBackgroundColor. based on the day of the week from the System.DateTime object returned by the context.