Author Topic: [Resource] SimObject::call and SimGroup::chainMethodCall  (Read 1204 times)

Here's a couple functions that seem like they should be part of the default game. The default call function can't be used with objects, so I made my own.

Code: (SimObject::call) [Select]
function SimObject::call(%this,%method,%v0,%v1,%v2,%v3,%v4,%v5,%v6,%v7,%v8,%v9,%v10,%v11,%v12,%v13,%v14,%v15,%v16,%v17)
{
%lastNull = -1;
for(%i = 0; %i < 18; %i ++)
{
%a = %v[%i];
if(%a $= "")
{
if(%lastNull < 0)
%lastNull = %i;
continue;
}
else
{
if(%lastNull >= 0)
{
for(%e = %lastNull; %e < %i; %e ++)
{
if(%args !$= "")
%args = %args @ ",";
%args = %args @ "\"\"";
}
%lastNull = -1;
}
if(%args !$= "")
%args = %args @ ",";
%args = %args @ "\"" @ %a @ "\"";
}
}

eval(%this @ "." @ %method @ "(" @ %args @ ");");
}



Example:
Code: [Select]
new scriptObject(test);
$method = "setColor";
$color = 0;
test.call($method, $color);




This function is used to recursively call a method.
Code: (SimGroup::chainMethodCall) [Select]
$ChainBatchSize = 100;
$ChainTimeOut = 10;

function SimGroup::chainMethodCall(%this,%method,%v0,%v1,%v2,%v3,%v4,%v5,%v6,%v7,%v8,%v9,%v10,%v11,%v12,%v13,%v14,%v15,%v16,%v17)
{
cancel(%this.chain_schedule);
%batch = (%this.chain_batchSize $= "" ? $ChainBatchSize : %this.chain_batchSize);
%index = (%this.chain_index $= "" ? 0 : %this.chain_index);
%count = %this.getCount();
%endIndex = (%index + %batch > %count ? %count : %index + %batch);

for(%i = %index; %i < %endIndex; %i ++)
{
%obj = %this.getObject(%i);
%obj.call(%method,%v0,%v1,%v2,%v3,%v4,%v5,%v6,%v7,%v8,%v9,%v10,%v11,%v12,%v13,%v14,%v15,%v16,%v17);
}
%this.chain_index = %index + %batch;
if(%this.chain_index >= %count)
{
if(isFunction(%this,%this.chain_callback))
%this.call(%this.chain_callback);
%this.chain_index = ""
%this.chain_batchSize = "";
%this.chain_timeOut = "";
%this.chain_callback = "";
}
else
{
%time = (%this.chain_timeOut $= "" ? $ChainTimeOut : %this.chain_timeOut);
%this.chain_schedule = %this.schedule(%time,"chainMethodCall",%method,%v0,%v1,%v2,%v3,%v4,%v5,%v6,%v7,%v8,%v9,%v10,%v11,%v12,%v13,%v14,%v15,%v16,%v17);
}
}



Example:
Code: [Select]
brickgroup_11902.chain_batchSize = 100; //this is optional
brickgroup_11902.chain_timeOut = 10; //this is optional
brickgroup_11902.chain_callback = "myCallback"; //this is optional - when the recursive method finishes, brickgroup_11902::myCallback(%this) will be called.
brickgroup_11902.chainMethodCall("setColor", 5);




I hope you find these useful! Please suggest any changes.
« Last Edit: May 02, 2013, 09:48:12 AM by Greek2me »