The Godot Community Forums are back up and running! Please read the the announcement post for important information on what happened. Thanks!

Can we retrieve in which instances "live" a variable ?

Hello,

i have an ennemy with a Health variable:

Ennemy.gd

extends Area2D

var health : Health

and in my Health class code i would like to retrieve the ennemy instance where the health variable is "living"

Health.gd

extends Reference             

class_name Health

func get_owner():
    #how to get the ennemy that "own" this Health instance?

for the moment i can give the ennemy instance in Ennemy.gd like this:

Ennemy.gd

extends Area2D.  

var health : Health

func _ready():
    health = Health.new()
    health.set_owner(self)

i was wondering if there was already a built-in method for Reference (or another class for this) that could retrieve the "owner" where the variable is "living" or if my method is the only way to retrieve the "owner"

Thanks

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 3,141Admin

    Welcome to the forums @skeleton60!

    I don't think there is anything built in to get the owner of a resource, because a single resource can be used by multiple scripts and nodes. For example, if you copy and paste a collision shape (or any Godot reference), every pasted-copy is the same reference, which is why when you alter the settings on one they all change. This makes getting the owner node(s) difficult, as any given resource could have n owners.

  • I understand, thanks for explanations

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file