You can use findobject("objectname") to check if an object exists before destroying it.
For clearing out GUIs when you leave a level(assuming you are creating the GUI's in a level script) you can try a serverside onPlayerLeaves(), like so:
PHP Code:
function onPlayerLeaves() {
if (player != null) player.triggerClient("gui","SomeSystemWeapon","clearcontrols","ControlName");
}
Then have a system weapon to handle the client trigger:
PHP Code:
//#CLIENTSIDE
function onActionClientside() {
if (params[0] == "clearcontrols") {
if (params[1] != null) {
temp.obj = findObject(params[1]);
if (temp.obj != null) temp.obj.destroy();
}
}
}
Other methods could be adding the control name to an array to clear out(along with a level name it should be linked to) and in a System weapon scanning through the array onPlayerEnters() and destroying any controls that exist if the player is not in the level name linked to that control.
For the other issue you can try keeping track of when an object is going to be destroyed. Just so you know, objects can also have dynamic variables assigned to them.
PHP Code:
new GuiControl("Control") {
x = y = 10;
width = height = 100;
this.isSomeFlag = true;
}
You can now access Control.isSomeFlag as well as something like:
PHP Code:
echo("Flag State: " @ Control.isSomeFlag);
// Will echo the same state as above
with (Control) {
echo("Flag State: " @ this.isSomeFlag);
}
So with that in mind, you can try something like this:
PHP Code:
function CONTROLNAME.onKeyDown(keycode, keytext, scancode) {
if (keytext == "d") {
// Don't try to destroy if already queued to be destroyed;
if (CONTROLNAME.isDestroyed == true) return;
this.option = CONTROLNAME.getselectedrow();
CONTROLNAME.isDestroyed = true;
CONTROLNAME.destroy();
}
}