Godot Version 4.2.1
please help fix this i have tried everything i can think of
Godot Version 4.2.1
please help fix this i have tried everything i can think of
randomize()
lines.pick_random() #for true random
lines[randi_range(0,7)]
replace your Label.text, you refer to Class not an instance of Label
var label:=Label.new()
label.text=lines.pick_random()
i get this error message
Invalid set index ‘text’ (on base: ‘null instance’) with value of type ‘String’.
now i changed it exactly how you wrote it i saw i had changed something by accident i get 5 errors
I blieve you are looking to create a new random number generator. randomize()
will seed the global random number generator, not make a new one.
var rng := RandomNumberGenerator.new()
rng.randomize()
Then I see your Label
is the type, not the node name, make sure to use the dollar sign to get child nodes
$Label.text = randomStr
But then you also queue_free()
this node immediately after, so I’m not sure what this program is supposed to achieve.
the queuefree is part of my collision dectector and dissapear code
and do you know a easier way to randomize strings because that doesn’t make sense yet i looked up what i wrote but i want to make code for my game that makes sense to me so i can edit better when i update my game with new features
pick_random()
as @solver10 said would reduce your code, it uses the global random number generator.
func _on_area_entered(area: Area2D) -> void:
if area.is_in_group("npc"):
$Label.text = lines.pick_random()
i tried i get this error
Invalid set index ‘text’ (on base: ‘null instance’) with value of type ‘String’.
Then $Label
doesn’t exist. What text are you trying to set?
i am trying to change my label i renamed reactions to a random sentence from my array called lines to appear on the screen
You’ll have to get your label from the scene tree somehow, do you know where it is relative to the scripted node? Could you share a screenshot of your scene tree?
Awesome, your label is named “reaction” so you would use $reaction.text = lines.pick_random()
it still doesn’t work
Could you be more verbose? What doesn’t work? Do you get an error? Could you include your updated script too?
It works by that i mean it runs it just doesn’t change the text to a random sentence everytime the npc gets hit all it does is stay the same original test text i wrote in the label text it should update the label with new text but it stays the original no change in the text label text
Do you still have queue_free()
in the script?
yes thats part of the npc getting hit and respawning code
Then you are deleting any changes made to the text, maybe you want to randomize the text every time the NPC spawns? like on _ready()
?
no i want to appear before it disappears these lines are supposed to be a reaction to getting hit