The "sprite_get_index" Function

Syntax

sprite_get_index(sprite, [offset]);
ArgumentTypeDescription
spritespriteThe sprite to retrieve image index of
[offset]realOptional: Sets the number of frames to offset the sprite index

Description

Every instance has a built-in image_index variable which tracks the animation frame for the sprite assigned to sprite_index, adjusted for image_speed. But many objects draw multiple sprites, each of which may have a different speed. This script returns the image_index value for any sprite, factoring in both sprite speed and delta time.

Note that this script is based on global session time, and will always return the same index at the same time for every instance of the sprite. Sometimes this synchronization is not desirable, in which case an optional offset time can also be supplied, as a value of frames. For example, to base starting time on instance creation time, use -sprite_get_index() (must be a variable declared in an event that is not run every Step).

Example

//CREATE EVENT
var offset = -sprite_get_index(my_sprite);
//DRAW EVENT
draw_sprite(my_sprite, sprite_get_index(my_sprite, offset), x + 32, y - 32);