0 votes

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!

Godot version Godot_v3.3.2-stable_win64
in Engine by (15 points)
edited by

1 Answer

+1 vote

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
by (6,934 points)

Thanks Wakatta,

I'm gonna try it!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.