Draw a rectangle to match the string width.


I'm trying to draw a rectangle window in a menu system, which uses an array to store the options. The thing is that such options aren't always going to be the same, they could vary in width and height and I'd like the rectangle to match those measures.

This is the code.

if (alpha < 1) alpha += 0.08; else alpha = 1;

draw_rectangle(x + espacio, y, x + string_width(string(diagMenu)), y + 100, 0);


//Dibujar Texto
var m;

for (m = 0; m < array_length_1d(diagMenu); m += 1)
    draw_text(x + espacio, y + (m * espacio), string(diagMenu[m]));
I tried to use string_width(string(diagMenu)) to get the width of the strings; however it seems to give me a sum of every option. But weirdly enough for me, I did a test to check the width of a single option and it gave me the following result. I used string_width(string(diagMenu[0]))


the rectangle doesn't cover all the width of the string.

Should I use the function in another way?


I'm trying to do the same thing. I didn't touch the project in a year so half of it is mystery to me.

I have an object (o_textbox) that draws text above the head of character.

I want to add a black rectangle beside the text.

Draw Event:

draw_set_color (c_black);
draw_rectangle(x-2, y+16, x + 2 + string_width(o_textbox), y, false);

draw_set_color (c_white);

No matter the text, the rectangle is one character wide.

I tried to add variable that tied to string_width in step event to no result.

Also I tried to copy-paste previous solution with no effect. And I don't understand it.

It's like easier to create a black sprite with changing width than to make it properly.


TMC Founder
GMC Elder
to both OP and above... why are you passing something that is NOT A STRING to a function that requires a STRING to determine the width in pixels of a string!!!?

that's an object... how is gml supposed to know you want to measure that text variable specifically?

that is a 1d array... how is gml supposed to know to loop through it and find the longest string in the array? string of an array btw returns "{{word1, word2, word3,...}}"



> how is gml supposed to know you want to measure that text variable specifically?

So how does gml suppose to know the length of the text object draws?