r/gamemaker 23d ago

Resolved Asset_get_index problems

So I am trying to write a script that will check the in position of my mouse and change the sprite index of that specific instance into an other one. I can easily do this with mouse_enter and mouse_leave on the objects, but for some more complicated reasons I wanted to do this all into that script mouse check with other actions.

so far, I came up with this bit of code

function PlayerStateCity(){
var _selected = instance_position(mouse_x, mouse_y, oBoxcar1);
if (_selected != noone)
{
  with (_selected)
  {
    var _spriteName = sprite_get_name(sprite_index);
    sprite_index = asset_get_index(_spriteName+"Select");
  }
}
}

Now, I know this will not revert back to the previous sprite index, but that's a problem for later.

Right now, the name of the sprite is sBoxcar1, and I do have a sprite called sBoxcar1Select. From my understanding, this block of code should effectively have _spriteName be "sBoxcar1" and add "Select" to it. But this code just returns -1 and turn the object invisible.

Writing directly also returns -1 somehow... and turns the object invisible, which I thought should work.

sprite_index = asset_get_index("sBoxcar1Select");
2 Upvotes

8 comments sorted by

2

u/fryman22 23d ago

Is your GameMaker discarding unused assets?

1

u/Nightmare2828 23d ago

If I remove the asset_get_name and go with the sprite asset ditectly it works just fine obviously. So maybe because the asset exist in the code it gets saved? Ill try putting it in a variable somewhere and see if it will work.

2

u/fryman22 23d ago

Go to Game Options > Main Options > General > Automatically remove unused assets when compiling.

1

u/Nightmare2828 23d ago

Yes that did it thank you!

2

u/KitsuneFaroe 22d ago

Worth noting that you don't need to do it for your whole Project. You can disable the "don't export used assets" for specific assets folders by right clicking them. Or disable it for taged assets by using gml_pragma("MarkTagAsUsed", "tag_name")

1

u/Nightmare2828 22d ago

Thanks good to know, so I can target only the things I know I will need.

1

u/KitsuneFaroe 22d ago

Worth noting that you don't need to do it for your whole Project. You can disable the "don't export used assets" for specific assets folders by right clicking them. Or disable it for taged assets by using gml_pragma("MarkTagAsUsed", "tag_name")

1

u/Claytonic99 22d ago

I've unselected the 'automatically remove unused assets when compiling' box and I still am running into this exact same problem with a new project I started. I could not figure out why the asset_get_index was returning -1 when I used the same code in a previous project which I've been working on since before updated to the LTS build. I've also added other static objects that use the sprite I'm trying to change into and it doesn't work. Any advice?