Legacy GM Is it possible to convert a string into a sprite/object name?

R

Rosieu

Guest
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!
 
R

Rosieu

Guest
Just out of interest, would this method of asset_get_index also work for a script?
 
R

Rosieu

Guest
Oh that's handy to know as well! That's so handy, my design just became far simpler, thanks so much everyone! :D
 

jr carey

Member
you can also do, object_get_name(objects.object_index) and this will return the name of the current instance in the room
 
Top