Christos Rymenidis
Christos Rymenidis

Reputation: 13

GODOT Can not access Packedscene's child from another Node using Signals or Getnode

So I want to set values, like scale, of a packedscene from the editor from another scene. I want "Candlestick" to set the scale of "Top" which is within "Body Hollow" (see below).

The problem is when I run "Body Hollow" scene everything works perfect. But, when I run "Candlestick" it just doesn't work. I have tried using $Node instead of Signals it grabs the "Body Hollow" node, but then I get a null error on calling body_hollow.scale_x (ofcourse I slighty change the code). The point is it acts as if "Top" does not exist!

I am trying to find an answer to such a simple problem and yet I can not.

Please help me find a solution. I am going crazy! Thank you in advance!

Handlestick Tree Body Hollow Tree

# Attach this to Candlestick
class_name Candlestick
extends Node2D

@export var body_hollow: BodyHollow

func _ready():
  print("Candlestick is running...")

func _process(_delta):
  if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
    body_hollow.scale_x(200)
# Attach this to "Body Hollow"
class_name BodyHollow
extends Node2D

signal scale_changed(_x: float)

func scale_x(_x: float) -> void:
  scale_changed.emit(_x)

func _process(_delta):
  if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
    scale_x(200)
# Attach this to "Top"
class_name BodyHollowHandles
extends Sprite2D

# Connect this "scale_changed" signal from "Body Hollow"
func _on_body_hollow_scale_changed(_x):
  scale.x = _x
  scale.y = _x

Upvotes: 1

Views: 404

Answers (2)

M. R. M.
M. R. M.

Reputation: 653

From the images, It doesn't seem that you've added an instanced of the Body Hollow scene into your tree rather you've added the node of class BodyHollow

enter image description here

What's the difference?
I'm guessing you added the BodyHollow from the 'Create New Node' menu as such

enter image description here

Whereas you should have been dragging and dropping the BodyHollow.tscn scene from the file system into your tree as such:

enter image description here

Upvotes: 0

Cassiano
Cassiano

Reputation: 126

It seems the issue comes from this line:

@export var body_hollow: BodyHollow

This line by itself does nothing. It will just declare an empty variable. You need to get the child node itself.

Changing this line to:

@onready var body_hollow: BodyHollow = get_node("Body Hollow")

should solve the issue.

Upvotes: 0

Related Questions