GMS 2 Help with sprite change on asset layer

rawket

Member
So in an object I have this code for room start
if areas_coastal == "burned"
{
layer_sprite_change(layer_sprite_get_id("buildings", "spr_diner1"), spr_diner1Burned);
}
It runs but the sprite doesn't change
 
Last edited:
layer_sprite_get_id takes the Id of the layer, not the name. You will need to use layer_get_id("buildings") to get the layer id to then use in layer_sprite_get_id.
 

rIKmAN

Member
If you check the manual page for the layer_sprite_change function you will see that the first argument is an id and the second argument is a sprite index - not strings - so use an id (like @BaBiA Game Studio mentioned) and remove the quotes.

Get into the habit of checking the manual for information on functions when they don’t appear to work, it should be your best friend and first port of call when starting out.

You can middle click the function name in the code editor to be taken straight to the manual page for that function which gives an explanation and a code example showing how to use it (which would have shown your mistake if you compared your code against the example).
 

rawket

Member
If you check the manual page for the layer_sprite_change function you will see that the first argument is an id and the second argument is a sprite index - not strings - so use an id (like @BaBiA Game Studio mentioned) and remove the quotes.

Get into the habit of checking the manual for information on functions when they don’t appear to work, it should be your best friend and first port of call when starting out.

You can middle click the function name in the code editor to be taken straight to the manual page for that function which gives an explanation and a code example showing how to use it (which would have shown your mistake if you compared your code against the example).
I did, the quotes were kinda a desperate attempt cuz going by the help page got me the same result

layer_sprite_get_id takes the Id of the layer, not the name. You will need to use layer_get_id("buildings") to get the layer id to then use in layer_sprite_get_id.
I had it like that to begin with and still no change this iteration was kinda a reach but maybe I was just tired. I'll give it another go

So in an object I have this code for room start
if areas_coastal == "burned"
{
layer_sprite_change(layer_sprite_get_id("buildings", "spr_diner1"), spr_diner1Burned);
}
It runs but the sprite doesn't change
Update just for testing to see if it's working as a standalone bit of code I now have it written like this

var layer_id = layer_get_id("buildings");
var sprite_id = layer_sprite_get_id(layer_id, "spr_diner1");
layer_sprite_change(sprite_id, spr_diner1Burned);

exactly structured like the help page, and it's still not changing the sprite
 
Last edited by a moderator:
Try putting the code in a click or key event instead. Perhaps the object Room Start event is happening before everything is being put in the room on all the layers, so it does not find anything. Also throw in some show_debug_message calls or use the debugger to see what the values of those variables are being returned as, just to see if it is in fact finding the layer id and the sprite id.
 

rawket

Member
Try putting the code in a click or key event instead. Perhaps the object Room Start event is happening before everything is being put in the room on all the layers, so it does not find anything. Also throw in some show_debug_message calls or use the debugger to see what the values of those variables are being returned as, just to see if it is in fact finding the layer id and the sprite id.
Maybe but before I tried this way I had it hiding the layers and it worked and I have some other code it's running in reference to the layers and they worked. Is it just that hiding the layer prevents that from being shown weather it was placed yet or not?
Either way I'm still gonna give it a go. Is it possible that putting it on an alarm would work? only then you would see a flash wouldn't you?

Try putting the code in a click or key event instead. Perhaps the object Room Start event is happening before everything is being put in the room on all the layers, so it does not find anything. Also throw in some show_debug_message calls or use the debugger to see what the values of those variables are being returned as, just to see if it is in fact finding the layer id and the sprite id.
Alright I set it up on a one step alarm and it still isn't working.

Try putting the code in a click or key event instead. Perhaps the object Room Start event is happening before everything is being put in the room on all the layers, so it does not find anything. Also throw in some show_debug_message calls or use the debugger to see what the values of those variables are being returned as, just to see if it is in fact finding the layer id and the sprite id.
Ok idk how to use the debugger so instead I had it draw some text and the layer id was like 56 and the sprite id is -1. I don't think either of those are correct, the sprite id is DEFINITELY not correct and idk if layer id counts up all the layers loaded in the game overall but there aren't that many in the room
 
