1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

(solved) sprite_add and asset_get_index

Discussion in 'Programming' started by JFitch, Jan 26, 2018.

  1. JFitch

    JFitch Member

    Joined:
    Sep 28, 2016
    Posts:
    428
    If you create a sprite using sprite_add, is it possible to access it using asset_get_index?

    I'm making a game in which the characters that are only available through in app purchases will be stored online so the game won't waste memory on them when they're not usable anyway.

    The sprites are named so the character's name, direction (0 through 23), and state (standing, walking, punching, etc.) determine what sprite to use. This is how I set the sprite.
    Code:
    name = "johnfitch";
    facing = 16;
    state = "punching";
    sprite_index = asset_get_index(name + "_" + string(facing) + "_" +state);
    // This sets the sprite to johnfitch_16_punching. 
    Is there a way to do that with sprites created using sprite_add?
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,694
    Yes, by manually keeping track of them in a map and then shimming the functionality.

    At game start:
    Code:
    global.loaded_sprites = ds_map_create();
    Loading a sprite:
    Code:
    ///sprite_load(fname, imgnumb, removeback, smooth, xorig, yorig)
    var spr = sprite_add(argument0, argument1, argument2, argument3, argument4, argument5);
    ds_map_add(global.loaded_sprites, filename_change_ext(filename_name(argument0), ""), spr);
    return spr;
    Getting the sprite:
    Code:
    ///get_sprite_named(name)
    if (ds_map_exists(global.loaded_sprites, argument0)) {
      return global.loaded_sprites[? argument0];
    }
    return asset_get_index(argument0);
    
     
  3. JFitch

    JFitch Member

    Joined:
    Sep 28, 2016
    Posts:
    428
    Thanks! I would have never thought of that on my own but I completely understand it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice