How inherited variable behaves across scripts extending same parent node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By skysurveil

When a variable is declared and assigned a value in a parent node, for example, Actor.gd, and then there are two nodes/scripts extending Actor, I understand both nodes extending Actor have access to its defined variables but how exactly do they interact?

I am following the GDQuest First 2D Game tutorials for context.
The Actor node sets a value to velocity and gravity.
Two nodes that extend Actor, are created, Player and Enemy.
Both Player and Enemy can use velocity and gravity.
Velocity is changed in both Player and Enemy.
The changes do not appear to impact the other script.
Is this similar to pass by value in C++ where the original value is not changed (the value in parent node)?

TL;DR:
Does extending Actor give access to its variables but if the variables are updated within each of the child nodes, they are only updated locally and do not interfere with the sibling nodes?

:bust_in_silhouette: Reply From: Wakatta

Your assessment is correct so far except that inherited Actor nodes only inherit variable and functions not state changes.

Think of Actor.gd as a blueprint for how Player and Enemy are to be created and behave, however once created each is independent of each other with it’s own set of variable values.

If your intentions are to have multiple behave the same way try using groups

:bust_in_silhouette: Reply From: faulknermano

I’ve just learned something about this, and I thought I’d weigh in if only for reference.

E.g. in defining Actor:

class_name Actor
var velocity:Vector2 = Vector2.ZERO

If I don’t declare this as an instance variable, if I try setting this velocity value in any of the child class instances, e.g. Player, Enemy, those objects will get the same value across them.

In order to mark this variable as an actual instance var, I found out I needed to use _init():

func _init():
    self.velocity = Vector2.ZERO

Unlike Python, where a class var is declared under the class body, not in the methods (i.e. __init__), in GDScript you need to first declare it as a class var (outside funcs), and then mark it is an instance var afterwards, and usual/best place to do it is in the _init() callback, but I think anywhere else appropriate is ok.