Get_node and Signal not working

Godot Version

4.3

Question

Pt-br

Olá, estou a algum tempo tentando chamar uma função de um script para outro, porem ele diz que o node não existe, já tentei usar tanto a função signal quanto a mais simples com só a get_node.

Eng

Hello, I’ve been trying to call a function from one script to another for some time, but it says that the node doesn’t exist. I’ve tried using both the signal function and the simpler one with just get_node.

Scripts:

Coin:
extends StaticBody2D

@onready var cont = get_node(“player”)

func _on_area_2d_body_entered(body: Node2D) → void:

if body.name == "Player":
	print("pegou")
	cont.cont_moedas = +1
	queue_free()

if has_node("player"): #Debug
	print("Nó encontrado!")
else:
	print("Nó não encontrado!")

pass # Replace with function body.

Player:

extends CharacterBody2D

var SPEED = 150.0
const JUMP_VELOCITY = -400.0
var cont_moedas = 0

@onready var _animated_sprite = $AnimatedSprite2D

func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("ui_left", "ui_right")
if direction:
	
	if Input.is_action_just_pressed("B_correr"): #função experimental
		velocity.x = direction * 5000
	else:
		velocity.x = direction * SPEED
	
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	
if Input.is_action_pressed("ui_left"):
	_animated_sprite.play("AA_esquerda")
	
if Input.is_action_pressed("ui_right"):
	_animated_sprite.play("AA_direita")
	
	
if direction == 0:
	_animated_sprite.stop()
	


move_and_slide()
1 Like

Is Player a child of coin scene ?
Please share screenshot of your scene tree.

1 Like

Coin Tree:
image

If I sent the wrong image file, let me know and I’ll send you the right one. Thank you very much for helping me. I know how to make programs in C++.
Game creation and everything else is new to me.

1 Like

player tree:
image

main tree:
image

1 Like

In your coin script replace @onready var cont = get_node(“player”) to @onready var cont = get_parent().get_node(“player”)

You can learn more about get_node() here : Node — Godot Engine (stable) documentation in English

2 Likes

EN -
Also, an alternative would also be to add the Player to a group, and use this, the effect is the almost exactly same

Pt-BR -
Uma alternativa também seria adicionar o Player a um grupo, e usar este comando, o efeito é quase exatamente o mesmo

@onready var player = get_tree().get_first_node_in_group(“Player”)

2 Likes

Hello! Good morning, I managed to make it work with this method! Before it hadn’t worked because I hadn’t put the player in a group hahaha Thank you very much for your help!

2 Likes

En -
Hello! Good morning, it was thanks to this get_tree() that I understood that my mistake was not putting the player in a group hahahha
Thank you very much for your help!!!

pt-BR -
Olá! Bom dia, foi graças a esse get_tree() que havia entendido que meu erro foi não colocar o player em um grupo hahahha
Muito obrigado pela sua ajuda!!!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.