Last edited by a moderator:
Okay, I have figured it out. It is because you do not use the sprite name as you see it in the resource tree. You have to use the name that it gets given in the Room editor, eg: graphic_2E82C1DF
Code:
var layer_id = -1;
layer_id = layer_get_id("buildings");
var sprite_id = -1;
sprite_id = layer_sprite_get_id(layer_id, "graphic_2E82C1DF");  // this is the name that you would change to match the one in the Room editor
layer_sprite_change(sprite_id, spr_diner1Burned);
The layer id appears to be working fine as it is giving you an id (56), but because you are using the sprite name in the resource you get -1 as it does not find that asset on the layer as it is changed to the name in the Room editor.

There is another way to do what you want, and that is to iterate through the elements on the layer and if they are a sprite and of the right resource/asset name (as seen in the Resource tree) then you can change them. See the code below:
Code:
var layer_id = -1;
layer_id = layer_get_id("buildings");
var a = layer_get_all_elements(layer_id);
for (var i = 0; i < array_length_1d(a); i++;)
{
   if layer_get_element_type(a[i]) == layerelementtype_sprite
   {
        if (sprite_get_name(a[i]) == "spr_diner1") {
            layer_sprite_change(a[i], spr_diner1Burned);
        }
   }
}
 
  • Like
Reactions: jms

rawket

Member
Okay, I have figured it out. It is because you do not use the sprite name as you see it in the resource tree. You have to use the name that it gets given in the Room editor, eg: graphic_2E82C1DF
Code:
var layer_id = -1;
layer_id = layer_get_id("buildings");
var sprite_id = -1;
sprite_id = layer_sprite_get_id(layer_id, "graphic_2E82C1DF");  // this is the name that you would change to match the one in the Room editor
layer_sprite_change(sprite_id, spr_diner1Burned);
The layer id appears to be working fine as it is giving you an id (56), but because you are using the sprite name in the resource you get -1 as it does not find that asset on the layer as it is changed to the name in the Room editor.

There is another way to do what you want, and that is to iterate through the elements on the layer and if they are a sprite and of the right resource/asset name (as seen in the Resource tree) then you can change them. See the code below:
Code:
var layer_id = -1;
layer_id = layer_get_id("buildings");
var a = layer_get_all_elements(layer_id);
for (var i = 0; i < array_length_1d(a); i++;)
{
   if layer_get_element_type(a[i]) == layerelementtype_sprite
   {
        if (sprite_get_name(a[i]) == "spr_diner1") {
            layer_sprite_change(a[i], spr_diner1Burned);
        }
   }
}
OMG thank you! It works. Finally it works!
 
  • Like
Reactions: jms

rIKmAN

Member
Okay, I have figured it out. It is because you do not use the sprite name as you see it in the resource tree. You have to use the name that it gets given in the Room editor, eg: graphic_2E82C1DF
That’s exactly what it says on the on the manual page for layer_sprite_get_id
I did, the quotes were kinda a desperate attempt cuz going by the help page got me the same result
If you’d worked backwards and checked the manual entry for each function you used in that small block of code you would have seen the information above and realised you were using layer_sprite_get_id incorrectly.

The code example alone doesn’t obviously show this but the function description and argument description explain it pretty clearly - like I said, make it your best friend.

Also you can multi-quote messages from different people like I have here by hitting the +quote button on all the posts you want to reply to, then clicking “Insert Quotes” below the reply box and adding your replies between the quoted blocks,

It saves you having to make separate 3 posts one after the other to reply to each person.
 
Last edited:

TheouAegis

Member
Or you can click REPLY on each post, assuming you're quoting the whole post rather than multi-quoting from one post.
That’s exactly what it says on the on the manual page for layer_sprite_get_id

If you’d worked backwards and checked the manual entry for each function you used in that small block of code you would have seen the information above and realised you were using layer_sprite_get_id incorrectly.

The code example alone doesn’t obviously show this but the function description and argument description explain it pretty clearly - like I said, make it your best friend.

Also you can multi-quote messages from different people like I have here by hitting the +quote button on all the posts you want to reply to, then clicking “Insert Quotes” below the reply box and adding your replies between the quoted blocks,

It saves you having to make separate 3 posts one after the other to reply to each person.
OMG thank you! It works. Finally it works!
Maybe but before I tried this way I had it hiding the layers and it worked and I have some other code it's running in reference to the layers and they worked. Is it just that hiding the layer prevents that from being shown weather it was placed yet or not?
Either way I'm still gonna give it a go. Is it possible that putting it on an alarm would work? only then you would see a flash wouldn't you?


Alright I set it up on a one step alarm and it still isn't working.


Ok idk how to use the debugger so instead I had it draw some text and the layer id was like 56 and the sprite id is -1. I don't think either of those are correct, the sprite id is DEFINITELY not correct and idk if layer id counts up all the layers loaded in the game overall but there aren't that many in the room
:p
 

rawket

Member
That’s exactly what it says on the on the manual page for layer_sprite_get_id

If you’d worked backwards and checked the manual entry for each function you used in that small block of code you would have seen the information above and realised you were using layer_sprite_get_id incorrectly.

The code example alone doesn’t obviously show this but the function description and argument description explain it pretty clearly - like I said, make it your best friend.

Also you can multi-quote messages from different people like I have here by hitting the +quote button on all the posts you want to reply to, then clicking “Insert Quotes” below the reply box and adding your replies between the quoted blocks,

It saves you having to make separate 3 posts one after the other to reply to each person.
Sorry, I just misunderstood stuff on the help page.
Or you can click REPLY on each post, assuming you're quoting the whole post rather than multi-quoting from one post.




:p
And this is the first I'm hearing about this. I'm still pretty new to actually using the forum
 

rIKmAN

Member
Sorry, I just misunderstood stuff on the help page.
No need to apologise, just trying to advise you of good habits to get into to save your own time and speed up your progress.

Learning how to learn is a skill in itself, and learning how to effectively use the manual will save you loads of time waiting on replies to questions that are either answered in the manual or have likely been asked/solved before - which is where expanding to Google searching and the forum search at the top right will also come in handy for you.

The forum search bar doesn't like 3 letter words (or smaller) but Googling "gamemaker <my issue here>" will usually list threads on here amongst the answers, and you can use Google to specifically search just the forums by adding site:forum.yoyogames.com to the end of your search query.

As an example Google search for "platformers" just on these forums: platformers site:forum.yoyogames.com

Welcome to the community!
 

rawket

Member
No need to apologise, just trying to advise you of good habits to get into to save your own time and speed up your progress.

Learning how to learn is a skill in itself, and learning how to effectively use the manual will save you loads of time waiting on replies to questions that are either answered in the manual or have likely been asked/solved before - which is where expanding to Google searching and the forum search at the top right will also come in handy for you.

The forum search bar doesn't like 3 letter words (or smaller) but Googling "gamemaker <my issue here>" will usually list threads on here amongst the answers, and you can use Google to specifically search just the forums by adding site:forum.yoyogames.com to the end of your search query.

As an example Google search for "platformers" just on these forums: platformers site:forum.yoyogames.com

Welcome to the community!
I usually do try to look up my issues, but I have gotten to know what sorts of things I know how to say and this was the sort of thing that I was gonna have a hard time finding outside of the help page just cuz I suck at words. Sometimes (not this time) but sometimes I feel like I know game maker code better than english. So I just felt like it was gonna be easier for me to find help by making a post in my own words than doing what I normally do which is searching for hours hoping Ima find what oddly specific problem I'm trying to solve
 

TheouAegis

Member
That's kind of where Google comes in. On the forums, it's pretty strict about what you search for. Google, on the other hand, was built around people not knowing how to speak English, so it often searches for synonyms of what you type as well. Your go-to sources are going to be the manual - which will usually pop up as a Google hit, this forum, and Reddit. Oh, and I think Steam has its own forum too. Unfortunately you still need to know if you are working with gm8, gms1, or gms2 and need to have some idea of what code is valid with your version, so Google isn't without its faults either.


This thread is solved now, right?
 

rawket

Member
That's kind of where Google comes in. On the forums, it's pretty strict about what you search for. Google, on the other hand, was built around people not knowing how to speak English, so it often searches for synonyms of what you type as well. Your go-to sources are going to be the manual - which will usually pop up as a Google hit, this forum, and Reddit. Oh, and I think Steam has its own forum too. Unfortunately you still need to know if you are working with gm8, gms1, or gms2 and need to have some idea of what code is valid with your version, so Google isn't without its faults either.


This thread is solved now, right?
Well google is where I go first. I just can never find the answer I'm looking for. I get results that in an extremely aggravating fashion, sound right, but are way off the mark
 
Top