-
Notifications
You must be signed in to change notification settings - Fork 2
Objects
TaffyScript isn't really a traditional Object-Oriented programming language, but it does have support for an object construct.
When defining an object, you don't give it any member variables. Instead you give it instance scripts, hereby called methods to distinguish them from global scripts.
object obj_name {
script create {
name = "Chris";
}
}In order to create a new object type, you start with object followed by the object name. Personally, I follow a Gamemaker inspired naming scheme and prefix all objects with obj_ or par_ in order to avoid naming conflicts, but it is not strictly necessary. In between braces, you can fill out the object with methods.
A method is just a script that is bound to an instance. It can access all of the other methods and variables defined by its type.
A method is defined in the same way as a global script. It starts with script, followed by the method name. Optionally there can be a list of arguments in between parentheses. The arguments can have default values by putting a = followed by a constant value. You cannot have an argument with a default value before an argument without. The method can access the argument array in the same way as a script.
If you assign any variable inside of a method that isn't a local variable (i.e. it wasn't created with var), it will be assigned to that instance. In addition, any global scripts called from the method can write new variables or read existing ones from the instance in the same fashion.
There is one special method name in TaffyScript: create. When you create an instance of an object, the create method will be called if it exists. In other words, the create method doubles as a constructor.
When inside of the object definition, you have direct access to all of its methods. Outside of it, you can call the scripts via an instance of the object.
object obj_example {
script public() {
internal(); //Calls a defined script directly
}
script internal() {
print("Called internal method");
}
}
script main {
var example = new obj_example();
example.public(); //Calls a script via access.
}In order to create an instance of an object, you can use the new keyword, followed by the object name, followed by a set of parentheses containing the arguments to be passed into the instances create method.
object obj_name {
script create(my_name) {
name = my_name;
}
}
script main {
var obj = new obj_name("Alexander Hamilton"); //Create an instance of obj_name
}There is also a script defined in the Base Class Library that can be used to create an instance: instance_create(object_name, ..args) where object_name is the name of the object to create, and args is an optional parameter that should be the arguments to pass to the create method. Please note that instance_create is not as efficient as just using new, so it should only be used when the type of the object is not known at compile time.
There are two ways to access variables on an instance. You can directly access it using a dot. Using our earlier example:
var inst = instance_create(obj_name, "Alexander Hamilton");
var name = inst.name; //Read
inst.name = "Bob"; //WriteAlternatively, any code placed inside of a with block will be run as if the object had called it. A with statement lloks like the following:
with(instance) <statement>
//For example
var inst = new obj_name("Alexander Hamilton");
with(inst) {
print(name); //Read
name = "Bob"; //Write
}Sometimes, you may want to have an argument with a specific name and an instance variable with the same name (especially with constructors). When this is the case, you can use the self keyword to refer to the current instance.
object obj_name {
script create(name) {
self.name = name;
}
}An object can inherit all of the methods from another object, known as the parent. The child object can override any of the parent method. An overridden method can call the parent method using the base keyword. Overridden methods don't need to have the same signature as the parent.
To inherit from another type, put : parent_type after the object name. For example:
object obj_parent {
script create(name) {
self.name = name;
}
script to_string() {
return name;
}
}
object obj_child {
script create(name, age) {
base(name); //call obj_parent.create
self.age = age;
}
script to_string() {
return base() + ", " + string(age);
}
}