0 votes

I'd like to import a variable and a function from autoload, but it says
"invalid type in function 'gemfound' in base 'Node2D (Global.gd);. Cannot convert argument 1 from Nil to int." Why is currentscore Nil?

Global.gd

extends Node2D

#autoload

var current_score = 0

func gem_found(score:int):
    score += 1
    return score 

Gem.gd

extends Area2D

var Ship = load("res://scene/Ship.tscn")
var Global = preload("res://singletons/Global.tscn")
var global = Global.instance()

var current_score = global.current_score
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
func _on_Gem_body_entered(Ship):
    var current_score =  global.gem_found(current_score)
    print(current_score)
    self.queue_free() 
Godot version v3.5.1
in Engine by (15 points)

1 Answer

+1 vote
Best answer

if your Global.gd is singleton and enabled in AutoLoad tab (https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html), then you don't have to preload it's script and instance it. It is automatically available in all scripts via it's registered name (which is script name by default, in your particular case it would be Global). So you have to change your gem.gd script to

extends Area2D

var Ship = load("res://scene/Ship.tscn")
# var Global = preload("res://singletons/Global.tscn") # <- remove this line
# var global = Global.instance() <- remove this line

var current_score = Global.current_score # <- Access singleton here via it's name, i.e. Global

func _on_Gem_body_entered(Ship):
    var current_score =  Global.gem_found(current_score) # <- Access singleton here via it's name, i.e. Global
    print(current_score)
    self.queue_free()

Note that all autoloaded singletons are automatically added to the "/root" node during game startup.

by (1,650 points)
selected by

I got how to use autoload properly, thanks! I also had to set the argument as body, not Ship.

var score = Global.currentscore
func _on
Gembodyentered(body):
if body.name == "Ship":
score += 1
#Global.gem_found(score) keeps returning 1 however many times I hit the gem.
print(score)
#as It counts the score of each gem, I should adjust the position of gem instead of removing the child and adding it again.
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.