• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [QUESTION] Pool vs Create Instances

xDGameStudios

GameMaker Staff
GameMaker Dev.
I wanted to ask some questions regarding the benefits of instance pools!
A lot of times and in a lot of engines one of the best approaches to use is to create a pool for storing unused instances and instead of creating a new instance you retrieve them from the pool!

In game maker this can be done by deactivating instances and storing them and activating them upon request.

What are the benefits of doing so in GMS2, is it viable?! Is it better to create/destroy instances or activate/deactivate instance?! I ask this because the documentation says that de/activating instances is a process that impacts with performance.

Another question would be: are there any other ways of implementing this other then deactivating/activating instances?! Some ideias?!

Thank you for your time and attention
 

GMWolf

aka fel666
I wanted to ask some questions regarding the benefits of instance pools!
A lot of times and in a lot of engines one of the best approaches to use is to create a pool for storing unused instances and instead of creating a new instance you retrieve them from the pool!

In game maker this can be done by deactivating instances and storing them and activating them upon request.

What are the benefits of doing so in GMS2, is it viable?! Is it better to create/destroy instances or activate/deactivate instance?! I ask this because the documentation says that de/activating instances is a process that impacts with performance.

Another question would be: are there any other ways of implementing this other then deactivating/activating instances?! Some ideias?!

Thank you for your time and attention
I think pools are still faster than creating new instances.
Deactivation should be fine. I think deactivating instances doesnt affect performance that much, its when you start deactivating regions, or all, that you start seeing performance hits.
 

TheouAegis

Member
instance_change() along with deactivating. And don't forget you can still manipulate deactivated instances as long as you know their id.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
instance_change() along with deactivating. And don't forget you can still manipulate deactivated instances as long as you know their id.
Instance change?! How would you go about using it?!
Pooling instances and converting them one into another when needed?!!
I read instance_change is not that good... because all variables are passed through one instance to another.. no?!
is there a way of clearing an object?! (like clean all it's variables) that would be awesome :)
 

GMWolf

aka fel666
Dont use instance change, its not worth it.
Just pool your instances by type.

Pooling becomes especially useful when your instances have special data structures.
For instance, each instance may have its own list. In that case, you dont have to create and destroy that list everytime.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
Dont use instance change, its not worth it.
Just pool your instances by type.

Pooling becomes especially useful when your instances have special data structures.
For instance, each instance may have its own list. In that case, you dont have to create and destroy that list everytime.
But wouldn’t that be “dangerous” to keep lists and maps and not destroying them?!
I would need at least to clear them and for that I would need a custom “dispose” method for each type of object for cleaning purposes.
 
Last edited:

GMWolf

aka fel666
But wouldn’t that be “dangerous” to keep lists and maps and not destroying them?!
I would need at least to clear them and for that I would need a custom “dispose” method for each type of object for cleaning purposes.
its why GMS2 has the destroy and cleanup events.

Only free the lists in the cleanup events.
Have a custom create event, like an init event you call when the object is "unpooled"
 
T

TimothyAllen

Guest
its why GMS2 has the destroy and cleanup events.

Only free the lists in the cleanup events.
Have a custom create event, like an init event you call when the object is "unpooled"
But remember that deactivated instances don't have events called... including Room End and Destroy (which I use for clean up).
 

GMWolf

aka fel666
But remember that deactivated instances don't have events called... including Room End and Destroy (which I use for clean up).
Thats the point of pooling.
You want those deactivated instances to be available throughout the game, so that you dont have to re-create them everytime!
the cleanup will be called when you clean your pools though, or if you have a pool size limit
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
One more question that is somehow related to this!! Is it possible to call the clean up event?! like event_perform(ev_clean_up)
there is no macro for that event!!
 

GMWolf

aka fel666
One more question that is somehow related to this!! Is it possible to call the clean up event?! like event_perform(ev_clean_up)
there is no macro for that event!!
Yeah, it is.

But why? Call the destroy event when it goes back Into the pool, and the clean-up gets called automatically when you clean the pools and use instance destroy an all pooled instances.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
Yeah, it is.

But why? Call the destroy event when it goes back Into the pool, and the clean-up gets called automatically when you clean the pools and use instance destroy an all pooled instances.
@GMWolf do you know if the clean up event gets called even on deactivated instances when leaving rooms or do I have to activate them and destroy manually?!
 

GMWolf

aka fel666
@GMWolf do you know if the clean up event gets called even on deactivated instances when leaving rooms or do I have to activate them and destroy manually?!
I think so.
But just test it!
Deactivate an instance, and see if it gets cleaned up by showing a message in the clean-up event
 

GMWolf

aka fel666
Has anyone compared clearing a data structure vs. destroying and recreating it?
Unless GM does pooling, clearing should be quite a bit faster.

Memory allocation is slow.

Also, it's more complex than that:
Growing an array is expensive. But if the array has already grown, then resizing it is cheap.
So the array will be cheaper to use that way.
 

GMWolf

aka fel666
How do you resize an array in GML?
Well, with a list, you just add/remove values to it

... i think, if you dont understand memory allocations and such, that maybe you dont need instnace pooling. Are you actually seeing performance issues due to creating/destroying instances a lot?
Do you even need instances?
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
Well, with a list, you just add/remove values to it

... i think, if you dont understand memory allocations and such, that maybe you dont need instnace pooling. Are you actually seeing performance issues due to creating/destroying instances a lot?
Do you even need instances?
I do understand about memory allocation! But I was talking about resizing arrays not a list I was talking about an actual ARRAY ;)

When I pass data in my game I always pass around arrays and avoid lists because of GC... so I almost always have to create a list and then convert it to array in the end of the process :)
 

jo-thijs

Member
One more question that is somehow related to this!! Is it possible to call the clean up event?! like event_perform(ev_clean_up)
there is no macro for that event!!
event_perform(12, 0) does the trick.

You could have figured this out yourself through the following experiment:
Create an object with the following code in the create event:
Code:
for(var i = 0; i < 100; ++i) {
    for(var j = 0; j < 100; ++j) {
        k = false;
        event_perform(i, j);
        if k {
            show_message(string(i) + " :: " + string(j));
        }
    }
}
and the following code in the clean up event:
Code:
k = true;
However, this is not an approach supported by GameMaker and could thus chnge over versions or targets (although I don't think that will happen any time soon).

You hould also go with what GMWolf suggested instead.
You shouldn't be calling the clean up event like that.

Have your own method to clear data structures (use an "other user defined" event for this or use some scripts or both).
Let the clean up event destroy the data structues when the instances get destroyed (e.g. removed from the pool).

@GMWolf do you know if the clean up event gets called even on deactivated instances when leaving rooms or do I have to activate them and destroy manually?!
I think so.
But just test it!
Deactivate an instance, and see if it gets cleaned up by showing a message in the clean-up event
You'd hope so, but no.
GameMaker doesn't deal well with cleaning up deactivated instances.

Has anyone compared clearing a data structure vs. destroying and recreating it?
I just did.
This code:
Code:
var d = ds_list_create();
var t = current_time;

for(var i = 0; i < 1000000; ++i) {
    repeat 100 {
        ds_list_add(d, random(100));
    }
    ds_list_clear(d);
}

show_message(string(current_time - t) + " ms");
gave as results:
Code:
List clearing:
13596 ms
13660 ms
13652 ms
and this code:
Code:
var d = ds_list_create();
var t = current_time;

for(var i = 0; i < 1000000; ++i) {
    repeat 100 {
        ds_list_add(d, random(100));
    }
    ds_list_destroy(d);
    d = ds_list_create();
}

show_message(string(current_time - t) + " ms");
gave as results:
Code:
List destroying:
15151 ms
15075 ms
15266 ms
So GMWolf's statement seems accurate.

How do you resize an array in GML?
You can grow arrays in GML (happens automatically), but you can't shrink them.

You will have to just create a new array of lower dimensions that has a copy of the values of the former array.

If you don't care about memory consumption or defensive programming (having index out of bounds errors be noticed quickly) too much,
you can also simulate resizing an array by not shrinking the array, but keeping the length of the subarray that is being used (the "shrunk" length) in a separate variable.
Before GM:Studio, you had to do this anyway, as there were no array_length_* functions.

If you want to shrink an array to size 0, you can just set the array to 0 or [].
 

GMWolf

aka fel666
@jo-thijs is the real MVP.

The difference in performance may not look like much in jo-thijs' benchmark, but keep in mind most of the cycles will be spent doing the random function, and the list add operator.
 

Old2DGuy

Member
Been doing some research on this and instead of creating a new thread, I thought I would ask it here. I'm not seeing that much activity in search engines about using object pooling with GM2+. Some benchmarks showed just a small improvement over standard create/destroy instances. So is anyone using object pooling these days? I'm really....hoping GM2+ is optimized enough to not worry about this. Thx for any replies.
 
Top