GameMaker: Studio

GameMaker: Studio

查看统计:
pez263 2018 年 6 月 21 日 下午 2:01
Adding new text to a text box constantly?
Hello, I'm shifting from Construct 2 to Gamemaker Studio for a text based rpg project because I think Gamemaker would be more reasonable for it. My question is how can I contantly add to the text within a text box? I'm including a link to my wip from CS2 to give you an idea for how I want the text box to function. Ignore the typing function because I've decided to go with clickable buttons instead which aren't a problem for me to make, but the way the text is drawn and added to from the bottom up is what I am trying to recreate in Gamemaker Studio.

https://pez263.itch.io/the-salvage-of-eyes-

Thank you~
< >
正在显示第 1 - 5 条,共 5 条留言
The Winter Bud 2018 年 6 月 21 日 下午 2:15 
Here I would use an 'ds_list'[docs.yoyogames.com] to store all the different lines of text. You can then draw a set of elements out of that list
//create event textToDraw=ds_list_create(); ds_list_add(textToDraw,"This is the first line of text"); // this is how you add to the list startX=32; startY=room_height-32;
//draw event for(var i=0;i<ds_list_size(textToDraw);i++){ draw_text(startX, startY-string_height("X")*i,textToDraw[| i]); }

this is the most basic form of what you are looking for.
textToDraw - is the list that will hold all the text
startX - the horizontal location to start drawing text
startY - the vertical location to start drawing text

Here is a more extensive dialog box system by Shaun Spalding (big youtuber who makes GMS videos)

EDIT: the example starts drawing the text at the bottom of the screen. Each new line will be drawn above the last one by the use of startY-string_height("X")*i
It will not stop though. If you have 100 lines it will draw all 100 lines of text. To limit this function you will have to clamp the number of lines in the for loop
for(var i=firstIndexToDraw; I<firstIndexToDraw+10; i++){ // this will count between (X) and (X+10) }
最后由 The Winter Bud 编辑于; 2018 年 6 月 21 日 下午 2:21
pez263 2018 年 6 月 21 日 下午 2:34 
This helps greatly, I am playing around with this code to get a feel for it and I just have two questins.

1. when I add an event to add addittional text, in this case I simply added a spacebar event that created a new line that said test", it creates the new line above the previous text, how would I tell it to draw the text below and shift the previous text upwards?

2. I try to make the text box appear a little bit higher so I can fit buttons underneath the text but changing the startY=room_height-32 to a lower number such as -20, but I don't see it really adjusting it. I changed the "-" to a "=" and it shifted the text to the top of the screen entirely, but that wasn't quite what I was looking for. Could this be due to my room height in general?

Thank you again.
pez263 2018 年 6 月 21 日 下午 3:05 
With that script that you added at the bottom, I tried adding it first to he create and then to the draw events and it keeps telling me that the "firstIndexToDraw" variable was not set prior. I apologize, I'm not terribly familier with reading gml.
The Winter Bud 2018 年 6 月 21 日 下午 9:09 
In regards to the error, you'll have to initialize the variables in the create event. Give it the index of the first line you want to draw. You'll also have to update the variabe some where as well so it moves through the list (like a mousewheeel up/down event) You'll also make sure that firstIndexToDraw+10 isn't out of bounds on the list.. If the list is 10 items long and you start the index at 1, the count will go from 1-11 which will give you and "out of bounds" error because there is no 11'th element. For now I would leave the variable out and use the original script until you really understand what you're doing with the list and how you are doing it.

to reverse the order of the lines, think about what you're drawing, and where you are drawing it. Let's say you want to draw the 10 most current lines in a 'warcraft' texty style.

text0 <-- first line of text entered into the list
text1
text2
text3
text4
text5
text6
text7
text8
text9 <-- most current line of text

in a 'for' loop we can count up (1,2,3,4,5,etc..) or we can count down (5,4,3,2,1, etc..) In this case counting from the most current text to the oldest text seems the way to go as we wont have to adapt our already given code too much to assimilate it. So let's see what the new code looks like
// let's count down instead of up for(var i=ds_list_size(textToDraw)-1;i>=0;i--){ draw_text(startX, startY-string_height("X")*i,textToDraw[| i]); }
that's it's. We start with changing the starting point to the end of the list wich is the size of the list-1. List's and other data structures start with an index of 0. [0,1,2,3,4] = size of 5. We also changed the comparison to see if it's greater than or equal to 0.. we want to use the 0 index which is the first text entered into the list. And finally instead of adding to 'i' we subtract from 'i' to make it count down

The room origin starts in the top left corner (0,0) when we draw text or any other thing for that matter we can draw it absolutely on the screen. Or we can draw it relative to something in the room.
//absolute startX=10; // top left corner 10 pixels to the right from the left edge startX=10; // top left corner 10 pixels down from the top edge //relative startX=x+10;// origin of the current sprite + 10 pixels to the right startX=y+10;// origin of the current sprite + 10 pixels down // another way to say relative startX=room_width-5; // 5 pixels left of the far right side startX=room_height-10; // 10 pixels above the bottom edge

Remember on the horizontal plane we subtract to go left and we add to go right. On the vertical plane we subtract to go up and we add to go down.

So to move the current location of the text box up we need to change the startY by subtracting even more from room height
startY=room_height-50;
最后由 The Winter Bud 编辑于; 2018 年 6 月 21 日 下午 9:16
The Winter Bud 2018 年 6 月 21 日 下午 9:34 
I would recommend getting the example going and create the part that will add your lines so you can run the game and test the mechanic first hand. Once you have that done, then start thinking about implementing the clamping idea to the basic mechanic.

you know you have a list that starts at index 0 and ends at size-1;
there are gamemaker math functions that can clamp values for you such as
max(value1,value2) -> will return the maximum value
min(value1,value2) -> will return the minimum value
median(value1,value2,value3) -> will return the middle value of all values

examples
firstLine=0; lastLine=min(firstLine+10,ds_list_size(list)-1;
The min() function here will select the number wich is smaller. either 10, or the size of the list minus 1. If the list has 3 lines of text in it this example will draw lines 0 to 2 (size-1)

speed=max(speed-1,1);
In this example speed will either get smaller by subracting 1 or it will be 1. lets say you're speed is 10 this function will return 9. let's say your speed is 1, then this function will return 1, not 0. This example is to make something slow down but not totally stop

angle=(45,direction,135);
in this example we are limiting angle between 45 and 135. ( the upward pie shape from origin) If direction is in between 45 and 135 then this function will return the direction otherwise if direction is less than 45 it will return 45. If direction is greater than 135 it will return 135. This example is something that a turret might have on it. SInce a turret can turn independant from it's tank base, it may want to limit how much the turret can turn even though the tank base can turn all the way around.
最后由 The Winter Bud 编辑于; 2018 年 6 月 21 日 下午 9:36
< >
正在显示第 1 - 5 条,共 5 条留言
每页显示数: 1530 50

发帖日期: 2018 年 6 月 21 日 下午 2:01
回复数: 5