DestroyObject - Why Does This Function Fail? (RESOLVED)

@kevL_s

I tested your script with the same setup and it also worked. So I am now going to try to setup the same on my mushrooms (which follow identical behaviour) and see what happens. I will report back here … I know you are resting now, but will update here with some results for you anyway. :slight_smile: Maybe I can see what breaks my usage?

RESULTS AND CONCLUSION:

OK, I adapted my own function to use similar as your own and gradually rebuilt to use the same code as my own. I tested the various changes on two identical patches of mushrooms (one copied from the other) in two areas … one had more objects than the other …

The patch that was in the original area (and the one that caused my issues to begin with) is the larger more complex area with more objects … Only one mushroom destroyed in all tests. BUT, in the smaller area, all mushrooms were destroyed.

So, the issue appears to be that the GetNearestObject function fails when there are many objects for it to work though in an area. Better to use GetFirstObjectInArea instead (as we discovered). The bottom line is that the GetNearestObject does have a maximum coping limit compared to its counterpart functions.

I am starting to see these various limitations as they tend to show quite quickly in MP games as well. I am just glad we have alternative means of achieving the same results. And I must try to remember which functions are less reliable under certain conditions.

image

1 Like

If you need objects-of-type inside a GetNearestObjectByTag() loop →

object o = GetNearestObjectByTag();
while (GetIsObjectValid(o))
{
    if (GetObjectType(o) == TYPE1 || GetObjectType(o) == TYPE2) // etc.
    {
        // do ...
    }
    o = GetNearestObjectByTag();
}

otherwise it should get, as you say, objects of any/all types

1 Like

Ok

1 Like

You probably won’t want to do a delete inside the loop. I think it messes up the evaluation. At least that’s been my experience with object loops.

it shouldn’t. destruction doesn’t happen until the script completes

not sure what your code was … but the following script iterates over ‘destroyed’ objects just fine here.

// 'test_destroy'
/*
	Console script test DestroyObject()
*/

void Delayed(object oPc, string sDelay);

void main()
{
	object oPc = OBJECT_SELF;

	int i = 0;
	for (; i != 5; ++i)
	{
		CreateObject(OBJECT_TYPE_PLACEABLE, "plc_nl_bonepile", GetLocation(oPc));
	}

	SendMessageToPC(oPc, ". after creation");
	i = 1;
	object o = GetNearestObjectByTag("PLC_NL_BONEPILE", oPc, i);
	while (GetIsObjectValid(o))
	{
		SendMessageToPC(oPc, IntToString(i) + " bonepile");
		o = GetNearestObjectByTag("PLC_NL_BONEPILE", oPc, ++i);
	}

	SendMessageToPC(oPc, ". during destroy");
	i = 1;
	o = GetNearestObjectByTag("PLC_NL_BONEPILE", oPc, i);
	while (GetIsObjectValid(o))
	{
		DestroyObject(o);
		SendMessageToPC(oPc, IntToString(i) + " bonepile");
		o = GetNearestObjectByTag("PLC_NL_BONEPILE", oPc, ++i);
	}

	SendMessageToPC(oPc, ". after destroy");
	i = 1;
	o = GetNearestObjectByTag("PLC_NL_BONEPILE", oPc, i);
	while (GetIsObjectValid(o))
	{
		SendMessageToPC(oPc, IntToString(i) + " bonepile");
		o = GetNearestObjectByTag("PLC_NL_BONEPILE", oPc, ++i);
	}


	float fDelay = 0.0f;
	DelayCommand(fDelay, Delayed(oPc, FloatToString(fDelay, 18, 2)));
}

void Delayed(object oPc, string sDelay)
{
	SendMessageToPC(oPc, ". delayed " + sDelay);
	int i = 1;
	object o = GetNearestObjectByTag("PLC_NL_BONEPILE", oPc, i);
	while (GetIsObjectValid(o))
	{
		SendMessageToPC(oPc, IntToString(i) + " bonepile");
		o = GetNearestObjectByTag("PLC_NL_BONEPILE", oPc, ++i);
	}
}

Dunno. I’ve had some issues with that operation. At least with the First/Next type loops.