• 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!

GameMaker Drawing text and deleting text upon clicking object

OnLashoc

Member
I've been racking my brain on this and for some reason I just can't get anything to work. The closest I've come was by downloading a market addon, but even then click on a separate object wouldnt change the text nor destroy text.

Im sure there is an easier way bu I just havent figured it out. So basically I have a couple images representing different races, each image is a separate object. You click on a race and the text object displays a little information about that race.. Upon clicking on another race, the text object needs to be destroyed and replaced with a different text object containing the background info for the new race you clicked on.

I tried several other methods to try and accomplish this but nothing has quite worked. Any suggestions?

This code has worked to accomplish this with objects that have an image, and no text on the DRAW event. But text? hell no it wont destroy the text object.

Code:
if mouse_check_button_pressed(mb_left) && instance_exists(oTribeinfobox){
    if collision_point(mouse_x, mouse_y, oETaman, true, false)
        {
           
        }
Another example that is actually working.

Code:
if mouse_check_button_pressed(mb_left) && instance_exists(oBlankf) {
    if collision_point(mouse_x, mouse_y, oColorgrey, true, false)
        {
            instance_destroy(oBlankf);
            instance_create_layer(oTribeselect.x+730,oTribeselect.y+430,"Banner",oGflag);
          
        
        }  
}
    else
    {
      
        if mouse_check_button_pressed(mb_left) && instance_exists(oRflag) {
    if collision_point(mouse_x, mouse_y, oColorgrey, true, false)
        {
            instance_destroy(oRflag);
            instance_create_layer(oTribeselect.x+730,oTribeselect.y+430,"Banner",oGflag);
        
        }  
}
This code basically destroys the oBlankf object and subsequent code replaces that object with a different flag object

But when the object has no image and contains only a text string to draw, it will not instance_destroy the text object.

Something with having the text drawn is causing it to remain regardless of how I try to change that text upon clicking a different race. I'm almost to the point of creating an image in photoshop of the text, but only want to do that as a last resort.
 

chamaeleon

Member
Instead of using collision functions which would require your instance to have bounds based on an assigned sprite, you could try using a point in rectangle function. If the object in fact does not have a sprite assigned. Just guessing based on your saying "the object has no image". Or assign a sprite of suitable size anyway but don't draw it in the draw event as the act of drawing has nothing to do with collisions and bounds.
 

OnLashoc

Member
I will try your method when I get home. This is very frustrating for something that seems would be so simple. I'm watching Shawn Spauldings Textbox Tutorial again while I work to see if a solution is there as well.
 

TsukaYuriko

☄️
Forum Staff
Moderator
It is in fact simple, but note that collision functions rely on an instance's mask. If you take away an instance's sprite without assigning a mask to it (as it normally defaults to the sprite), it will have no mask and therefore can not collide. Drawn graphics do not form a mask.

If you take away an instance's only reference point for collisions (the mask), you'll have to provide it yourself, e.g. checking for the mouse within the drawn text's bounds (with the bounds serving as the mask). You can store the text to be drawn in a variable and then use the functions string_width and string_height, or their _ext variants for multi-line auto-wrapped text, to automate this process.
 

Gamebot

Member
Edit: Odd half my message disappeared then I couldn't edit.

You say you have two images, they should be sprites for those races correct? If not make them that way. This is the easiest.

How do we "switch" between texts for the person clicked on, is this correct? If so there are many ways.

1. In each object:

Create:
Code:
txt = "Hi I am the race of .."
dratxt = false;
Step:
Code:
// ASSUMING YOU HAVE A SPRITE AS SUGGESTED //

if (posiiton_meeting(mouse_x, mouse_y,  self) && mouse_check_pressed(mb_left))
{
 with (all) {dratxt = false}
 with (id) {dratxt = true}
}
Draw or Draw GUI (Which ever you need):
Code:
if (dratxt)
{
  draw_text(x, y, text)
}


2. Use a global such as (global.txtid) within a controller/parent object that when a person is clicked that persons id gets put into the global variable. Then in the draw event of the parent object's:

Step:
Code:
if (posiiton_meeting(mouse_x, mouse_y,  self) && mouse_check_pressed(mb_left))
{
  global.txtid = id;
}
else
{global.txtid = noone;}

Draw:
Code:
if (global.txtid != noone)
{
 with(global.txtid)
 {
  draw_text(x, y, id.text)
 }
}
 
Last edited:
Top