Dragonfly Cloud is now available on the AWS Marketplace - Learn More

Question: How can I find out how many instances of an object exist in GameMaker?

Answer

To count the number of instances of a particular object in GameMaker, you can use the function instance_number(object_index) where object_index is the index of the object you want to count. This function returns an integer value that represents the total number of instances of the specified object that currently exist in the room.

Here's an example of how you might use this:

// Count the number of instances of obj_Enemy var enemy_count = instance_number(obj_Enemy); // Display the count in the debug console show_debug_message("There are " + string(enemy_count) + " enemies in the room.");

If you need to check the number of instances across all objects, you can use the special built-in variable instance_count. This variable holds the total number of instances of all objects in the current room.

For example:

// Get the total number of instances in the room var total_instance_count = instance_count; // Display the count in the debug console show_debug_message("Total number of instances in the room: " + string(total_instance_count));

Keep in mind that these functions and variables provide a count at the moment they are called; if instances are created or destroyed during gameplay, the numbers will change accordingly.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost