GetSpriteIndex

From ZDoom Wiki
Jump to navigation Jump to search
Note: This feature is for ZScript only.


Actor

native static int GetSpriteIndex(name sprt)

Usage

Returns a SpriteID for the provided four letters of a sprite name. Can be used to modify the actor's sprite dynamically, by setting the result value to the actor's sprite field.

Error.gif
Warning: In order to be able to modify an actor's sprite directly, the SpriteID in question must be loaded into memory. For that to happen, the sprite name must be defined somewhere in an actor. It can be any actor, and the actor in question doesn't have to spawn, it just needs to be defined somewhere in the code.

Only sprite name is relevant, while frame letters are not: for example, using ZOMB A 0 will load all sprites that start with "ZOMB" into memory.


Example

This torch will randomly take an appearance of RedTorch, BlueTorch or GreenTorch. The sprite names are stored in a static array and the sprite is set in PostBeginPlay.

class RandomTallTorch : RedTorch 
{
    static const name torchSprites[] = 
    {
        'TRED',
        'TBLU',
        'TGRN'
    };

    override void PostBeginPlay() 
    {
        super.PostBeginPlay();
        sprite = GetSpriteIndex(torchSprites[random(0, torchSprites.Size()-1)]);
    }

    States 
    {
    Spawn:
        #### ABCD 4 bright;
        loop;
    }
}

This is a variation of the same example that also uses A_AttachLightDef to attach a dynamic light to the torch:

class RandomTallTorchWithALight : RedTorch 
{
    static const name torchSprite[] = 
    {
        'TRED',
        'TBLU',
        'TGRN'
    };
    static const name torchLight[] = 
    {
        'BIGREDTORCH',
        'BIGBLUETORCH',
        'BIGGREENTORCH'
    };
    override void PostBeginPlay() 
    {
        super.PostBeginPlay();
        int i = random(0,2);
        sprite = GetSpriteIndex(torchSprite[i]);
        A_AttachLightDef("0",torchLight[i]);
    }
    States 
    {
    Spawn:
        #### ABCD 4 bright;
        loop;
    }
}