• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Finding unused sprites and objects

M

maverick77

Guest
Hi there,

I've been working on a GMS 1.4 project for a while. During that time I've created a number of sprites and objects within the project. As it's evolved and changed, I've subsequently deleted various objects from the game, and ultimately not used them.

Is there a way to auto search a project for sprites and objects that are in the project but not used? I want to tidy it up prior to publishing on Google Play, which has a 50MB APK limit, and mine is a little larger than this - I'm guessing to all this extra bloat! Manually hunting all the levels and GML code to make sure a object (and sprite) is not in use may be a little painful and time consuming!

Cheers!
 
N

NeonBits

Guest
I don't think so.... You could create a "level folder" for sprites and objects and add each item to their respective level. In the end, you will find what's not needed.
 

Mick

Member
I have made a tool for that purpose that I never released, I have used it personally successfully. Do you want to test it? To be safe, you would have to run it on a backup of your project. It can find assets that are no longer in the project folder, but it can also find assets not referenced anywhere else in the project.
 

Mick

Member
Well here it is anyway, use at your own risk. :) Not much harm can be done as long as you take a backup of your project first.

Download it here: https://gamephase.net/files/GMS14ProjectCleaner.zip

EDIT: Just tried it on Thy Sword, that project has a life of over three years and is quite big. I had cleaned up the project at some point but running this tool now it found 260 obsolete and unused resources, lol. I had to run the "find unused resources" three times. The project compiled without a problem after removing these resources with the tool.


INSTRUCTIONS

Choose GM:S Project

Select a GMS1.4 project to work with. Take a backup of your project first! The tool will create a folder named "_backup" where the removed resources can be found. You can remove that folder manually, it's just created as a precaution.

Find Obsolete Resources
This will find resources in the project folder that doesn't exist in the project file. These should always be safe to remove.

Find Unused Resources
This will find resources that exist in the project file but are not referenced elsewhere (in code etc.). This process will take a while in a big project (a minute or so). After removing unused resources once, you might want to do it again (until no more unused resources are found), because there might be more unused resources now that were only used in the now removed resources.
 
Last edited:

COWCAT

Member
I would love for a GMS 2 version of this.
I make super big games and I'm sure there's lots of unused stuff. Trying it on my old 1.4 project and it's taking ages to find anything XD
 
Well here it is anyway, use at your own risk. :) Not much harm can be done as long as you take a backup of your project first.

Download it here: http://gamephase.net/files/GMS14ProjectCleaner.zip

EDIT: Just tried it on Thy Sword, that project has a life of over three years and is quite big. I had cleaned up the project at some point but running this tool now it found 260 obsolete and unused resources, lol. I had to run the "find unused resources" three times. The project compiled without a problem after removing these resources with the tool.


INSTRUCTIONS

Choose GM:S Project

Select a GMS1.4 project to work with. Take a backup of your project first! The tool will create a folder named "_backup" where the removed resources can be found. You can remove that folder manually, it's just created as a precaution.

Find Obsolete Resources
This will find resources in the project folder that doesn't exist in the project file. These should always be safe to remove.

Find Unused Resources
This will find resources that exist in the project file but are not referenced elsewhere (in code etc.). This process will take a while in a big project (a minute or so). After removing unused resources once, you might want to do it again (until no more unused resources are found), because there might be more unused resources now that were only used in the now removed resources.
Hi Anyway you can update this for Gamemaker Studio 2 latest release please and thank you.
 

renex

Member
i have a set of scripts for detecting unused sprites in gm8, but i've been considering making a more proper standalone tool for a while. if i make it, i'll let you guys know.
 
As this thread still seems to have some interest, I will explain how I would approach doing this.

Whenever you create a project in GMS 1, you end up with a collection of folders and GML files. These files are the template for how your project is constructed, and can be read by software like Notepad, or you can even use GMS to make your own tool that will read the contents. No matter how the information is presented (GMS 2 are JSON, whereas GMS 1 is XML..??) these are all just text files, and GMS has the functionality to read and write them.

STEP ONE:
Read the 'Object' directory of the project

For each object you will see a GML document, and it will have this line of text:

GML:
 <spriteName>&lt;undefined&gt;</spriteName>
This is what you will see if there is no sprite attached to the object ( &lt;undefined&gt; )

If it had a sprite, you would see the name shown here instead.

We want to read this line, and remove the start and end brackets to find the content.

Rough code (this is the basic gist only):

Code:
str = file_text_readln(file)
str = string_replace(str, '<spriteName>', '""); // delete string section, and leave empty 
str = string_replace(str, '</spriteName>', ""); // delete string section, and leave empty
This would leave just what was between the brackets.

By reading that edited content as a string, we can get it's name (or not). We can ignore it if it has no sprite, or if it has one: save that sprites name to a data structure.

STEP TWO:
We want to know if there is any manual attachment of a sprite, that happens after creation and where it isn't done through the UI / engine. To do this, you have to read and parse all of your code. This isn't quite so straightforward, but it is doable.

Open up the file and read it line by line as a string. Whilst doing this you would use the "string_pos" function to see if there is the presence of code that sets up sprites ("sprite_index" / "draw_sprite" etc)

This would take more effort, but be simpler than trying to find each word in a line, and parsing it. You could try to do each one, and then see if it is actually a resource, and then if it's a sprite. But by doing it this way (looking for any possible coding way of allocating a sprite) you can be much more precise in your searching.

If "string_pos" does find the presence of these strings, then you parse that content and find the sprite name. Then add it to a data structure that holds all of those sprites that were used.

FINALLY:
Having done steps one and two you should now have a list of all the sprites actually used in your project (assuming I haven't overlooked an area where sprites may be called, such as shaders, or even timelines? if so: it's just another file to be read and parsed in the same fashion)

The last step is then editing the project files, and removing any sprites that aren't found on our compiled ACTUAL list.

This is what you will see in the GMX project file (whatever is the name.project)

Code:
<sprites name="sprites">
    <sprite>sprites\prop_mask</sprite>    // is this present? yes / no - delete if not
    <sprite>sprites\sprite1</sprite>     // is this present? yes / no - delete if not
    <sprite>sprites\sprite4</sprite>    // is this present? yes / no - delete if not
    <sprite>sprites\sprite3</sprite>  // etc
    <sprite>sprites\sprite5</sprite> // etc
    <sprite>sprites\sprite6</sprite> // etc
  </sprites>
Remove here any that aren't used, and then delete them from the sprites directory too.

And there you go: How you can make a "clean up" tool for your projects. It is all just text files to be read and edited, and is what GMS does behind the scenes whenever you alter a projects contents. If you know what you're doing, it's perfectly possible to do it yourself, and make useful utilities that cover some things GMS 1 lacks. GMS 2, for example, has a much more advanced text editor, and this can be replicated in GMS 1 in a not too dissimilar fashion.
 
Last edited:
Top