• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Transforming text into object

D

Darth Raven

Guest
Hi again!
Is it possible to transform a text into object? I'm making a game where you have to drag some words, however I dont feel making a image for each of them and also gives a lot of work with coordinates.
Is there a way to use "draw"?
 

TsukaYuriko

☄️
Forum Staff
Moderator
Yes, it is possible to interact with instances of objects with the mouse even though they have no sprite with a bounding box assigned. You're not going to get around "a lot of work with coordinates", but I wouldn't exactly define it as "a lot".

You can determine the boundaries of text being drawn by keeping an instance's coordinates (x, y) and the current horizontal and vertical text alignment (whatever was set via draw_set_halign/valign - there is no way to retrieve this as of writing, so you'll have to assume the same settings you use when drawing) in mind and using the functions string_width and string_height. To quote the manual, they calculate "(the width and height of) the string as it would be drawn with draw_text using the currently defined font".

Using all of this data, you should be capable of constructing a rectangle which outlines the text being drawn (essentially like a sprite's bounding box) and can then compare mouse cursor coordinates to check whether it's inside the rectangle.
 
D

Darth Raven

Guest
I see. However, it doesn't seem to be easier then creating a image for each word. I say this because I would have to calculate the size of a rectangle for each word ( since they all have different width and height ).
Or am I wrong? What would you do?
And thanks for the answer!
 

TsukaYuriko

☄️
Forum Staff
Moderator
You can assign the text to a variable using the instance creation code (right-click an instance in the room editor -> Creation Code) or by assigning a value to a variable of a dynamically created instance after creating it with instance_create, then use that variable for all further calculations instead of hard-coding the text.
 
Top