Variable is not being called correctly inside the function

Godot Version

4.3

Question

`PT-BR
Olá, estou com uma duvida, estou chamando uma função de um outro script onde ele muda o numero de 0 para 1, e quando dou print fora da função que desejo usar, ele funciona, mostrando corretamente o numero digitado no script anterior, porém quando o chamo dentro da função desejada, ele acaba sempre devolvendo 0, e apenas aceita numeros alterados dentro do proprio script.

ENG
Hello, I have a question, I’m calling a function from another script where it changes the number from 0 to 1, and when I print it outside the function I want to use, it works, correctly showing the number informed in the previous script, but when I call it inside the desired function, it always ends up returning 0, and only accepts numerous changes within the script itself.

scripts:

extends Node

const BD = preload("res://bloco_debug.gd") 
var drop_item : int  #variable called in the other script
var item_loader = ""
var teste = 123

func _process(delta: float) -> void:
	add_to_group("itens")
	if Input.is_action_just_pressed("debug"):
		print("tipo in t_itens: ", drop_item)
		#print("teste desse caralho: ", teste)

#fazer com que ele indentifique qual drop dropar
func tabela(string):
	#ele não está indentificando o drop_item dentro do tabela??
	print("drop_item inside tab: ",drop_item)
	print("teste inside tab: ",teste)
	
	match drop_item:
		0:
			print("spawn 0 ativado")
			return "res://cogumelo.tscn"
		1:
			print("spawn 1 ativado")
			return "res://inimigo.tscn"

`

how are you changing the variable from the other script ?

1 Like

I’m calling him this way

@export var type_drop = 1
func _ready() -> void:
	TabItens.drop_item = type_drop

is TabItens declared as an Autolad node with the script you attached in your original post ?

yes

and your second script, where does it come from ? is it possible that the _ready function of your 2nd script is called BEFORE your TabItens autoload initialization ?

sorry for taking so long to respond, the second script comes from a collider/block, I’ll put the complete script here, and regarding the initialization, I put set_process_mode(0) in the first script and in the second I put it by the node itself as priority 5, and even so the problem persists.

Script 1:

extends Node

const blocoD = preload("res://bloco_debug.gd")  
var drop_item : int
var teste = 123

func _ready() -> void:
	set_process_mode(0)

func _process(delta: float) -> void:
	add_to_group("itens")
	if Input.is_action_just_pressed("debug"):
		print("tipo in _process: ", drop_item)
		#print("teste desse caralho: ", teste)
		

#fazer com que ele indentifique qual drop dropar
func tabela(string):
	#ele não está indentificando o drop_item dentro do tabela??
	print("drop_item inside tab: ",drop_item)
	print("teste inside tab: ",teste)
	
	match drop_item:
		0:
			print("spawn 0 ativado")
			return "res://cogumelo.tscn"
		1:
			print("spawn 1 ativado")
			return "res://inimigo.tscn"

Script 2:

extends Node2D
class_name Bloco

#tentando implementar o drop
#existem 5 itens, dependendo de qual selecionado, e o que vai dropar
#ps.: NÂO TEM 5 ITENS



@export var type_drop = 1
@export var Quebravel : bool = false
@export var debug = false

func _ready() -> void:
	load("res://tab_itens.gd")
	TabItens.drop_item = type_drop

var item = load(TabItens.tabela(tipoD))
var tipoD = ""

func _process(delta: float) -> void:
	if debug == true:
		while debug == true:
			print("type_drop in block Number :",type_drop)
			print(item)
			debug = false
	if Input.is_action_just_pressed("debug"):
		item_drop()

func _on_area_2d_body_entered(body: Node2D) -> void:
	
	if body.name == "Player" :
		
		match Quebravel:
			false:
				#Para mudar a cor do bloco, totalmente debug, tbm muda a cor do drop kkkkk
				set_modulate ("3a3a3a")
				item_drop()
			
			true:#bloco quebravel
				queue_free()
	pass # Replace with function body.

func item_drop():
		print("Spawn_on")
		var item = item.instantiate()
		item.position = Vector2(0,-40)
		add_child(item)