Create a "scrolling" text with lists?

So, I’m trying to understand how lists work in GDevelop and create a simple text box thingie for displaying text in rows stacked on top of each other. What I would like to happen is something like this:

You’re warm!
The sun is setting.
It’s getting colder!
It’s late.

Up there are four lines of text that should go into the text box or text area. It’s just a specific spot in the game screen where I’m creating text objects. Every time one object is created, it’s created on screen in a specific order. Then every new line of text after the four rows are all filled (thus there are 4 text objects on screen) would replace the top or bottom row and delete the fourth entry (the oldest entry) and then move the rest up or down accordingly so that the newest, most recent entry appears in its proper place. This should give the illusion of a “scrolling” text field with 4 rows of text.

Currently I’m storing the text into a variable and then appending that variable to a list and then creating a text object on screen and I kinda get it to work. I can create four rows of text like that, but I’m completely at a loss how to actually make it work properly. I’m not really sure what I’m even doing anymore and just trying out things left and right without truly understanding what it is I’m doing.

I’m having such issues as the game constantly creating new text objects on screen when I only want it to happen when there’s an event happening that triggers one. Events such as player taking damage where it would show something like “You take X damage.”

So what I’m asking for would be an example of how to do something like that and if there is a better way of doing this than using lists. That’s just the first thing that came to my mind.

Here is what the “code” looks like. I know it’s all messed up and wrong, but like I said, I’m not really sure what I’m even doing at this point.

I don’t have any example to offer.
The main issue I see with your screenshot is that you don’t link text objects with their content.
You should define an instance variable for each text object, using the rowIndex value, I guess, and then use that variable to know which object should be deleted.

Another method would be to keep creating text objects, and move them all up one row each time a new one is created, and delete the ones that are above a certain Y position.

If you don’t understand what you’re doing, there’s no point continuing.
Write your logic with natural language in a text document or on paper, and when things are clear, start over, converting natural language to GDevelop events.

1 Like

@BossWeapons, here’s a way of doing it that works and is easier to maintain :


That’s a sage piece of advice :smiley:

1 Like

Hey, thanks for the replies. Haven’t had the time to mull this over properly yet, but I glanced at the example @MrMen handed out above. Let’s assume the list is empty to begin with. Wouldn’t removing index 0 at that point just make it so that the list never gets populated and remains empty? Haven’t tried it out yet due to lack of time.

This is so true. That’s actually something I already did, or at least started with, even before making the first post. But for some reason just swapping out text in a way I was trying to describe above has proven to be one of the most difficult roadblocks I’ve faced yet, even if it sounds super simple on paper. Kind of odd that.

Yes, the assumption is that there are already 4 items in the array.

A simple fix would be to add a subevent, just before the "For every child...", that checks whether the array child count > 4, and move the "Remove variable at index 0..." into the new subevent.

1 Like

That worked nicely. Although, if I wanted to center the text, let’s say in the middle of a frame of sorts, then it kind of messes up with the whole thing, since all the text is printed into a singular text object. Makes all of the text move around when sentences of different length are drawn on screen. I guess printing each line into its own text object would work better for that.

Yes, you’ll have to do that. To get the lines below each other without hardcoding the y position changes, you could use the textbox height of the previous box added tot eh previous textbox y position. That would allow you to change font size without changing the events.

1 Like