• 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!

Instances or sprite element id overflow

trip

Member
Hi.
What is doing when instances or sprite Id is overflow?
I mean when I use first time instance_create () then Instances id is maybe 1 and I store this id for use later.
Now I create many instances, each new instance increase id by 1 and variable for instances id is 64bit?
What doing when I create more then max (I thing 9 223 372 036 854 775 807) instances?
Is possible having instances id with sign -?
And what doing when instances id overflow again?
will my first id forever connected with instances that was create on start
 
When an instance is destroyed, new objects can reuse that ID, so unless you have the maximum number of instances simultaneously, you'll never run out.

If you try to have that many instances in a game at the same time, it'll crash due to memory issues well before you hit that point.

(I actually just tested this, and GMS did better than I thought it would - a million blank instances ran, albiet at 7 fps, but when I tried to make a billion it crashed)
 

trip

Member
thanks but this is different example.
I mean 1. instances id is stored and each other instances are destroyed.
Instances id is increased when create new instance but not decreased when is destroyed.
 
Create instance A. It has a unique ID (10001)
Create instance B. It also has a unique ID (10002).
Destroy instance A. The ID it was using is now free.
Create instance C. It may have the ID that instance A used to have(10001), or it may have a never before used unique ID(10003). If instance C didn't use 10001 as it's ID, any instance created in the future may also use 10001 as it's ID. IDs will be reused before they overflow.
 
D

Deleted member 13992

Guest
If an instance uses up only a couple bytes of memory, which is extremely low, you'd still be looking at a computer with several petabytes of RAM needed for that many instances. I don't think that exists even among supercomputers.
 

TheouAegis

Member
I think someone tested this, once you reach the maximum number of instances, and once you reach instance ID $ffffffffffffffff it rolls back around to 1000000. But I think that person was destroying the instances every time. If you really want to test it, just write a simple program that does exactly what you're trying to test -

