• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

If there's any way to force instance_create act like a draw

GoliBroda

Member
For example we have that code in step or draw: if create = true {instance_create(obj)} .
when create = true the code will be creating 30 or even 60 objects per secound.
when you change it to false those objects will not dissapear.

I am using instance create for making windows for menu, dialogues etc (you know draw roundrect or something isnt look too estetic so i made an object that creates a windows with borders etc) and it's problematic for me to make new vars and you know, play ith those instance_destroys() especially instance_destroy() destroys all obiects with same name.
 
C

Ctl-F

Guest
What you can do if you must use objects is keep a list of the IDs of the objects you create and call instance_destroy on just that ID. You can take advantage of the fact that instance_create returns the ID of the instance created.

Code:
// create
instance_list = ds_list_create();

// draw
if(create == true){
    ds_list_add(instance_list, instance_create(obj));
}
else {
   for(i=0; i<ds_list_size(instance_list); i++){
      instance_destroy(instance_list[| i]);
   }
}

// destroy event (clean up)
for(i=0; i<ds_list_size(instance_list); i++){
   instance_destroy(instance_list[| i]);
}
ds_list_destroy(instance_list);
 
Just to clarify Ctl-F:
keep a list of the IDs of the objects you create
Ctl-F actually means a list of the instance IDs, not object IDs. He knows the difference but reading your post I assume you don't. So here's the thing many new GameMakers struggle with at first:
If you call the object ID you refer to the ressource or all the instances of said object. So:
Code:
with (obj_whatever) instance_destroy();
You'll destroy all instances of that object.

However the instance_create function returns the instance ID and you can store that as a value for future reference:
Code:
new_instance = instance_create(x, y, obj_whatever);
Now you can refer to that specific instance and just destroy that:
Code:
with (new_instance) instance_destroy();
And that's exactly what Ctl-F does. His code creates an instance and stores it's instance ID in a data structure for later reference.

Somehow I get the feeling your issue is more basic than that even. But since I don't really understand your question you might need to clarify what you coded, what you expected to happen and what really happened.
 
Last edited:

GoliBroda

Member
Ok, thanks for help, ill gonna try it

yeee, i made it in draw gui event:
Code:
    var i;
    i=true
    if i = true {i = false new_instance = instance_create(10, 10, o_Window)}
    if option = 7 {with (new_instance) instance_destroy();}
it's not working. i thing the script is creating more than 1 obiects.
 
Last edited:
What you do is very unlogic:
Code:
i = true;
if (i) {
    i = false;
    // do something
}
Does nothing useful at all. Everything in that if-statement get's executed every frame because i will always be true at the beginning . So yes, this code constantly creates instances of o_Window. This is definately not the way to do it.

You still need to clarify what you expected to happen. As an example:
I have a button object called o_Button. When I click that button I want an object called o_Window to appear. That o_Window should stay open for 10 seconds and then close.
 

RangerX

Member
And do you put this code in a draw gui event? It doesn't make sense and this event isn't meant for that.
 

FrostyCat

Member
You need to stop re-declaring that controlling variable, a quick manual trace would have easily revealed that. Declare it somewhere that runs only once.

Create:
Code:
create = false;
new_instance = noone;
Step:
Code:
if (create) {
  create = false;
  new_instance = instance_create(10, 10, o_Window);
}
if (new_instance != noone && option == 7) {
  with (new_instance) instance_destroy();
  new_instance = noone;
}
 
Top