0 votes

Is there a way to achieve sth like this, label.get_line(3)?

PS: I have tried get_text() and then split("/n")ing it, but that didn't work.

asked Aug 31, 2017 in Engine by Chain (71 points)

1 Answer

0 votes

Works fine for me.

I would recommend that you review how arrays and string processing work. It will save you a lot of time.

Is it safe to assume you don't know how arrays and indexing works? Because the solution (without error checking and a function built into one specific label), is just:

Split returns an array of strings that are divided by the token you give.

extends Label

func get_line(num):
    return get_text().split("\n")[num - 1]
answered Aug 31, 2017 by avencherus (4,811 points)

Well, I know how Arrays, etc. work. But if I try your way, that also looks right to me, it doesn't work. It only returns me the whole text of the label. Maybe its also worth mentioning that this label has autowrap on. Heres my code:

extends Label

var text = ["test","a car is cool","Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea est Lorem ipsum dolor sit amet."]

var k = 0
var narrating = false

func _ready():
    set_text(text[k])
    set_process_input(true)
    print(get_max_lines_visible())

func _input(event):

    if Input.is_action_pressed("ui_accept") and not narrating:
        k+=1
        if k >= text.size():
            k = 0
        set_visible_characters(0)
        set_text(text[k])

    elif  Input.is_action_pressed("ui_accept") and narrating:
        set_visible_characters(text[k].length())

func _on_Timer_timeout():

    if not get_visible_characters() >= text[k].length():
        narrating = true
        set_visible_characters(get_visible_characters() + 1)
    else:   
        print(get_text().split("\n")[0]) <-- This doesn't ouput the first line, but the hole text
        narrating = false
        return

Scene structure:

http://imgur.com/a/8lwcJ

I see now.

Well the problem isn't autowrap, it is that you only have 1 line. The line is being wrapped in the box, but it doesn't modify your string to include any newline/carriage returns.

The text you're sending in this example is simply element 0 of your text variable, which is "test".

As a single line with no newlines characters, there is nothing you can split the string with.

This is an example of something you might want to do if you want to set up such scenarios directly in script.

extends Label

var lines = ["line1", "line2", "line3"]

func _ready():

    var string = ""
    var first_line = true

    for line in lines:

        if(first_line):
            first_line = false
            string += line

        else:
            string += "\n" + line

    set_text(string)
    print(string.split("\n")[1]) # Get line 2
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.