Skip to content
Kameron Brooks edited this page Oct 10, 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 not very exciting, but it shows what kind of things are possible with runtime scripting.

First, Create The 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 = col;
   }

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

The 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() {
      string script = LoadScript("demo_file.ccl");

      // Initialize the library to take advantage of delegate caching
      RuntimeFunctionLibrary.Initialize();

      // Create Lexer to tokenize script
      Lexer lexer = new Lexer();
      Token[] tokens = lexer.Tokenize(script);

      // Create compiler to compile script
      Compiler compiler = new Compiler(tokens);

      // Compile script, it is best to call this in a try catch, in case the script has an error
      try {
         _compiledScript = compiler.Compile();
         
         // Pass in context
         _compiledScript.SetContext(_context);
      } catch (Exception e) {
         // Compilation Error Handling code...
      }
   }

   public RunScript() {
      try {
         _compiledScript.function();
      } 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 will be fetched dynamically when the user starts the game/app.

Example 1

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

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.