HI! I’m super new to using Godot and I’m trying to call a function from my MainScene script (it’s an autoload) in my FishingRod script. I have a control node (sanddollar_score) which is a parent of a label (value) which is what I want my function to change. When I try to run the MainScene it shows the error message attached, which I’m confused about because when I had the function in my sanddollar_score script it worked perfectly fine, so the string thing should not be a problem. So I’m thinking it’s something to do with the script’s connection to the label? Please help!
We can see your script for sanddollar_score
? please paste your code here use three ` before and after that or slecet it use in the forum editor.
And before error (Invalid set index ‘text’ (on base: ‘Nil’) with value of type ‘String’.) you should see line (and char/column) for this error, please share it with us.
this is my MainScene script
extends Node
@export var sanddollar_score : Control
@export var value : Control
var sanddollar = 0
func _ready():
value.text = str(sanddollar)
pass # Replace with function body.
func sanddollar_add():
sanddollar += 1
value.text = str(sanddollar)
print(sanddollar)
and there isn’t a script for sanddollar_score right now as I was moving all the original code into the MainScene script (basically everything in the above)
The error message is for
value.text = str(sanddollar)
Have you specified the label for the value in the inspector?
If you don’t have a script in child nodes, then there is no variable called text in canvas, and only label has such a If you don’t have a script in child nodes, then there is no variable called text in Control, and only label has such a variable.
This is the Inspector for value and I have placeholder text in it if that’s what you mean? I used to have the same code above in my sanddollar_score
Control node and it worked fine, but I’m trying to transfer it to MainScene script (autoload) so that the function sanddollar_add
can be used in other scenes and now it’s saying the text doesn’t work?
You need to open up MainScene
in the inspector and assign your label named value
to the export variable value
. I also recommend you come up with more descriptive names for them. The reason your code worked before and doesn’t now is you have two generically named variables and you have confused them in your head. Giving them more descriptive names would eliminate this confusion in the future.
As you said, that’s what I mean.
The most important thing is not to name a node and a variable the same name, especially when the variable may not refer to that node!
Another tip: it’s usually not a positive thing to merge functions in one place, that’s the basis of OOP. Anything that works independently correctly is better than something that is controlled from the outside, so I recommend keeping your priority on keeping the program modular, one solution doesn’t work in all cases, and there was probably a reason you did it in your example.
Oh I see, thank you for explaining! I’m still quite new to this so I thought I was exporting my actual value
Label straight into the script, didn’t realise I was making a new variable, that’s why I named them the same thing.
Is there a way to use a variable from child of another scene? Because it’s not letting me expand sanddollar_score
to select my value
Label
Right-click sandollar_score and select Editable Children in the menu that appears.
What does your Scene tree’s “Remote” tab say when running the game? Did you set the scene .tscn as an Autoload/Global or the script .gd?
Yeah, notice how your MainScene doesn’t have any children? This is because you set the global as just the script, the Autoload/Global only creates a Node for the script, not the children you were expecting. Set the scene.tscn as Autoload/Global.
I see! That works, thank you!!
Thank you so much to everyone for helping!! Learnt a lot more about how Godot works and my code is working now!! Appreciate it a lot!! <3
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.