Label - Get a line

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Chain
:warning: Old Version Published before Godot 3 was released.

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.

:bust_in_silhouette: Reply From: avencherus

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]

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:

Imgur: The magic of the Internet

Chain | 2017-09-01 08:12

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

avencherus | 2017-09-01 18:39