GML Sprite inside of a string?

Dagoba

Member
Hey,

is there a possibility to have a sprite inside of a string message?

I am creating a sign that guides the player and in the message it should display like: "Press X to Jump".
The X would be replaced with a sprite of X, because the player could for example modify their keyboard settings and have the jumping on the Z -key.
Is this anyhow possible?
Also, all of the sprites are inside of "spr_keyboard_sprites" and the script would determine which image_index would be displayed.

I am using GM 1.4 Studio.

Thanks in advance!
 
Use a special character in a string that denotes where to split the string into two parts. Draw the sprite at the string width of the first half, and then draw the second half at the sprite width.
 

Dagoba

Member
Use a special character in a string that denotes where to split the string into two parts. Draw the sprite at the string width of the first half, and then draw the second half at the sprite width.
How is that achieved? I have the string in a variable called "msg" and it's like this (Creation Code of the Sign object in Room):
msg = "Press X to jump!";

Should I replace the X with like % symbol and have some kind of script that reads what comes next to the percent symbol? Like %3 would cut the string and add the spr_keyboard_sprites image_index number 3?

I have no clue how to code this though.
 

Nidoking

Member
draw the first string
draw the sprite
draw the second string

You will find functions like string_width helpful in determining where to draw each part.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
You can also create a custom font and in place of a special character, you add a desired image.

GML:
font_add_sprite();
This is actually my preferred method... I use font_add_sprite_ext() in practically every game I make, then in the string that defines the character order I use "special" characters that aren't generally used (like ¿, |, [, ç, etc...) for any buttons or symbols that I need. Then I would do something like "Press ¿ To Start" in the game code, where the ¿ character represents the X button sprite in the sprite font. This is not quite as flexible as other methods, but it's perfect for something simple like what you're after.
 
I'm an actual idiot and have never thought of that font_add_sprite method. That's super useful for a quick and dirty sprite addition to text.
 

Dagoba

Member
You can also create a custom font and in place of a special character, you add a desired image.

GML:
font_add_sprite();
This is what I've tried before. I got pretty confused by it. Do I need to like make a new font and have a sprite for each letter? And then just replace the symbols with my own sprites? What if I have an animation with the sprite (for example the key would animate as pressed, and released) so how is this achieved?
 

FoxyOfJungle

Kazan Games
This is what I've tried before. I got pretty confused by it. Do I need to like make a new font and have a sprite for each letter? And then just replace the symbols with my own sprites? What if I have an animation with the sprite (for example the key would animate as pressed, and released) so how is this achieved?
If you want to animate, then the right thing would be to use the Nidoking's method, but I'm not sure if it would be possible to make an animated font or if that is the right thing to do...
 

Dagoba

Member
If you want to animate, then the right thing would be to use the Nidoking's method, but I'm not sure if it would be possible to make an animated font or if that is the right thing to do...
That Nidokings answer doesn't really help. I even stated that I need to split the strings and add the sprite in between, therefore I realized it already. That's why I said I have no idea how to execute that, is there even any string_split methods? I found one from the forum but it's an asset and I want to avoid them as much as possible.

And my message is stored in a single string, not in two.
 

gnysek

Member
One of easiest way to define special character is to use macro, like
#macro XBUTTON chr(500), and then in code you can write draw_text(0,0, string("Press " + XBUTTON + " to jump");
Also, this can be then easily used in font_add_sprite_ext, like font_add_sprite_ext(spr_CalcFont, "0123456789+-*/=" + XBUTTON, ... where XBUTTON will be last image (you can use more special images). In fact you can use any number for chr() if it doesn't overlap with letter codes from string_map . So for English language, that can be everything above 127, for others you need to find out special characters (eg. for me those are ąćęłńóśżź which are randomly throw over UTF map...).
 

curato

Member
If you aren't liking the string suggestions you could always make a sprite with the instruction in it then have a sprite with the control options in it then draw the instruction first then follow with the Button symbol. Not nearly as flexible but it is pretty darn straight forward.
 
Top