Official function to create an instance

Official function to create an instance, see system.js, line 1284.

SysActs.prototype.CreateObject = function (obj, layer, x, y)
{
    if (!layer || !obj)
        return;

    var inst = this.runtime.createInstance(obj, layer, x, y);
 
    if (!inst)
        return;
 
    this.runtime.isInOnDestroy++;
 
    var i, len, s;
    this.runtime.trigger(Object.getPrototypeOf(obj.plugin).cnds.OnCreated, inst);
 
    if (inst.is_contained)
    {
        for (i = 0, len = inst.siblings.length; i < len; i++)
        {
            s = inst.siblings[i];
            this.runtime.trigger(Object.getPrototypeOf(s.type.plugin).cnds.OnCreated, s);
        }
    }
 
    this.runtime.isInOnDestroy--;

    // Pick just this instance
    var sol = obj.getCurrentSol();
    sol.select_all = false;
    sol.instances.length = 1;
    sol.instances[0] = inst;
 
    // Siblings aren't in instance lists yet, pick them manually
    if (inst.is_contained)
    {
        for (i = 0, len = inst.siblings.length; i < len; i++)
        {
            s = inst.siblings[i];
            sol = s.type.getCurrentSol();
            sol.select_all = false;
            sol.instances.length = 1;
            sol.instances[0] = s;
        }
    }
};

Procedure of plugins and event sheet in each tick

  1. pretick() in plugins
  2. tick() in behaviors
  3. posttick() in behaviors
  4. tick() in plugins
  5. "condition: every tick" in event sheet
  6. tick2() in behaviors
  7. tick2() in plugins
  8. draw() in engine 

Reference: preview.js, line 2416, function "logic".

Get behavior instance

function GetThisBehavior(inst)
{
    var i, len;
    for (i = 0, len = inst.behavior_insts.length; i < len; i++)
    {
        if (inst.behavior_insts[i] instanceof behaviorProto.Instance)
            return inst.behavior_insts[i];
    }
        return null;
};

See the official DragnDrop behavior.
"behaviorProto" could be changed to specific behavior.

timescale of object

To get dt of instance, use
var dt = this.runtime.getDt(inst);

But only plugin which has "type" to "world" would have my_timescale property. So my_timescale need to be added manually for other case.
For example:
this.my_timescale = -1

Reorder z order of layer

Step1. Get layer
var layer = inst.layer;
or
var layer = (typeof layerparam == "number")?
            this.runtime.getLayerByNumber(layerparam):
            this.runtime.getLayerByName(layerparam);


Step2. Sort the list layer.instances
layer.instances(SORTFN);
for example
function sortInstanceByZIndex(a, b)
{

    return a.zindex - b.zindex;
}

Step3. reassign zindex of instances in this layer.
layer.zindices_stale = true;


Reference

Get timeline instance

instanceProto._timeline_get = function ()
{
    if (this.timeline != null)
        return this.timeline;

    assert2(cr.plugins_.Rex_TimeLine, "Scenario: Can not find timeline oject.");
    var plugins = this.runtime.types;
    var name, inst;
    for (name in plugins)
    {
        inst = plugins[name].instances[0];
        if (inst instanceof cr.plugins_.Rex_TimeLine.prototype.Instance)
        {
            this.timeline = inst;
            return this.timeline;
        }
    }
    assert2(this.timeline, "Scenario: Can not find timeline oject.");
    return null; 

};

Reference

Create instance

instanceProto.CreateInst = function (objtype,x,y,_layer)
{

    var layer = (typeof _layer == "number")?
                this.runtime.getLayerByNumber(_layer):
                (typeof _layer == "string")?

                this.runtime.getLayerByName(_layer):
                _layer;
 

    // call system action: Create instance
    cr.system_object.prototype.acts.CreateObject.call(
        this.runtime.system,
        objtype,
        layer,
        x,
        y
    );

   
    return objtype.getFirstPicked();  // return the created instance
};


Reference

Get SID from object-type, get object-type from SID

Get SID from object-type
var sid = objtype.sid

Get object-type from sid
var obktype = this.runtime.getObjectTypeBySid(sid);

It will check the object-types list to find the matched target. Keeping the index of the matched target might increase the performance.
var objtypes = this.runtime.types_by_index;
var t = objtypes[index];
if ((t != null) && (t.sid === sid))   // find the target
    return t;
var i, len=objtypes.length;
for (i=0; i<len; i++)
{
    t = objtypes[i];
    if (t.sid === sid)      // find the target
    {
        index = i;          // save the index
        return t;
    }
}

Reference

Get angle and distance between two points

Distance
var distance = cr.distanceTo(px0, py0, px1, py1);


Angle
var angle = cr.angleTo(px0, py0, px1, py1);      // in radian
angle = cr.to_clamped_degrees(angle);     // radian to degree

Reuse system action

cr.system_object.prototype.acts.SetLayerEffectParam.call(
    this.runtime.system, // this
    param0,
    ....
);

Destroy instance

this.runtime.DestroyInstance(inst);


Callback
This method will trigger a callback, add the callback by
this.myDestroyCallback = function (inst)
{
....
}
this.runtime.addDestroyCallback( this.myDestroyCallback ) 


Remove the callback by
this.runtime.removeDestroyCallback( this.myDestroyCallback );

Array shallow copy

cr.shallowAssignArray(to_arr, from_arr);

Function declaration string for eval

....
param = param.replace(re, "\\n"); // replace "\n" to "\\n"
var code_string = "function(scenario)\{\
    var MEM = scenario.Mem;\
    var Call = scenario._getvalue_from_c2fn;\
    return "+param+"\
    }";
_thisArg = this;
var fn = eval("("+code_string+")");
param = fn(this);
....

Reference


The variable space will be cleaned in this eval , in CocoonJS exporter.

Get UID from instance, get instance from UID

Get UID from instance
var uid = inst.uid;

Get instance from UID
var uid2inst = function (uid)
{
    if (uid == null)
        return null
    return this.runtime.getObjectByUID(uid);  
}

It will return an instance or null.

Reuse ACE

Reuse action/condition/expression from other plugin.

Action cr.plugins_.WebStorage.prototype.acts.StoreLocal.call(webstorage_obj, key, value);


Condition
cr.plugins_.WebStorage.prototype.cnds.LocalStorageExists.call(webstorage_obj, key);



Expression 

var fake_ret = {
    value:0,
    set_any: function(value){this.value=value;},
    set_int: function(value){this.value=value;},
    set_float: function(value){this.value=value;}, 
    set_string: function(value) {this.value=value;},
};
cr.plugins_.WebStorage.prototype.exps.LocalValue.call(webstorage_obj, fake_ret, key);
return fake_ret.value



Get instance 
var plugins = this.runtime.types; 
var name, inst; 
for (name in plugins)
{
    inst = plugins[name].instances[0]; 
    if (inst instanceof cr.plugins_.WebStorage.prototype.Instance)
    {
        // get target instance 
    } 
 
 
 
Reference - webstorage extension plugin