Getting var of parent of area?

Godot Version

4.3 stable

Question

I have two objects. A and B

Object A has a var called power set to 10.0.
It has an area that can overlap other objects.

Object B has a var called power set to 0.0.

inside object B I have this code:

var power : float = 0.0

func _on_area_3d_area_entered(area: Area3D) -> void:
	if area.is_in_group("PowerSource")
		power = area.get_parent().power

however… it says that this cannot be done!

Invalid access to property or key ‘power’ on a base object of type ‘Node3D (script_powerreceiver.gd)’.

Anyone know how to get this var out of object A? :flushed:

Have you spelt the power variable differently in object A?

No. And I hmm wait 5 seconds before it tries to get the Var. So no chance it hasn’t inited yet. Strange.

Is object B getting collisions other than object A? If it is, make sure the script knows that the area_3d.get_parent() has to be object A for it to try and get the power variable.

If I debug and do a
Print(area.get_parent().name)

It yields the correct asset that should have the power Var.

So I guess collision and overlap and all that works.

obj A looks like this:

extends Node3D
@export var power : float = 10.0

it has an area and a collision shape. Both set to “powersource” group.

obj B has an area with collision shape as well. The area scales up and covers the area of obj A every 5 seconds. (this works! since obj B can print the .name of the parent of obj A)

obj B looks like this:

extends Node3D
@export var power : float = 0.0

func _on_area_3d_area_entered(area: Area3D) -> void:
	if area.is_in_group("PowerSource"):
		print(area.get_parent().name)
		power = area.get_parent().power <----------------------------FAILS! :worried:

func _process(delta: float) -> void:
        #doing the things that i dont need to write here, since it works. 

Does anyone know why that line would fail to get the power value of that area object?

Adding a timer may not be sufficient to allow a child node to complete.
You might be able to test If that is the issue by
print(get_parent().is_node_ready())

If it isn’t the issue then it could only be that despite printing the correct name the node being garnered by get_parent() is not the one you want.
Double check the remote tab while the game is running to see your scene structure.

NOTE: I was wrong.

I did some testing on this and area.get_parent().my_variable works fine on all of my tests. Removing my answer, but I think you have a referencing problem. ie power is not defined in your parent, or your parent is not what you think it is.

I thought maybe get_parent was not providing a way to access keys, only properties, but this is not the case.

1 Like

Can you show your scene organization?

Don’t you need to do @onready someplace on all objects in group?

I’ll try this. Thanks! :person_gesturing_ok::person_gesturing_ok::person_gesturing_ok: