• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Change asset layer sprites in game

Trandoxiana

Member
So, I have a scrolling asset layer that I use as a background (on top of a solid color background layer). It's just some rotated squares that I manually placed in the room editor. They are currently a light blue, but I want to be able to change that color when in another room. I am not sure this is the right way to go about this, but what I've been trying to do is use layer functions to change their color based on what room it is in. (I would be doing this through a persistent controller object.) I first tried this:
GML:
var lay_id = layer_get_id("Squares");
var spr_id = layer_sprite_get_id(lay_id, "spr_diamond_backround");
layer_sprite_blend(spr_id, c_red);
and then this:
GML:
var lay_id = layer_get_id("Squares");
var spr_id = layer_sprite_get_id(lay_id, "spr_diamond_backround");
layer_sprite_index(spr_id, 1);
But neither worked. (For the second one I added another frame to the sprite.) It did not seem that either had any effect on the sprites. I know the reason this is so difficult is because it is an asset layer and they are not objects that I can just put code into, but I had made it this way to conserve resources. Should I just make them all objects? Should I not worry about performance issues? I don't have any currently, but I am conscious of creating an excessive amount of objects. Or is there another solution that I am missing?

P.S. This is my first post here, so sorry if I messed something up
 

angelwire

Member
You're on the right track! The problem is that Gamemaker needs to know exactly which sprite elements on the layer need to be changed (not just the sprite name from the asset browser).
So when it asks for the "asset name" in layer_sprite_get_id it's asking for the name that the room editor gives the sprite. Here's a screenshot of where to find the "asset name" in the room editor:
example.png

Now this means you have two options:
Keep track of every asset name on the sprite layer and change them each individually with layer_sprite_get_id(lay_id, "graphic_6FA0543")layer_sprite_get_id(lay_id, "graphic_530455AC") for every single sprite in the layer.
Or you could use layer_get_all_elements("Squares") to get an array of all the sprites on the layer and then loop through them to change the blend color.

If you have more than 5 or 6 sprites then I'd definitely recommend the second option.
 

Trandoxiana

Member
Thank you so much! That is exactly what I needed. I do have a number of squares, so I will be using layer_get_all_elements()
 
Top