Having issues with variable's value when passing to text field concatenation

Godot Version

4.5.1.stable

Question

Hello everyone! I’m kinda new, and English is not my mother language so, I apologize in advance for any issues this post might have.

I’m having some issues displaying the variable’s value on the dialogue line “extra_lines”.

I’m not sure exactly what I’m doing wrong, the variable gets printed correctly but when I print the coins variable.
But when I try to print the array it doesn’t work (an also doesn’t work when the character has those conditions to trigger the extra line”

var coins: String # I wanted to add the value score_label.text directly
# but I had to do it with _assign_coins due to 
# the score_label.text sometimes being null

# this function is to decide which dialogue display based on coins 
func _check_coins():
	print("coins: %s" % coins)
	if(int(coins) < 12):
		lines_to_go = blocked_lines
	elif(int(coins) > 12):
		lines_to_go = extra_lines
	else:
		lines_to_go = enter_lines
	
# this assigns the score label text to the variable coins 
func _assign_coins():
	if score_label: 
		print(score_label.text)
		coins = str(score_label.text)
		print("assign: %s" % coins)

(...)

var extra_lines: Array[String] = [
	"Scum! You need 12 coins to...",
	"Hrm, actually I need: %s" % coins,
	"Yeah, exactly that number.. HEY.",
	"DON'T STARE AT ME LIKE THAT, SCUM.",
	"You may pass"
]

(...)

# When interacting with the character 
func _on_char_interact():
	_assign_coins()
	_check_coins()
	print(coins)
	print(extra_lines)
	DialogManager.start_dialog(global_position, lines_to_go, speech_sound)
	await DialogManager.dialog_finished


(...)

I tried converting it to int, but nothing seems to work, I’ll keep debugging in case It works out and I’ll update this post!


output:
1
assign: 1
coins: 1
[“Scum! You need 12 coins to…”, "Hrm, actually I need: ", “Yeah, exactly that number.. HEY.”, “DON'T STARE AT ME LIKE THAT, SCUM.”, “You may pass”]

You are storing the initial value of coins (which is an empty string at that time) as replacement for the format string. You should provide the replacement right before printing/using the text to have the current value.

I don’t know if there’s an easier/better approach, but you could use the String.format() function for this:

var extra_lines: Array[String] = [
	"Scum! You need 12 coins to...",
	"Hrm, actually I need: {coins}",
	"Yeah, exactly that number.. HEY.",
	"DON'T STARE AT ME LIKE THAT, SCUM.",
	"You may pass"
	]
func format(text : String) -> String:
	return text.format(
		{
			"coins" : coins,
		}
	)
	print( extra_lines.map(format) )

You would have to make sure to always apply the formatting to the array before using it though.

That worked! Thank you so much! :grin: