Only "String" or "StringName" can be used as index for type "Node", but received "int".

Godot Version 4.7.2

Question

Hi there! I know that this is similar to other questions in the archive, but I looked at those solutions and wasn’t able to get it to work; so I’m hoping that maybe some more personalized responses may help me.

I have here this line, pulling the variable blurb_lines which is an array of strings from one file and randomly picking one of them for my main character to say (This is a sort of Tamagotchi type virtual pet, and I want him to speak periodically).

func Update(delta: float):

	if randi() % 10 == 0:
		random_line = blurb_lines[randi_range(0, blurb_lines.size()-1)].text
		$blurb_label.text = [random_line]

But, the third line here–the one beginning with random_line–keeps returning this error.

Error at (28, 35): Only “String” or “StringName” can be used as index for type “Node”, but received “int”.

Obviously the int is randi_range, but what I’m intending is to pull that numbered entry from the array. The general line here works fine when used in the base file where blurb_lines originates from (I’ve only used it to print text, but it DOES work) so can anyone tell me why it’s failing here?

Apologies if there’s any information missing, I’m a beginner so it’s entirely possible I’m missing something blatantly obvious. Thanks for any help!

What exactly do you assign to blurb_lines? Strings don’t have a text property.
And according to you error message it’s a node, not an array.

add

$blurb_label.text = str(random_line)

and it should convert it, if it doesn’t then you store something that can’t be turned into string and you need to provide more code on how these blurblines work

@export var blurb_lines : Array[String] = [
“Line 1.”,
“Line 2.”,
“Line 3.”,

]

Here’s the base for the lines–each dialogue line should, ideally, pop up at random intervals.

When I first added this, to make sure it functioned as a base, I added this section into the same script:

func _ready():
print("Random blurb: ", blurb_lines[randi_range(0, blurb_lines.size()-1)])

This one prints perfectly fine–I get a randomized choice of the three on boot. I can only guess that the issue lies somewhere between exporting blurb_lines from script one and importing it into script 2. Even trying the ‘print’ function, exactly as is, has the same issue.

No luck unfortunately–I’ve posted the full context around blurblines in another comment now

I think you convolute it a bit too much.

Why not just do

random_line = blurb_lines.pick_random()

$blurb_label.text = random_line

ok im back, i see @baba gave great suggestion to just use array.pick_random function, unless you need weighted chance or luck modifiers for these lines then it’s your best choice probably

If I understand correctly, this definition of blurb_lines is in a different script?
How is the one in the erroring script defined/initialized?

Sounds like you could be accessing the array in some weird way?

In the script you have shared here, you could just get the array in its entirety.

var blurb_lines = other_script.get_blurb_lines()

In the other script:

func get_blurb_lines():

return blurb_lines

Then just pick random from it.

Just print out blurb_lines to see what you get as output. Why does the error message refer to Nodes.

func Update(delta: float):

	if randi() % 10 == 0:
        print(blurb_lines)
		random_line = blurb_lines[randi_range(0, blurb_lines.size()-1)].text
		$blurb_label.text = [random_line]

Yes your issue seems to be about importing and exporting blurb lines. But you didn’t write any script or information about that part. So it’s probably very hard anyone to help you, until you give more information/code related to that part.