Author Topic: Scriptobjects  (Read 1126 times)

Alright, I understand how scriptobjects work and all, but there's one problem I encountered that I can't solve.

I'll try to explain it best I can:
In the Scriptobject, I have the variables Type[0], Type[1], ... Type[x] and I want to be able to remove Type[2] and have all the other types 'move down'. So Type[3] becomes Type[2], Type[4] becomes Type[3] and so on.
How would I make it so they will 'move down'?

You want to use a ScriptGroup/ScriptObject coupling.
ScriptGroups are essentially SimGroups with the ability to be extended upon, like ScriptObjects.

For instance:

Code: [Select]
function MommaBear::addBaby(%so)
{
%baby = new ScriptObject()
{
class = "Bear";
maternal = %so.getID();
name = getRandomString(5) SPC getRandomString(10);
};

if(isObject(%baby))
%so.add(%baby);
else
error("::addBaby() - ScriptObject not created!");
}

if(!isObject(MommaBear))
{
new ScriptGroup(MommaBear)
{
class = "Bear";
name = "Momma";
};

for(%a = 0; %a < getRandom(3, 8); %a++)
MommaBear.addBear();

MommaBear.listObjects();
}
« Last Edit: March 14, 2011, 01:38:18 PM by Iban »

You want to use a ScriptGroup/ScriptObject coupling.
ScriptGroups are essentially SimGroups with the ability to be extended upon, like ScriptObjects.

These can be very risky if you need to preserve order. Removing objects will sometimes shuffle the list.
As for moving things down, you can do a loop through each and set it to what will be moving into it.

Code: [Select]
for(%i = %slot; %i < %count; %i++)
    %object[%i] = %object[%i + 1];

These can be very risky if you need to preserve order. Removing objects will sometimes shuffle the list.
I have never seen a ScriptGroup/SimSet/SimGroup ever shuffle the list of objects. Provide an example on how to produce this.

Edit: You're right, but it doesn't shuffle, it just sticks the last object in the list in the place that the recently deleted object was in.
« Last Edit: March 14, 2011, 01:42:27 PM by Iban »

Thanks for the quick replies, I really had no idea of what I should have been doing.

Thanks for the quick replies, I really had no idea of what I should have been doing.
Are you cataloging a single piece of information, or are you doing objects?

I have never seen a ScriptGroup/SimSet/SimGroup ever shuffle the list of objects. Provide an example on how to produce this.

Edit: You're right, but it doesn't shuffle, it just sticks the last object in the list in the place that the recently deleted object was in.

Ah yeah, I wasn't sure exactly what it did. I just knew I had been screwed over by it in the past.

Quote
==>new ScriptGroup(A_Group);
==>for(%i = 0; %i < 10; %i++) { %obj = new ScriptObject() { letter = getSubStr("ABCDEFGHIJ",%i,1); }; A_Group.add(%obj); }
==>function list() { for(%i = 0; %i < A_Group.getCount(); %i++) echo(A_Group.getObject(%i).letter); }
==>list();
A
B
C
D
E
F
G
H
I
J
==>echo(6772.letter);
F
==>A_Group.remove(6772);
==>list();
A
B
C
D
E
J
G
H
I

Are you cataloging a single piece of information, or are you doing objects?
Just one piece of information.

Just one piece of information.
I wouldn't go with SG/SS/SGs, then.
I'm sure Truce has a ScriptObject that can do that.

However, if the information is anything more complex than just a list, I'd go with the fancy stuff.

However, if the information is anything more complex than just a list, I'd go with the fancy stuff.
Using your fancy way, how do I refer to a ScriptObject that's in a ScriptGroup?

Using your fancy way, how do I refer to a ScriptObject that's in a ScriptGroup?
ScriptGroup.getObject(%num);

Can you only add scriptObjects to scriptGroups? Or can a scriptObject "parent" other objects?
« Last Edit: March 14, 2011, 02:49:38 PM by lilboarder32 »

ScriptGroup.getObject(%num);
Thanks, I tried getObject() but wasn't sure what the arg was.

Can you only add scriptObjects to scriptGroups? Or can a scriptObject "parent" other objects?
You can't add an object to ScriptObjects. You can only add them to ScriptGroups.
« Last Edit: March 14, 2011, 02:56:12 PM by HellsHero »

Code: [Select]
function scriptObject::removeType(%this, %i) {
   %this.type[%i] = "";
   for(();%i<%types;%i++)
      %this.type[%i - 1] = %this.type[%i];
   %this.types--;
   %this.type[%this.types] = "";
}

Might not be optimized, but should work.

Code: [Select]
function scriptObject::removeType(%this, %i) {
   %this.type[%i] = "";
   for(();%i<%types;%i++)
      %this.type[%i - 1] = %this.type[%i];
   %this.types--;
   %this.type[%this.types] = "";
}

Might not be optimized, but should work.
Yeah well there's a syntax error on that for loop there.

I made this a few months ago for storing dynamic teams:
http://dl.dropbox.com/u/551734/code/torque/genericArraySO.cs
Code: [Select]
function genericArraySO::onAdd(%this)
{
%this.count = 0;
}
function genericArraySO::add(%this,%value)
{
%this.value[%this.count] = %value;
%this.count++;
}
function genericArraySO::get(%this,%index)
{
return %this.value[%index];
}
function genericArraySO::search(%this,%value)
{
for(%i=0;%i<%this.count;%i++)
{
if(%this.value[%i] $= %value)
return %i;
}
return -1;
}
function genericArraySO::remove(%this,%index)
{
%this.value[%index] = "";
for(%i=%index+1;%i<%this.count;%i++)
%this.value[%i-1] = %this.value[%i];
}
function genericArraySO::clear(%this)
{
for(%i=0;%i<%this.count;%i++)
{
if(isObject(%this.value[%i]))
%this.value[%i].delete();
%this.value[%i] = "";
}
%this.count = 0;
}