Calling a variable from a scene or script

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

Hi everyone,

i would like some explanation on how to call variables from different scenes and scripts into other. i’ve been trying to make a hit box dependant on stats changes,below is the code so far :

extends Area2D

var hit_direction = Vector2.ZERO
onready var stats_values = PLAYERSTATS
#variable acessed by all children nodes ( BOXES )
onready var damage = 1

func final_damage_value() :
	damage = damage * stats_values.strength
	
func _physics_process(delta):
	final_damage_value()

That’s a node (HITBOX) with a script of the same name, i’m trying to import variables from (PLAYERSTATS) in order to change “damage” variable wich is currently affecting the health of enemies.currently the damage set to 1 responds ok to the code, but i can’t make formulaes.

I’ve tried the option “Autoload” in project settings but its not working so far,been struggling to understand how it works on Godot, even though i read the documentation

THANKS for any help or explanation!

:bust_in_silhouette: Reply From: Wakatta

Firstly forget about scenes and scripts those are terms used in the Editor portion of Godot.

During gameplay each node in the sceneTree is considered an instance and has a unique nodepath, instanceID and name

Each variable in the script attached to a node is called a member during runtime and can be accessed as well as functions from that node if referenced using one of the previously mentioned unique ways

Let’s say you have this sceneTree

┖╴root
   ┖╴Maingame
       ┠╴HITBOX(Area2D) [Hitbox Script]
       ┃  ┖╴Node2D
       ┖╴PLAYERSTATS (Area2D) [Player Stats Script]
           ┖╴Control

Once you have the reference to the PLAYERSTATS node

onready var stats_values = get_parent().get_node("PLAYERSTATS")

you can access or change all of it’s members directly by using that variable with .member_name

Example

damage = stats_values.damage

Thanks Wakatta,

I’m gonna try it!

Pedro Henrique | 2021-08-08 14:59