Objects - KidneyThief/TinScript1.0 GitHub Wiki

This page focuses on the life cycle of objects, and their access and integration with a C++ project.

Table of Contents

object

  • In TinScript, this is a variable type, just like a float or an int variable. The usage of variables of type object is as a handle.
    • e.g. object obj_id = create CScriptObject("Test");
  • They can be assigned or passed as parameters - important to note, that this *does not* create a copy of the object.
    • e.g. object obj_id2 = obj_id;
      both variables refer to the same instance named "Test".
  • Printing a variable of type object will display the ID (integer), which can actually be used to reference the object. Object IDs are guaranteed unique within a thread.
    • e.g. Print(obj_id); // prints a number, say, 57

create

  • Any C++ class that is registered with TinScript, can be instanced using the 'create' keyword (as per the above example.
  • The class must have a default constructor (no arguments) - the name given ("Test", in our example) is a script identifier that allows you to find that object by name. Names are neither required to exist, or be unique.
  • The value returned by using the 'create' keyword is of type object, as described above.

<namespace>::OnCreate()

  • The object's namespace is determined upon creation - either by registered C++ class name, or by the name of the object itself. For more details, see the Namespaces section.
    • e.g. object obj_id = create CScriptObject("Test");
      will automatically call void Test::OnCreate() { ... } if it exists.
    • If that method does not exist, then the C++ class (in this case CScriptObject) is used, and again, if it exists, we automatically call: void CScriptObject::OnCreate() { ... }
    • This is similar, but not a constructor, in the sense that it is top down, calling only the objects actual <namespace>::OnCreate() method. You can mimic a constructor by scripting your ::OnCreate() methods to each call their parent class' ::OnCreate().

destroy

  • Since we have a 'create', we also have a 'destroy' keyword - which does what you would expect, it deletes an instance of an object. Variables of type object will still retain the integer value of the ID, but using them to try to access an object will give an "Unable to find object" error.

<namespace>::OnDestroy()

  • As in the case with ::OnCreate(), this method is automatically called when the destroy keyword is used to delete an object.
  • And also in the same spirit, ::OnDestroy() is not a destructor - only the object's actual <namespace>::OnDestroy() method is called. You must manually call the parent namespace methods manually, if you want the same behavior as a destructor.
    • e.g. destroy obj_id;
      will automatically call void Test::OnDestroy() { ... } if it exists.

self

  • There's an implicit 'this' keyword in C++, the TinScript equivalent is the 'self' keyword. If you are within an object's method (anywhere in the hierarchy), the 'self' keyword is used to refer to the object who's method is currently being executed.
    • e.g. object obj_id = create CScriptObject("Test");
      Print(obj_id); // suppose it printed '41'.

      We know that the ::OnCreate() is automatically called - suppose we had implemented:
      void Test::OnCreate()
      {
              Print(self); // This would *also* print '41'
      }
  • The reason for the 'self' keyword, is to differentiate between local variables, and members of objects. Within unittest.cpp, a class CBase has been registeredas well as a member named "floatvalue" (of type float, obviously), with a default value of 27.0f.
    • Suppose we had implemented:
      object obj_id = create CBase("TestBase");
      void TestBase::OnCreate()
      {
              float floatvalue = 19.0f;
              Print(floatvalue); // prints 19.0000, as it refers to the local variable
              Print(self.floatvalue); // prints 27.0000, since it's refering to the object member, not the local value.
      }
  • To go one step further, object can have members *added*, by declaring a variable using the self keyword:
    • e.g. Had we implemented the following methods, note the declaration of self.new_member in ::OnCreate()
      and the print statement in ::TestNewMember()
      void TestBase::OnCreate()
      {
              int self.new_member = 15;
              self.TestNewMember(); // calls the method implemented below
      }
      void TestBase::TestNewMember()
      {
              Print(self.new_member); // prints 15
      }
  • Accessing members uses the same syntax, whether the member is a class member registered from code, or a member declared in script. It is also the same, whether you reference a member through an object handle, or via the 'self' keyword.
    • e.g. Assuming the above methods were executed
      object obj_id = create CBase("TestBase");
      Print(obj_id.new_member); // also prints 15
⚠️ **GitHub.com Fallback** ⚠️