Having Manager object to check if there's another one

Aaeena

Member
Hi! My manager object (an object that handles most of the global fuctions and stuff, and is persistent) have many instances scattered across some of rooms in my project. I want them to check if theres already one in the game (before activating and/or executing its tasks) and if there is another one, to destroy itself.
Sorry for my english, can rephrase something if needed.
 
Howdydiddly buddy! What you will meed to do is pop in an if statement to check if the instance exsists like so:

Code:
if (!instance_exists(manager_obj);
   {
   Execute Tasks
   }
The ! Before the Instance_exists code makes sure that you are checking if NOT the statement :)

Hope that helps!
 

Aaeena

Member
Howdydiddly buddy! What you will meed to do is pop in an if statement to check if the instance exsists like so:

Code:
if (!instance_exists(manager_obj);
   {
   Execute Tasks
   }
The ! Before the Instance_exists code makes sure that you are checking if NOT the statement :)

Hope that helps!
Hi! I want though to Manager object check if theres ANOTHER Manager object, and if there is, to destroy only the one which was checking, right before executing its actions. I doubt that code will help with that but... that might be just me :)
 

samspade

Member
Here is my basic check (if I understand what you're asking)


Code:
with (object_index) {
   if (id != other.id) {
       instance_destroy(other);
   }
}
I generally turn this into a script and put it in the create event of any object I want there to be only one of. It will destroy any object of the same type after the first one.
 
Last edited:

Aaeena

Member
Here is my basic check (if I understand what you're asking)


Code:
with (object_index) {
   if (id != other.id) {
       instance_destroy(other);
   }
}
I generally turn this into a script and put it in the create event of any object I want there to be only one of. It will destroy any object of the same type after the first one.
the problem is, that my object still executes its variables (basically reverts any changes that were made to them), heres my code in Create event of the Manager object, the script is exactly copied from you
Code:
scrOnlyoneobject();
if instance_number(Manager) = 1 {
global.menuselection = 0
global.hsp = 0;
global.vsp = 0;
global.grv = 0.5;
global.walksp = 1;
global.jumpheight = -4; //have to be negative
global.fadeSpeed = .1;
global.warping = false;
global.firsttimeinmenu = 1;
}
 
Last edited:
I think I get what you are trying to do now :) It sounds as though what you need to do is use the 'for' functioning :) Like so:

Code:
//-----//
//If Other//
//-----//
var i = 0; //i is the base id//
for (i = 0; i < instance_count; i++;) //Checks if the id of each object is the same as this entity//
   {
   if instance_id[i] != id //if the id of the other is not the same as the calling instance//
      {
      with (instance_id[i]) //With the other instance//
         {
         instance_destroy(); //Destroy it//
         }
      }
   }
[
]

I hope this helps some pal! :D :D Apologies if there are any errors :D
 

Aaeena

Member
t
I think I get what you are trying to do now :) It sounds as though what you need to do is use the 'for' functioning :) Like so:

Code:
//-----//
//If Other//
//-----//
var i = 0; //i is the base id//
for (i = 0; i < instance_count; i++;) //Checks if the id of each object is the same as this entity//
   {
   if instance_id[i] != id //if the id of the other is not the same as the calling instance//
      {
      with (instance_id[i]) //With the other instance//
         {
         instance_destroy(); //Destroy it//
         }
      }
   }
[
]

I hope this helps some pal! :D :D Apologies if there are any errors :D
thank you for your reply! i have one question tho, where should i put it? i replaced the script i put before with it, but it doesnt seem to work
 

samspade

Member
the problem is, that my object still executes its funcions (basically reverts any changes that were made to them), heres my code in Create event of the Manager object, the stript is exactly copied from you
Code:
scrOnlyoneobject();
if instance_number(Manager) = 1 {
global.menuselection = 0
global.hsp = 0;
global.vsp = 0;
global.grv = 0.5;
global.walksp = 1;
global.jumpheight = -4; //have to be negative
global.fadeSpeed = .1;
global.warping = false;
global.firsttimeinmenu = 1;
}
That is how instance_destroy works: "It is worth noting that when you destroy an instance, its destroy event is called immediately after the code or action that calls the destroy. Second, although the destroy event is performed, the instance is not immediately removed from the game, and it will continue to perform the code contained in the current event. Only when the current event is over will it be removed from the game."

Since you are using global variables and the event still runs, it will set those global variables. If you need the object to destroy immediately and not execute any additional code there are two options:

Code:
//no script
with (object_index) {
   if (id != other.id) {
       instance_destroy(other);
       exit;
   }
}


//as script
///extraObjectDestroyed()
with (object_index) {
   if (id != other.id) {
       instance_destroy(other);
       return true;
   }
}
return false

//use
if (extraObjectDestroyed()) exit;
Note that the script version is more complicated now because if you call instance_destroy in a script, inside of an event, the rest of the event will still run (at least that is what it seemed like from a quick test I did) so you have to return something to know to stop the rest of the event. At this point, I would probably just use the code directly in the event, but the second version is helpful to know.
 

Aaeena

Member
That is how instance_destroy works: "It is worth noting that when you destroy an instance, its destroy event is called immediately after the code or action that calls the destroy. Second, although the destroy event is performed, the instance is not immediately removed from the game, and it will continue to perform the code contained in the current event. Only when the current event is over will it be removed from the game."

Since you are using global variables and the event still runs, it will set those global variables. If you need the object to destroy immediately and not execute any additional code there are two options:

Code:
//no script
with (object_index) {
   if (id != other.id) {
       instance_destroy(other);
       exit;
   }
}


//as script
///extraObjectDestroyed()
with (object_index) {
   if (id != other.id) {
       instance_destroy(other);
       return true;
   }
}
return false

//use
if (extraObjectDestroyed()) exit;
Note that the script version is more complicated now because if you call instance_destroy in a script, inside of an event, the rest of the event will still run (at least that is what it seemed like from a quick test I did) so you have to return something to know to stop the rest of the event. At this point, I would probably just use the code directly in the event, but the second version is helpful to know.
Thank you guys A LOT, it works! Aside from fixing my problem, i learnt a few things (most notably how scripts work). Thank you, again.
 

MCHLV

Member
EDIT: did not see you had your answer.
Hello,
Maybe Singleton Enforcement is what you are looking for.
Each instance within all your rooms (placed in the room using the IDE or created by code with instance_create) will first run its Create Event.
At the begining of this create event, a short test is ran to check if there is already already one in the game, if so the destroy_event is triggered and the exit function is used to stop the create_event from running the rest of it

Try with the following at the very begining of the Create Event :
Code:
// --------- singleton enforcement test
if ( instance_number(object_index) > 1 ) // how many object like me (counting me)
{
instance_destroy();
exit; // the code will stop here if this is the case
}

// --------- the rest of your code, skipped if there is already another me
if instance_number(Manager) = 1 {
global.menuselection = 0
global.hsp = 0;
global.vsp = 0;
global.grv = 0.5;
global.walksp = 1;
global.jumpheight = -4; //have to be negative
global.fadeSpeed = .1;
global.warping = false;
global.firsttimeinmenu = 1;
}
 
Top