SOLVED How can I get (instance_number) for all objects in a room.

Hello!

I'm a complete amateur and have "some" questions.

1. What is the difference between instance_count and instance_number ? Is it the same thing but for different versions of gm? or are they completely different functions?

2. How could I get instance_number for every object in the room and save it to a file with just 1 keypress?

3. Do I need to code it manually for each room or is there a better way of doing it? (example:
if (keyboard_check(ord"C"))
{
// get amounts
var1 = instance_number(obj_1);
var2 = instance_number(obj_2);
// save to txt file
(code for saving to a text file
}

4. Do I even need a script to check that? Maybe there's a simplified way I don't know of?
I haven't used the debugger for anything else but the step % so I could determine which objects take the longest to complete, but I was wondering if they had
something in there already to get the instance_number of all active objects currently in the room? Because I couldn't find it myself.

Thank you in advance!
 

TsukaYuriko

☄️
Forum Staff
Moderator
1)
instance_count is a variable, not a function, and it contains the amount of active instances in the room. That is, instances of any object.
instance_number is a function that returns the amount of active instances of a specific object in the room.

2)
If you want this to be split per object, you could add all objects you want to count the instances of to an array, iterate over that array, call instance_number on the currently iterated object and save the results along with something that identifies the object, such as object_get_name.
If you don't want to manually specify objects to count, you could instead iterate over all instances (with (all)) and use something like an array or a map to keep track of how many of each object index exists. You can then iterate over that data structure and write the results to a file.

If you don't want it to be split, instance_count.

3)
If you put this code somewhere where you can re-use it - e.g. not in an object that is present only in one specific room, but a global controller, or a function declared in a script which is then called by a global controller - then you don't have to code it manually for each room, but just once.

4)
Using a script is not strictly necessary. You could also put it in a function that's declared in an object, or even outside of any function at all and just sitting in an object's Key Press or Step event.
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
1. What is the difference between instance_count and instance_number ? Is it the same thing but for different versions of gm? or are they completely different functions?
One is a function and the other is a variable. The variable is ALL th instances in the current room, while the function can return the number of specific instances of an object, although you can use "all" instead of an object index to get all instances in the room.

2. How could I get instance_number for every object in the room and save it to a file with just 1 keypress?
GML:
if (keyboard_check(vk_space))
{
var _num = instance_count;
// save to txt file here
}
Do I need to code it manually for each room or is there a better way of doing it?
Yes, see the code above. Although you could also create a script that attempts to get all the instances in the game using the layer functions for rooms other than the room you are in, but that would be much more complicated.

4. Do I even need a script to check that? Maybe there's a simplified way I don't know of?
Normally, I simply just have a draw_text call in a controller object that draws the instance_count value to the screen. :)
 
Top