Help on trying to share variables between scripts

Godot Version

4.6.1

Question

Hello so I have a problem where I need to share variables between scripts.

I have a pistol that inherits from class Weapons and I am planning to have the different types of guns to shoot different bullets. I have a bullets that has a damage variable and I need to share that to the Enemy class so i could have that bullet deal damage to all enemies that inherit from this class is there any way I could do that, so far everything I have tried has failed.

What have you tried? How did they fail? Could we see your code?
We cannot help much unless we know what you tried so far or what your project even looks like.

Generally though you can simply put some code on the bullet itself that says “If I collide with a class that can be damage, call it’s damage() function.”

I’ve tried with singletons and globals but i discarted these ideas.

extends Node3D

var bullet_speed: float = 100.0
var damage: float = 10

@onready var mesh = $MeshInstance3D
@onready var hitbox = $Area3D

func _physics_process(delta: float) -> void:
	position += transform.basis * Vector3(0,0, -bullet_speed) * delta
	



func _on_area_3d_body_entered(body: Node3D) -> void:
	if body is Enemy:
		print("bullet deleted")
		queue_free()

this is the bullet script whose damage i have to transfer to the Enemy class

@abstract
class_name Enemy extends Node3D


@export var enemy_stats: Enemy_Stats

@abstract func Take_damage(damage)

and this is the Enemy absract class which needs to use the damage on the bullet. I only have a target dummy for now which is why the Enemy class is so empty for now.

So what is it about this apprach that doesn’t work? You have a Take_damage function which at the moment takes one argument, but can take as many as you wish. I suspect that’s what you meant by “share variables”, or am I wrong?

Well the damage is just a parameter, it’s not using the damage from the bullet script.That’s what i am trying to figure out.

In your bullet script, you need to call said function.

func _on_area_3d_body_entered(body: Node3D) -> void:
	if body is Enemy:
        body.Take_damage(damage)
		print("bullet deleted")
		queue_free()

Ok, It’s still not working you are correct, but now I notice that the problem is that it’s also not detecting the collision with objects with the Enemy class I noticed that because the bullets weren’t colliding the bullets deleted didn’t pop up in the console and after i removed the if statement that checks wether or not it’s an Enemy class it was popping up “bullets deleted” in the console so the problem might also be in the detection.

What class is your enemy? Since the abstract class is just a Node3D, which is not considered a body.

class_name Target_Dummy extends Enemy


func Take_damage(damage):
	enemy_stats.Health -= damage
	print(enemy_stats.Health)

This is the Target_Dummy script it inherits from the Enemy class. What should I change the abstract class to if I want collisions.

If this is purely for enemies that move around, it should probably be something like a CharacterBody3D or a RigidBody3D etc.. depending on what you need.