1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

How to set sprite_index to a string?

Discussion in 'Programming' started by Pafbar, May 23, 2019.

  1. Pafbar

    Pafbar Member

    Joined:
    May 19, 2019
    Posts:
    14
    Hello!

    (scroll down to see the crux of the issue)

    I'm making a game with RPG-style dialogue, where the different characters you talk to have different poses when they talk to you based on their emotion. The system has global variables that determine which characters are on the left and right sides of the dialogue box, and the emotion of each character. I'm trying to make it so that when setting the sprites for these two sides of the box, I can just extract the values of these global variables and set the character's sprite index from the string. For example:

    (in text controlling object)
    global.Lemote = "happy"
    global.Lcharacter = "fred"

    [This script controls the left-side talking object, called obj_Ltalk. I have a sprite called happy_fred. This is in the create event]
    sprite_index = string(global.Lemote)+"_"+ string(global.Lcharacter);

    (crux of issue starts here)
    My trouble is that the string tries to convert into an integer, so whenever I activate the text box it displays the error message:

    unable to convert string "happy_fred" to integer
    at gml_Object_obj_Ltalk_Create_0 (line 5) - sprite_index = string(global.Lcharacter)+"_"+string(global.Lemote);

    So what should I do to make it recognize the string?
    Thanks to anyone who responds!
     
  2. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    997
    Try using asset_get_index()
     
  3. Pafbar

    Pafbar Member

    Joined:
    May 19, 2019
    Posts:
    14
    In place of sprite_index? I've tried that but maybe I did it incorrectly.
     
  4. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    868
    Like this:
    Code:
    sprite_index = asset_get_index(string(global.Lemote)+"_"+ string(global.Lcharacter));
    It helps to read the manual for any commands that you do not understand the usage of, as there are example in there for each one that will definitely help you.
     
  5. Pafbar

    Pafbar Member

    Joined:
    May 19, 2019
    Posts:
    14
    Ah. Thank you, that worked!
     

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