GameMaker: Studio

GameMaker: Studio

52 ratings
Tooltips in GML
By Scorcher24
This Guide shows you how to make a tooltip in GML, which can be displayed when you move your mouse over an object.
   
Award
Favorite
Favorited
Unfavorite
Content
/* argument0 = text argument1 = font argument2 = text color argument3 = tooltip color argument3 = alpha */ var text_width; var text_height; var old_alpha; var old_color; // Set alpha old_alpha = draw_get_alpha(); draw_set_alpha(argument4); // Set font draw_set_font(argument1); // Set Aligns draw_set_valign(fa_bottom); text_width = 5 + string_width(argument0); text_height = 5 + string_height(argument0); if ( ( ( mouse_x - room_width + text_width) <= ( room_width - 5) ) && ( mouse_x - text_width ) < 0 ) { // Draw rect around the text draw_roundrect_color( mouse_x, mouse_y - text_height, mouse_x + text_width, mouse_y + 5, argument3, argument3, false); // Set color old_color = draw_get_color(); draw_set_color(argument2); // Draw the text inside the rect draw_text( mouse_x, mouse_y - 2, argument0); // Reset old color draw_set_color(old_color); } else { // Set alignment to right draw_set_halign(fa_right); // Draw rect around the text draw_roundrect_color( mouse_x, mouse_y - text_height, mouse_x - text_width, mouse_y + 5, argument3, argument3, false); // Set color old_color = draw_get_color(); draw_set_color(argument2); // Draw the text inside the rect draw_text( mouse_x, mouse_y - 2, argument0); // Reset old color draw_set_color(old_color); // Reset alignment to left draw_set_halign(fa_left); } // Reset alignment draw_set_valign(fa_middle); // Reset alpha draw_set_alpha(old_alpha);

Save it as a script and name it draw_tooltip.
In your object, make a variable called "showToolTip", initialize it in the create event to 0.
Add Mouse Over Event, set "showToolTip" to 1.
Add Mouse Leave Event, set "showToolTip" to 0.
Add a Draw GUI Event, create a new script and paste this:

if ( showToolTip == 1 ){ draw_tooltip("I am a Tooltip.", fntArial, c_white, c_black, 0.8); }
You need to modify it to your liking though.

The code in action in a game:



The tooltip also shifts to the left or to the right if there is not enough space to display it.
Did not test multiline, but it SHOULD work if you add the "#" in your text.
Thats basically it.
Have fun :)
17 Comments
Scorcher24  [author] 7 May, 2016 @ 4:26am 
Just switch the condition around, Banana Snow. That is all you need to do. But you won't learn anything if I just tell you everything. So try it out yourself and learn programming.

What you need to do is: Ask yourself why it defaults to the left and then change the condition of the if statement accordingly. It is really easy to do.
Scorcher24  [author] 17 Mar, 2015 @ 4:48pm 
Hey Darzall, here is the license:
http://www.wtfpl.net/about/
Darzall 17 Mar, 2015 @ 4:35pm 
Any rules for using this code?
Black Blade 6 May, 2014 @ 1:06pm 
Nice guide

And man it takes time for Valve to do any thing.. you will have to wait for them to hopefully fix it :/
Engi 5 May, 2014 @ 12:52pm 
Thank you :D
Scorcher24  [author] 5 May, 2014 @ 12:51pm 
Scorcher24  [author] 5 May, 2014 @ 12:49pm 
@TheEngiGuy Yes, Steam is swallowing some of the symbols in the code. I reported this multiple times to Steam, but no change so far. Anyway, it is not really meant for copy pasta anyway :D. But here is a working version of it: http://www.reddit.com/r/gamemaker/comments/2002d4/what_are_some_scripts_you_try_to_keep_in_your/cfyqowh
Engi 5 May, 2014 @ 12:30pm 
I got this compile error:
in script draw_tooltip line 26 pos 34: symbol ) expected
99 Beers 5 Jan, 2014 @ 8:08pm 
if ( ( ( mouse_x - room_width text_width) <= ( room_width - 5) ) && ( mouse_x - text_width ) < 0 )
{
// Draw rect around the text
draw_roundrect_color( mouse_x, mouse_y - text_height, mouse_x text_width, mouse_y 5, argument3, argument3, false);

----
Getting a couple of errors here. First error line 26. If I fex that with a '+' next error goes to the draw rectangle line. Messed around for 10-15m but still too new to GML to succeed.
vgreeson 21 Dec, 2013 @ 6:56pm 
Thanks! Very useful. Keep them coming?