Resource Counting

In big GM projects that you work on, you are bound to have many sprites, sounds, scripts, instances etc. in your GM6/GMK file. It would take ages to count all resources by yourself, so here’s a tutorial that shows you how you can easily count these resources much faster using one script for each resource. You can find the scripts and an example of how to use it in the ‘Resources’ folder. We recommend you to keep this example opened while you read this tutorial, or to try to figure this out by yourself.

Let’s have a look at the script that counts the sprites:

// arg0 = max index check gvar TOTALSPRITES; TOTALSPRITES=0; for (i=0;i<argument0;i+=1) {    if sprite_exists(i) {      TOTALSPRITES+=1 }   }    return TOTALSPRITES;

This script is not as complicated as it might seem for some users. First it defines the variable “TOTALSPRITES”. If i (the number of sprites) is less than argument0 (the maximum number of sprites to count), it adds one up if there is one more sprite.

For all other resources except for the lines in a script, this script works exactly the same.

The example also holds a script to count the lines in all scripts in your game. This script is not much different than the other scripts, except that it saves all scripts in a text file to count the number of lines. After it’s done counting, the text file is deleted again.

Here is the script that counts the lines of all scripts together.

var MAXCHECK,TOTALLINES; MAXCHECK = 1000 TOTALLINES=0; SCRIPTFL=file_text_open_write("LNCOUNT.txt") for (i=0;i<MAXCHECK;i+=1) {   if script_exists(i) {   sctext=script_get_text(i); file_text_write_string(SCRIPTFL,sctext); file_text_writeln(SCRIPTFL); }   }    file_text_close(SCRIPTFL); SCRIPTFL=file_text_open_read("LNCOUNT.txt"); do   { TOTALLINES+=1; file_text_readln(SCRIPTFL); }   until file_text_eof(SCRIPTFL) file_text_close(SCRIPTFL); file_delete("LNCOUNT.txt"); return TOTALLINES

If you study this script for a short while together with the other script, you can see that the difference, although it might look so, is not so big. This script only counts the lines of actually scripts. With a bit of modification you could also count the lines of code in all the objects to. You could take this script one step further and count that characters in the script also. However, depending on the size of the script this may be a slow process.

It is recommend that if you do use this script, that you only use it in the development process. This is because if you released this script in your game/program it would export all the scripts in it. Even if you do not actually run the script in your game/program a user may find a way to execute this script.

Now, let us look at how to connect this script to an instance, so that the number of a specific resource can be shown in a message box.

mic=get_integer("Enter Max Index Check:(this doesn't count for scriptlines)","") op=show_menu(" Sprite| Object| Sound| Background| Path| Script| Font| Timeline| Room| Scriptlines","") switch op   { case 0:{show_message(string(sprite_count(mic)))}break; case 1:{show_message(string(object_count(mic)))}break; case 2:{show_message(string(sound_count(mic)))}break; case 3:{show_message(string(background_count(mic)))}break; case 4:{show_message(string(path_count(mic)))}break; case 5:{show_message(string(script_count(mic)))}break; case 6:{show_message(string(font_count(mic)))}break; case 7:{show_message(string(timeline_count(mic)))}break; case 8:{show_message(string(room_count(mic)))}break; case 9:{show_message(string(scriptlines_count))}break; }

This piece of code goes in the mouse left pressed event of a button object that you need to put in the room. The variable op is the resource that will be counted. It gives you a menu so that you can decide which resource you wish to count. After that, in the message it runs the specific script and brings you the number of resources. The variable mic is the maximum number of resources to count, so if the number of resources is higher than the value of the mic variable, the difference won’t be counted with it.