Author Topic: Group.add(); doesn't add the object to the group. - Solved. My bad.  (Read 1136 times)

So I was fooling around with scriptGroups and I decided it would be a good alternative to the funky garbage I was doing before to check if a brick was being planted inside certain bounds.

So I've got this:
Code: [Select]
function inBounds(%brick) {
%client = %brick.client;
if(isObject(%client.lastBound))
if(isInBox(%brick,%client.lastBound))
return %client.lastBound;
for(%i = 0;%i < $LotGroup.getCount;%i++) {
if(isInBox(%brick,$LotGroup.getObject(%i))) {
%client.lastBound= $LotGroup.getObject(%i);
return %client.lastBound;
}
}
return 0;
}
(yes, for the first time in my life I am indenting code)

So that should check if the brick that was planted (via an onPlant package) inside the bounds. Now, on the other hand, I have this.
Code: [Select]
function fxDTSBrick::onPlant(%brick) {
Parent::onPlant(%brick);
if(!%brick.getDatablock().isBounds && inBounds(%brick) == 0) {
%brick.schedule(0,"delete");
return;
}

if(%brick.getDatablock().isBounds){
$LotGroup.add(%brick);
return;
}
}
When I plant a brick inside of the bounds, the brick gets deleted for being out of bounds (confirmed with console echoes) and the problem is that, in inBounds();, $LotGroup.getCount(); in the For loop returns 0.


If I manually type echo($LotGroup.getCount());, it will echo 0 even when bounds are planted. The only way to add the brick is manually putting into the console $LotGroup.add(brickID); and the syntax is the same in my code and in the console's input. I could use some help with this please.
« Last Edit: October 12, 2015, 11:48:48 AM by Pie Crust »

Show how you are handling setting the isBounds field on the datablock.

Code: [Select]
datablock fxDtsBrickData(Arena_brickBoundsdata: brick64x64FData)
{
category = "Arena";
subcategory = "Important";
cost=500;
uiName = "Bounds";
specialBrickType = "";
isBounds= true;
adminOnly = 0;
};
Nothing out of the ordinary. Also, this wouldn't be the issue since an echo that would be when the bounds brick is planted is being passed onto the console. (The echo was added after making the thread, fyi)

So I changed the code to this
Code: [Select]
function fxDTSBrick::onPlant(%brick) {
Parent::onPlant(%brick);
%bounds = inBounds(%brick);
echo(%bounds);
if(!%brick.getDatablock().isBounds && %bounds == 0) {
%brick.schedule(0,"delete");
return;
}

if(%brick.getDatablock().isBounds){
$LotGroup.add(%brick);
return;
}
}

And the console outputs the following when I press and hold my Enter button:
Code: [Select]
032552
032553
032554
032555
032556
032557
032558
032559
032560
032561
032562
032563
032564
032565
032566
032567
032568
032569
032570
032571
032572
032573
032574
032575
032576
032577
032578
032579
032580
032581
032582
032583
032584
032585
032586
032587
032588
032589
032590
032591
032592
032593
032594
032595
032596
032597
032598
032599
032600
032601
032602
032603
032604
032605
032606
032607
032608
032609
032610
032611
032612
032613
032614
032615
032616
032617
032618
032619

None of those are actual objects. isObject(); retuns false.


Code: [Select]
function inBounds(%brick) {
%client = %brick.client;
if(isObject(%client.lastBound))
if(isInBox(%brick,%client.lastBound))
return %client.lastBound;
for(%i = 0;%i < $LotGroup.getCount;%i++) {
if(isInBox(%brick,$LotGroup.getObject(%i))) {
%client.lastBound= $LotGroup.getObject(%i);
return %client.lastBound;
}
}
return 0;
}

$LotGroup.getCount(); in the For loop returns 0.

You are not calling $LotGroup.getCount(), you are accessing the variable getCount on $LotGroup. You forgot the ().
Idk if that will fix your overall problem.

Also that is inefficient, as every iteration it will call $LotGroup.getCount() . Instead you should either store the count in a variable before the loop; eg %count = $LotGroup.getCount(); for(%i = 0; %i < %count; %i++) { Or you could start at the end and work backwards; for(%i = $LotGroup.getCount() - 1; %i >= 0; %i--) {
The second method is slightly harder to read, but also slightly faster. Of course, it doesn't really matter in this case, as (I assume) there wont be too many objects in $LotGroup , but it's worth learning and getting used to.



And the console outputs the following when I press and hold my Enter button:
Code: [Select]
032552
032553
032554
-snip-

None of those are actual objects. isObject(); retuns false.

Just to be sure, you are doing the isObject() check before you delete them, right?

That's not necessary because the deletion part is within fxDTSbrick::onRemove(); and the brick MUST exist for that to be called. .add(); has no reason to not work.

Another thing I just noticed, you should not change the group of a brick, since they go in BrickGroups, which is a SimGroup that is created when a client joins, one per BL_ID, and all the bricks they own are placed into it.
Many brick functions rely on the brick being in that group, so by moving it you are breaking other things.

You should use a SimSet instead of a ScriptGroup (although SimSet doesn't allow you to use a custom class and stuff, and there's no ScriptSet. Use a ScriptObject with a reference to a SimSet).
An object has to be in exactly one Group, but can be in as many SimSets as you like.

An object has to be in exactly one Group
Then this was the problem. I didn't know this and I was trying to add it to a second group but still won't explain about it working on the console. I'll try the simsets.




Nevermind. For some reason, I duplicated the check and it was calling the one at the top of the code while I was editing the one at the bottom of the code.
« Last Edit: October 12, 2015, 11:48:33 AM by Pie Crust »