Game Maker Text Box

  1. //argument0 = the text
  2. //argument1 = whether or not to draw the box at the end
  3. //argument3 = whether or not to draw the text box
  4. //` = white text
  5. //{ = blue text
  6. global.drawingtext = true //the game is drawing text
  7. if argument2 = true //if it is the first time through
  8. self.position = 1//set the position as 1
  9. }
  10. global.more = argument1
  11. global.font = spr_letters //set the font at the beggining of the script
  12. //sets the postion of the pointer, so it knows how much text should be drawn
  13. if keyboard_check(vk_enter) = true //if enter is pushed
  14. self.done = true //you are done
  15. self.position = string_length(global.text) //end the text
  16. self.curstring = global.text //set the string to the text
  17. keyboard_clear(vk_enter) //stop generating events
  18. if self.done = false //if the text line isn't done
  19. self.curstring = string_copy(global.text, 1, self.position) //the first line is a part of the actual first line
  20. if string_length(self.curstring) = string_length(global.text) //if the text is done
  21. self.done = true //this line is done
  22. }
  23. if self.done = false //if not all text is done
  24. self.position += 1 //add one to the position
  25. if argument3 = true //if you should draw the text box
  26. draw_sprite(spr_textbox, 0, view_xview[1] + 16, view_yview[1] + 160) //draw the text box
  27. self.pointx = 22 //a little to the right...
  28. for(i = 1; i < string_length(self.curstring) + 1; i += 1) //once for every character
  29. self.lettertemp = string_copy(self.curstring, i, 1) //get the letter
  30. {
  31. self.pointy += 16 //go down a line
  32. if self.lettertemp <> ' ' and self.lettertemp <> '~' //if it isn't a space or a tilde
  33. if self.lettertemp <> '`' and self.lettertemp <> '^' and self.lettertemp <> '{' and self.lettertemp <> '}' //if it's not changing the font
  34. self.todraw = scr_getletter(self.lettertemp) //get the imagesingle of the value
  35. draw_sprite(global.font, self.todraw, view_xview[1] + self.pointx, view_yview[1] + self.pointy)
  36. }
  37. {
  38. {
  39. }
  40. {
  41. }
  42. {
  43. }
  44. {
  45. }
  46. }
  47. {
  48. }
  49. {
  50. self.pointy += 16 //move down for the next letter
  51. self.pointx = 22 //reset to the start of the line
  52. }
  53. screen_refresh() //refreshes the screen so you can actually see it
  54. if self.done = true and global.more = true //if we need to draw an arrow
  55. draw_sprite(spr_textmore, 0, view_xview[1] + 155, view_yview[1] + 216) //draw the arrow
  56. {
  57. sleep(30)
  58. scr_dialogbox(argument0, argument1, false, argument3) //do it all again
  59. else //yer done!
  60. screen_refresh() //refresh the screen
  61. sleep(30)
  62. keyboard_clear(vk_shift)
  63. global.drawingtext = false //get on with your regular life
  64. ___________________________________________________________________________________________________________________
  65. letter = argument0
  66. if ord(letter) >= ord('A') and ord(letter) <= ord('Z') //if it is an uppercase letter
  67. return (32 + (ord(letter) - ord('A'))) //return a value for the letter
  68. if ord(letter) >= ord('a') and ord(letter) <= ord('z') //if it is a lowercase letter
  69. return (64 + (ord(letter) - ord('a'))) //return a value for the letter
  70. if letter = '!' //exclamation mark
  71. return 0
  72. if letter = '' //quotations
  73. return 1
  74. if letter = '#' //number sign
  75. return 2
  76. if letter = '$' //dollar sign
  77. return 3
  78. if letter = '%' //percent sign
  79. return 4
  80. if letter = '&' //ampersand
  81. return 5
  82. if letter = '' //semi-qute
  83. return 6
  84. if letter = '(' //parentheses
  85. return 7
  86. if letter = ')' //parentheses
  87. return 8
  88. if letter = '*' //asterisk
  89. return 9
  90. if letter = '+' //plus sign
  91. return 10
  92. if letter = ',' //comma
  93. return 11
  94. if letter = '-' //dash
  95. return 12
  96. if letter = '.' //period
  97. return 13
  98. if letter = '/' //slash
  99. return 14
  100. if letter = '0' //0
  101. return 15
  102. if letter = '1' //1
  103. return 16
  104. if letter = '2' //2
  105. return 17
  106. if letter = '3' //3
  107. return 18
  108. if letter = '4' //4
  109. return 19
  110. if letter = '5' //5
  111. return 20
  112. if letter = '6' //6
  113. return 21
  114. if letter = '7' //7
  115. return 22
  116. if letter = '8' //8
  117. return 23
  118. if letter = '9' //9
  119. return 24
  120. if letter = ':' //colon
  121. return 25
  122. if letter = ';' //semi colon
  123. return 26
  124. if letter = '<' //less than
  125. return 27
  126. if letter = '=' //equal to
  127. return 28
  128. if letter = '>' //greater than
  129. return 29
  130. if letter = '?' //question mark
  131. return 30
  132. if letter = '@' //at sign
  133. return 31
  134. if letter = '[' //bracket
  135. return 58
  136. if letter = ' //slash
  137. return 59
  138. if letter = ']' //bracket
  139. return 60
  140. if letter = '^' //carot
  141. return 61
  142. if letter = '_' //underscore
  143. return 62
  144. if letter = '' //quotation mark
  145. return 63
  1. Write some text and click Submit to get your own personal speech bubble. This site is inspired by, but not affiliated with, the excellent comic diesel sweeties.
  2. Drawtextext(100, 50, keyboardstring, 3, 300); The above code will draw whatever text the user types into the keyboard, splitting it onto new lines every time the string length for that line exceeds 300 pixels. The code will also maintain a separation of 3 pixels between lines should this occur.

A game with dialogue can be drastically improved with a 'typewriter' effect, in my opinion at least. Not only is it aesthetically pleasing, but can ease the burden of a lot of text on-screen. Though this example is written for Game Maker, it can be adapted to other languages with ease.

Use the Draw Event in GameMaker: Studio when you want to display something during the game. The Draw Event does have its own submenu, as shown in this figure.

If you use a Draw Event, GameMaker no longer defaults to the assigned Sprite, and instead you have to tell GameMaker what to draw.

Scrolling Text Box Myspace

Text

The different options from the submenu include

Text Box Excel

  • Draw: The standard Draw Event should be sufficient for most of your needs. The Draw Event enables you to place code or Actions for an Object, and those Actions and code override the default draw, which would be the assigned Sprite.

    This is useful, for example, if you want to replace the assigned Sprite with text to display a message. This Event is called once per View, while the other Draw Events are drawn each step of the game.

    All Objects with an assigned Sprite with Visible toggled on (from the Object Properties window, as shown here) automatically trigger a default Draw Event every step of the game. GameMaker does this for you, which is nice, because you don’t have to assign a Draw Event for each Object with an assigned Sprite to have it appear in the game.

    Select the Visible check box to automatically trigger a default Draw Event.
  • Draw GUI, Draw GUI Begin, and Draw GUI End: GUI is an acronym for graphical user interface. The GUI is not affected by the View scale or rotation. Instead, the GUI is drawn on a separate layer than, say, the background.

    A Draw GUI Event is great for creating a heads-up display (HUD), which can show a player’s health, ammo, weapon type, a mini map, and so on. As with the other Begin and End Events, the Draw GUI Begin and Draw GUI End Events determine the order of what’s drawn.

  • Draw Begin and Draw End: These two Events do exactly as they say: They make sure that something is drawn in the game either before or after the standard Draw Event. Using these Events is a great way to make sure items are drawn in the order you want them to appear.

    The Draw Begin, Draw, and Draw End Events all run before the Draw GUI Events and between the PreDraw and PostDraw Events. This has significant meaning in that all these Events are drawn beneath the Draw GUI Event, no matter what the Depth is set at.

  • PreDraw and PostDraw: The PreDraw and PostDraw Events are drawn directly to the screen buffer, which is the combined screen space for all Views currently visible, or the window size if you don’t use Views.

    The PreDraw Event is triggered before any other Draw Events, which means you can use it to set values, set draw properties, and, of course, draw things. The PostDraw Event runs after the other Draw Events but before the Draw GUI Event.

    You need to deselect the Clear Background with Window Colour check box in the Room editor for PreDraw and PostDraw Events to work. This figure shows where to deselect this feature (the default setting has it selected). You also need to make sure that a background color is not drawn; you do this on the Background tab.

    Do not select Clear Background with Window Colour for PreDraw and PostDraw Events.

    If you turn off the View Clearing and disable drawing a background color, you might experience trails (leftover traces of images) during gameplay. These trails happen because now you’re drawing directly over the previous frame without it being cleared.

    You can use code to help solve this potential problem, which is draw_clear_alpha. You can place this code in an Execute Code Action within the PreDraw or PostDraw Event.

  • Resize: The Resize Event is used specifically for Windows 8 games and doesn’t actually draw anything in the game. Instead, the Resize Event is to help snap the game window when the player moves that window to the side of the screen.

    If you’re familiar with working in a Microsoft Windows environment, you’re aware that if you’re working in a window and you drag the top of the window to the top of your screen, Windows will snap the bottom of the window to the bottom of the screen. This Event is triggered every time the player resizes or moves the game window.