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

GM:S 1.4 Is it possible to convert a string into a sprite/object name?

Discussion in 'Programming' started by Rosieu, Sep 15, 2017.

  1. Rosieu

    Rosieu Member

    Joined:
    Sep 9, 2016
    Posts:
    117
    Hi again everyone!

    Another strange question for everyone here, I'm afraid: Is it possible to make game maker interpret a string as an entity/sprite/object name, etc?

    Sorry for the vagueness of the question, I'm kinda grappling with a code structure idea and I'm wondering how it would work?

    Basically, I'm seeking a way to have a variable interpret the contents it holds (which presumably would be a string) in a way that allows gamemaker to act upon them, without the need for excessive boolean structures or switches.

    Basically, I'm wondering if it's possible to utilise the functionality of string manipulation to introduce some variability to variables without the use of individual evaluations.

    I'll provide an example of what I mean due to the crap job I'm doing of explaining things!

    Code:
    //So, given variables can manipulate strings like so:
    
    word1 = "Hello";
    word2 = "world";
    
    Greeting = word1 && word2;
    Which then would (assuming I haven't botched my pseudo-code) result in the variable 'Greeting' containing the string "Hello world", is it possible to then read this string as a function / name in Game Maker? By my logic, if that was the case, it would allow for incredible flexibility, as you could introduce structures like the below:
    Code:
    //Setting up variables (which may be manipulated by preceding code)
    
    prefix = spr_
    target = example
    spritetodraw = prefix && target
    
    Which would allow for the 'draw_sprite()' function to be increasingly dynamic.

    The issue I see however, is that the above pseudo-code would simply see the contents of prefix and target as other variables, however if I designated them as strings, the variable 'spritetodraw' would then contain a string, which would not be able to be read by draw commands, and other aspects of code.

    What I'm wondering is if there is a way to make GM interpret a string (which might read "spr_example") and think "Hmm, I'll treat that as spr_example, which is then a sprite name" and act accordingly.

    I know this is essentially something you could work around with boolean, arrays or data structures, but by my logic it would simplify things a great deal; for example, if you were drawing sprites in an rpg, you could draw different sprites dynamically (like so):
    Code:
    prefix = "spr_";
    actor = "sephiroth";
    state = "psn";
    spritetodraw = prefix && actor && state;
    
    draw_sprite(spritetodraw, -1,x,y)
    rather than having to resort to if statements to decide "if actor is poisoned, choose this sprite, else if paralyzed, choose this one instead" and so on.

    Anyway, sorry about this rambling musing of mine, but I just wonder if it could be a really neat way or organising some code and potentially increasing project dynamism.

    Any input is great appreciated!
     
  2. hippyman

    hippyman Member

    Joined:
    Jun 20, 2016
    Posts:
    565
    Documentation is a wonderful thing.

    asset_get_index is probably what you want.
     
    Binsk and Rosieu like this.
  3. Rosieu

    Rosieu Member

    Joined:
    Sep 9, 2016
    Posts:
    117
    AHHHH! Thank you! I trawled through the docs looking for exactly this, I've been looking in the wrong spot though, I was looking under strings and variables rather than assets. I cannot thank you enough! :D
     
    Binsk and hippyman like this.
  4. hippyman

    hippyman Member

    Joined:
    Jun 20, 2016
    Posts:
    565
    No worries! That's what we're here for :D
     
    Rosieu likes this.
  5. Rosieu

    Rosieu Member

    Joined:
    Sep 9, 2016
    Posts:
    117
    Just out of interest, would this method of asset_get_index also work for a script?
     
  6. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,496
    It would; there's also asset_get_type to see what asset did the asset_get_index find.
     
  7. Rosieu

    Rosieu Member

    Joined:
    Sep 9, 2016
    Posts:
    117
    Oh that's handy to know as well! That's so handy, my design just became far simpler, thanks so much everyone! :D
     
  8. jr carey

    jr carey Member

    Joined:
    Sep 22, 2016
    Posts:
    172
    you can also do, object_get_name(objects.object_index) and this will return the name of the current instance in the room
     

Share This Page