GMS 2 [HELP]How to use function "draw_sprite_pos" to draw a part of sprite ?

mather12

Member
I need to draw a random part of a random sprite and use "draw_sprite_pos" to twist it.What should I do?plz.
 
Last edited:
1) You want to get a sprite index: to do that randomly you need to know how many sprites there are. Off the top of my head I don't know if there is a function that tells you that. If not, then I think you could do something like this:

begin_pos = 0;
while(sprite_exists(begin_pos))
{begin_pos += 1;}
being_pos -= 1;

begin_pos should end up being the same amount as there are sprites. Though it has one removed from its value, as the sprite indexes start at 0. If you have 13 sprites, then the last sprite is numbered 12.

2) set sprite_index (or just get the index of a sprite) to random number between 0 and begin_pos, using irandom_range

3) get sprite width and sprite height of random sprite

4) use those values for further irandom_range results, and get the first x / y position of what will be drawn

5) then use lengthdir_x / y to define the next point, and then the next point from the previous, and so on, so you can draw and manipulate the other corners.

Assuming that is correct, then looking up those functions in the manual should get you on the right track
 
Last edited:

Yal

šŸ§ *penguin noises*
GMC Elder
There's draw_sprite_part() but it only draws axis-parallel rectangular parts, so it depends a bit on how you want the part to work.
 
I was wrong about some things, as you can't use draw_sprite_pos for an original sprite. Since it is based around using the whole sprite, rather than part of it.

The code below does work to find out how many sprites there are in total:

Code:
begin_pos = 0;
while (sprite_exists(begin_pos))
{begin_pos += 1;}

begin_pos -= 1;
And this code below could be used, depending on how you go ahead. For example, you might create a new sprite from these dimensions and then could manipulate it with draw_sprite_pos. This should ensure you never create a sprite that is less than one pixel in width / height:
Code:
is_sprite = irandom_range(0, begin_pos);
is_width = sprite_get_width(is_sprite);
is_height = sprite_get_height(is_sprite);
start_x = irandom_range(0, is_width - 1);
start_y = irandom_range(0, is_height - 1);
end_x = irandom_range(start_x, is_width);
end_y = irandom_range(start_y, is_height);
Though creating a new sprite makes a new texture page, which is I think less efficient than using part of the sprite as a textured primitive (as flyingsaucerinvasion suggests)

If you went with a textured primitive, then it would be a bit different as the scale (what would be the sprite width / height part previously) goes from 0 to 1:
Code:
is_sprite = irandom_range(0, begin_pos);
one_pixel_width = 1 / sprite_get_width(is_sprite);
one_pixel_height = 1 / sprite_get_height(is_sprite);
start_x = random_range(0, 1 - one_pixel_width);
start_y = random_range(0, 1 - one_pixel_height);
end_x = random_range(start_x, 1);
end_y = random_range(start_y, 1);
I'm not sure about the above code, as irandom_range returns a whole number (0, or 1, in this case) - so it can't be used here, as the scale goes from 0 to 1 on a texture. random_range returns a number that isn't whole, so I think it is the one to use (0.1, 0.05, 0.72 etc) instead. If its correct then it should never draw anything less than one pixel in width / height of the sprite, and will be randomly picking what part is drawn. Then to manipulate the primitive you would use these dimensions (what part is drawn) with something like lengthdir_x / y to control the points (where its drawn)
 
Top