String continuation using the @ sign

B

BobsTheDude

Guest
I am new to GM and following some tutorials to help me learn GM2. I'm following the Space Rocks tutorial and I am at the draw_text() with the @ continuation.

GML:
draw_text(
            room_width/2, 200,
            @"Score 1,000 points to win!
            UP: Move
            LEFT/RIGHT: change direction
            SPACE: shoot
            
            >> PRESS ENTER TO START <<
            "
        );
The thing is I see the following output:

Capture.PNG

I cannot figure out why I get these boxes.

I have changed fonts and searched with no luck.

Thanks,

BTD
 

Tthecreator

Your Creator!
I think that these boxes you see are the tabs you have in your code. Try and remove them and check if that was the problem.
 
The @ before the string makes it into a literal string, which means it tries to print every single character. This includes characters like tab (as @Tthecreator has pointed out). Because there is no "tab" character in the font, it prints a default "character not found" character, which happens to be that box. You have a few options.

1. Remove the tabs from the string and keep the @ symbol making it a literal string.
2. Remove the @ symbol turning the string into a non-literal string and instead use \n where you want the new lines to happen i.e. "This text is on the first line.\nThis text is on the next line.". This is called an escape character and there are many different ones you can look up in the manual. This might also involve setting the horizontal alignment for the text to centered using draw_set_halign(fa_center) before the draw_text() call. Though, you might already be doing that, it's impossible to know from the code given.
 
B

BobsTheDude

Guest
Thanks for the tip, but I am using the enter key after every line, not tab, unless there is something I am missing.
 
B

BobsTheDude

Guest
Ok, you guys are right thank you so much for pointing this out to me. The funny part is I did not add tabs to the sting itself, but in my switch statement which is really weird.

Before:

GML:
switch(room){
    
    case rm_start:
    
        draw_set_halign(fa_center);
        var c = c_yellow;
        draw_text_transformed_color(
            room_width/2, 100, "SPACE ROCKS",
            3, 3, 0, c ,c ,c, c, 1
        );
        
        draw_text(room_width/2, 200,
            @"Score 1,000 points to win!
            UP: Move
            LEFT/RIGHT: change direction
            
            SPACE: shoot
            >> PRESS ENTER TO START <<
        "
        );
        draw_set_halign(fa_center);
        break;
        
    case rm_game:
    
        draw_text(20, 20, "SCORE: " + string(score));
        draw_text(20, 40, "LIVES: " + string(lives));
        
        break;
        
    case rm_win:
        break;
        
    case rm_gameover:
        break;
    
}
Capture.PNG

Updated switch statement:

Code:
switch(room){
    
    case rm_start:
    
        draw_set_halign(fa_center);
        var c = c_yellow;
        draw_text_transformed_color(
            room_width/2, 100, "SPACE ROCKS",
            3, 3, 0, c ,c ,c, c, 1
        );
        
draw_text(room_width/2, 200,
@"Score 1,000 points to win!
UP: Move
LEFT/RIGHT: change direction
            
SPACE: shoot
>> PRESS ENTER TO START <<
"
        );
        draw_set_halign(fa_center);
        break;
        
    case rm_game:
    
        draw_text(20, 20, "SCORE: " + string(score));
        draw_text(20, 40, "LIVES: " + string(lives));
        
        break;
        
    case rm_win:
        break;
        
    case rm_gameover:
        break;
    
}
Capture1.PNG

So it recognized the tabs I used to ident the switch statement as tabs in the string?

So the GM2 IDE does not handle any space with no character as white space and ignore, like VS, Xcode, etc?

Or do I have something not set properly in settings.

Again thank you so much for quick response and help.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Ok, you guys are right thank you so much for pointing this out to me. The funny part is I did not add tabs to the sting itself, but in my switch statement which is really weird.
...
So it recognized the tabs I used to ident the switch statement as tabs in the string?
You may not realize it, but you did add tabs to the string itself. Not intentionally by any means, but you did, as in the end, they are a part of the string since they are between the opening and closing quote. Tabs in strings are not ignored no matter where they are. This is why it's called a string literal - it takes you literally, even when you don't want it to. :)

So the GM2 IDE does not handle any space with no character as white space and ignore, like VS, Xcode, etc?
White space is, for the most part, ignored. Just not in strings... and that's good becauseyoudon'twantyourtexttolooklikethis. ;)


I'd recommend not removing the indentation in your code and instead just concatenating the string differently. No reason to sacrifice the readability of your code, especially when the "fix" to the problem is easily undone by a code auto-formatter or a careless coworker that gets triggered by this and re-indents the code. No amount of formatting should ever have a chance to break your code. If it can, your code has structural issues that should be addressed immediately.
 
B

BobsTheDude

Guest
Duh, noobie mistake, I see my error, finally., will follow your advice.

Again thank you.
 
Top