Anyone know why get_random() isn't working for me

Godot Version 4.2.1

I don’t know why but isn’t working like i think it should


pick_random() is a method of the Array class, and isn’t a standalone function. In order to use it, you write the array first, then .pick_random(), like this:

lines.pick_random()

For more information, please reference the docs: Array — Godot Engine (4.2) documentation in English

tried got this eroor
Invalid type in function ‘get_index’ in base ‘Label’. Cannot convert argument 1 from String to bool.

Which line is the error originating from? What’s the code there?

In any case, it seems you have a String value in a method that is expecting a bool value.

Make sure to read the Label docs for more info: Label — Godot Engine (stable) documentation in English

this what i have

Perhaps try

$Label.text = lines.pick_random()
extends Node2D

const MOVE_SPEED = 300
const SCREEN_WIDTH = 1000
const SCREEN_HEIGHT = 600  # Set your screen height
var npc_scene = preload("res://npc.tscn")
var can_spawn = true
var lines = ["Help I got hit by a bomb",
"I think I got egg on my face wait no it's mayoniase",
"Help the sky is falling",
"What in the world why is glue falling from the sky",
"Watch out the goverment is testing bio weapons again",
"Oh great and I just got octopus ink out of my hair"]


func _process(delta):
	if can_spawn == true:
		var stage_node = get_parent()
			
		var npc_instance = npc_scene.instantiate()
		npc_instance.position = Vector2(25, 500)
		stage_node.add_child(npc_instance)
		can_spawn = false

func respawn_npc():
	await get_tree().create_timer(1.0).timeout
	var random = randi_range(lines[0], lines[5])
	$NPC/Label.text = str(random)
	can_spawn = true

here is my updated where label is now in main game scene instead of npc directly

randi_range() returns an integer, you want to call this: as @WannaKnowMorePls
mentioned before

$NPC/Label.text = lines.pick_random()
1 Like

doesn’t work any other ideas

What do you mean by “doesnt work”? what happens when you try it? do you get any error messages?

it doesn’t change it, it just messess up more

Invalid set index ‘text’ (on base: ‘null instance’) with value of type ‘String’.
this the error i get

This means that the node-path ($NPC/Label) doesnt work.
Can you show the scenetree of this scene?

1 Like

It should just $Label
You can see this in his script.

hey i wound deleting all my label code because i thought it was spawning right but apparently i have more to perfect because when i raised the npc in the main game scene i saw that it spawns two at once you just can’t see it becasue the position so first i need to fix that