create a master controller object and stick it in your room. That instance will have an ID of 100000 (or 100001, doesn't matter). Is important that this is the only instance of this object in the room. Have another object and and it's create event check if id is less than 100001 (or 100002, whichever doesn't cause errors on startup) and then have a check if it's id is not the same as the controllers id, then if so, have it show id in a message. And the objects and step event, have it destroy itself. Have your controller object create a couple hundred of these every step, crank up your game speed, run the program, then go do something else for a day. You are looking for one or three things to happen. Either you come home and the messages showing which means everything works as hoped, or you come home to nothing, which means the controller got overwritten, or you come home to an error which doesn't tell you much of anything.
 
Last edited:

trip

Member
I think someone tested this, once you reach the maximum number of instances, and once you reach instance ID $ffffffffffffffff it rolls back around to 1000000.
oh first useful answer thanks.
You know, when I have instance number 1 000 000 as the first general Instance.
What happens now that instances number is again 1 000 000?
It change my general instance?

Now I tested layer sprite element.. Here is 32bit and when element id reach number 2,147,483,647 the next is -2,147,483,648, -2,147,483,647, -2,147,483,646 (it is like 0,1,2,3)
When I have Element_id 4 and I create Layer_sprite and it will have element id -2,147,483,644 (I thing, maybe 45) it rewrite previous sprite.
This is an unwanted action for my.

Is this same for instances?

Is possible force write instance_id and element_id?
for example. I know permanent important instances have id 1 000 000 000 - 1 000 000 100. When is next ID is above 10B I want write that next instances_id will 1 000 000 101.
 
Last edited:

Nidoking

Member
If you're performing any kid of mathematical operations with instance IDs, including greater than/less than comparisons, you're doing it wrong. Instance IDs, element IDs, resource IDs, and the like are numbers only insofar as they are numeric values and can be stored in numeric variables. They are numberish things, but should never be treated as numbers. Whatever you think you're doing by storing a maximum instance ID, don't do it. It's bad and wrong, and is an incorrect way of thinking about the program. You're almost certainly going to provide an explanation for why you think it's a fine thing to do, but I can tell you even without having read it, your explanation is wrong and you're doing something you shouldn't. Go back to the problem you were originally trying to solve when you came up with this method and solve it in a correct way, one that doesn't try to do math with IDs.
 

trip

Member
I don't have problem for now. I'm trying to avoid them. I need know Game maker 2 limits.
I no need mathematical operation with ids.
I want when I create instance or sprite when game start then this instances and sprite still same for forever. Only my action will change this instances or element.
 

TailBit

Member
If i remember correct then instance ids avoids the -100000 to 100000 range when they loop, it would make sence for it to avoids picking existing ids too, but I didn't test for that..

You can store stuff in arrays and use the array index to get a set number even if the ids change .. will that work for your purpose?
 

trip

Member
I finded bug with element_id Maybe. Can anybody test it?
OS Win 10. latest version GM 2
I testing create layer_sprite
Test project have 1 room, 1 object code is below, 2 sprites
GML:
//create event
var _i = 0
Layer = layer_get_id("Assets")
global.Element = array_create(0)
repeat(1){
    global.Element[_i] = layer_sprite_create(Layer,200,100,Sprite2)
    _i++
}
//step event
var _test = 0,
repeat(10000000){
    _test = layer_sprite_create(Layer,100,100,Sprite1)
    layer_sprite_destroy(_test)
}
show_debug_message(string(_test))
starting watch table = 1 element (instance), 1 element (sprite)
1638816443614.png
watch table after reach element id 1903481007
this element is visible in the room and can't be remove with layer_sprite_destroy
1638816586848.png
watch table after element id reach overflow
I can remove 3 elements -2147483648-46 with layer_sprite_destroy in watch table, this element was not removed in step code where is layer_sprite_destroy
(I think _test = layer_sprite_create (Layer, 100,100, Sprite1) returns -1 or something else)
Now I can't destroy layer sprite that was create in creation event id 2 is missing in layer_get_all_elements("Assets") and what I stored in global variable
Now I can't select instance element_id 0 that was in "Instances" layer and sprite in "Assets"" layer too. I cant remove sprite from room
I can access to instance with (instance_num).variable, but I can't use layer_instance_get_instance() .
1638816641681.png
watch table after reach element id -244000641 (it is same bitwise like 103481007 with -)
this element is visible in the room and can't be remove with layer_sprite_destroy
after reach next overflow now I have element id 0 and 1
I can remove element 0 and 1 with layer_sprite_destroy in watch table, this element was not removed in step code where is layer_sprite_destroy
(I think _test = layer_sprite_create (Layer, 100,100, Sprite1) returns -1 or something else)
1638817184158.png
after reach element id 1903481007 again I see 2 elements with same ID and I cant destroy it.

Question 1. Element_id 1903481007 and -244000641 is bug?
Question 2. How do I fix an overflow? The best thing for me is something like the internal GM2 variable element_id (last used id or next used id for element). I will check this id and I change it to the number I need.
 
Last edited:

Nidoking

Member
I'm going to suggest "Don't create and then destroy ten million sprites every step" as a potential workaround for this issue. I think that should hold you over until someone who actually works on the game can solve the problem in response to the help desk ticket you've no doubt created. You know, if you bend the stick because you want to see when it's going to break, you can't complain when it breaks.
 

TheouAegis

Member
So what would happen if you destroyed the layer at intermittent intervals? Does destroying a layer then recreating it reset the asset ID counter? Of course then the issue potentially arises where you have an overflow on layer IDs, but if it resets the asset counter, that gives you more room to work with. Furthermore, does restarting the room reset the asset counter? If so, then arguably all you need is a variable to keep track of how many assets have been created and then when you get too close to the Limit, restart the room. But I digress, this is purely hypothesis that needs testing.
 

trip

Member
You means destroy all layer? because element id is shared for all layers. Destroy all layer causes destroy all control objects. I can try it but I thing it will not reset element id counter. Restart near overflow is impossible, ok not impossible, but very wrong. In this case, my game will be playable for maybe 33 hours without a break. This is whole loading procedure for server and all clients its not comfortable solution.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Can you explain, in simple terms, WHY you think this is important to know? I'm trying to think of a single reason why any game would create (and require) more than 9223372036854775807 unique instance IDs or more than 1903481007 unique sprite IDs and I honestly can't. I mean, I can understand simple curiosity, but you seem to be implying that this is a problem for you and I'd love to know why? In all my years administering this forum and using GameMaker, I've yet to see an occasion when this is a problem... I mean, look at a game like Forager which has a massive amount of instances and stuff going on and they don't have any issues with "overflow"...

SO maybe if you outlined what you think you'll be doing that will make it a problem, then we can suggest alternative measures?
 

trip

Member
I wanted create all instances and layer_sprite when game is loaded. But many instances (some active and many inactive) causes lags when move, because I must activate/deactivate maybe 300 instances in 1 step (300 was not problem, problem was 200 000+ instances (active+inactive)). Solution was don't create all instances at start and create only visible instances (destroy instances outside view). But when I made this major change, I changed all the minor instances to layer_sprite. Bonus was more FPS.
Ok I changed instances for layer_sprite, but many created sprite causes drop FPS. Solution was stored ids and create/destroy maybe 300 layer_sprite in 1 step when move. Its work fine but now I need many ids and owerflow is problem.
First and best solution for me is the ability to change the element_id counter.
This is for element ids. Instances ids will sufficient maybe, because I use many layer_sprite and some instances now. I'm curious and I want to know the limits I can use.
 
Last edited:

trip

Member
I am here again :)
bug with element_id 1903481007 was reported.

But now I tested create instance. Below is test code.
GML:
with Object3{
    instance_destroy(self)
}
repeat(100000){
    Test = instance_create_layer(100,100,Layer,Object3)
}
I think Instances have 64 bit for his IDs this is 9223372036854775807 potential instances, but each instance consume 1 element_id then I can create only 2,147,483,647 instance, each next is created but is broken.
The Instances can't destroyed.
When element_id is overflowed and trying create instances with element id -2147383642 if exist instance 100006 then it fail . output message "Attempting to add instance -2147383642 multiple times to a layer"
1638953393830.png

I think This software is not create for permanent instances/layer_sprite and other elements :/
Is the finite number for all elements 2,147,483,647 ? When I reach this number then I must restart game?
Does anyone know where to read more about creating elements, allocating ids and similar technical stuff?
 

TheMagician

Member
I wanted create all instances and layer_sprite when game is loaded. But many instances (some active and many inactive) causes lags when move, because I must activate/deactivate maybe 300 instances in 1 step (300 was not problem, problem was 200 000+ instances (active+inactive)). Solution was don't create all instances at start and create only visible instances (destroy instances outside view). But when I made this major change, I changed all the minor instances to layer_sprite. Bonus was more FPS.
How far into development is your game? You say that you encountered lags when activating/deactivating around 200.000+ instances per Step. Did you hit that "limit" in the middle of the development process or are you still in the very early planning stages and trying out different approaches with a test project?

It sounds like your game is a large open-world multiplayer game and I agree that if games reach a certain scope you need to fully understand the technical underpinnings of your engine - sometimes even before you start development on the project. However, I'm still trying to think of a reason for your high asset count. Can you give some examples of objects that you are managing in the way you have described? Are we talking about individual building, cars, rocks, individual pieces of grass? Depending on your answer it feels to me that some of these things could be managed by one object that then uses the Draw Event to populate the visible part of the room while you are moving through the world?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
GML:
with Object3{
    instance_destroy(self)
}
This code does NOT do what you think it does... use "id" not "self", or simply omit the argument.
It sounds like your game is a large open-world multiplayer game and I agree that if games reach a certain scope you need to fully understand the technical underpinnings of your engine - sometimes even before you start development on the project. However, I'm still trying to think of a reason for your high asset count. Can you give some examples of objects that you are managing in the way you have described? Are we talking about individual building, cars, rocks, individual pieces of grass? Depending on your answer it feels to me that some of these things could be managed by one object that then uses the Draw Event to populate the visible part of the room while you are moving through the world?
This, 100%... It's what I was asking previously... an overview of what you are doing and why you think this is an issue. Not code, not examples, just a conceptual overview of what you are making.
 

trip

Member
Far from the beginning and far from the end in development :).
Not 200k activating/deactivating per 1 step, but was created 250k instances ,only 3k active instances and activating/deactivating around 300 instances per step. It is past. I use layer_sprite instead instances now.
Depending on your answer it feels to me that some of these things could be managed by one object that then uses the Draw Event to populate the visible part of the room while you are moving through the world?
hm... interesting idea I can try it and test it.
 

trip

Member
GML:
with Object3{
    instance_destroy(self)
}
This code does NOT do what you think it does... use "id" not "self", or simply omit the argument.
You right keyword self is redundant in my case and I remove it (I create it at night and I was tired), but Instance destroy work correctly with self. It is like use keyword other and it is possible.
This, 100%... It's what I was asking previously... an overview of what you are doing and why you think this is an issue. Not code, not examples, just a conceptual overview of what you are making.
I create large map (500x500 tiles maybe bigger) for multiplayer (10 players? maybe) not MMO. Max resolution is around 3 000 tile visible. 2,5D view.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I create large map (500x500 tiles maybe bigger) for multiplayer (10 players? maybe) not MMO. Max resolution is around 3 000 tile visible. 2,5D view.
Okay, then yes, look at creating a vertex buffer and adding all the assets into that (or sectioning the map into chunks with a v-buffer for each) then blitting the buffer(s) to the screen each draw step. This is REALLY fast as it's all GPU based and not CPU dependent. See this from @matharoo :
 
Top