Creating and exporting resources between scenes using class_name

Godot Version

v4.5.1

Question

I’m working on a turn-based combat system for my game where the player and enemy choose attacks at about the same time, which are compared against each other in a rock/paper/scissors type way. That much is working, but the issue comes when I try to calculate the damage.

The stats for the player and enemy are defined in the same way in both scripts, so here’s the enemy script:

extends CharacterBody2D
# I haven't reset any of the values yet at the start of the turn, I'll do that once the damage calculation works
class_name Enemy

# attack type signals
signal charming
signal intimidating
signal knowledge

# stats
@export var maxhealth = 200
@export var health = maxhealth
@export var charm = 10
@export var intim = 7
@export var know = 6
@export var damage = 1.5

# choosing move type and stopping spam variables
var spamstop = true
var movechoice
var movetype = KNOW
enum{KNOW, CHARM, INTIM}

# Choosing an attack type
func _process(delta):
	if spamstop == true:
		if health > 100:
			if Input.is_action_just_pressed("Attack"):
				spamstop = false
				movechoice = randi() % 10
				if movechoice < 8:
					movetype = CHARM
					$AnimatedSprite2D.play("charm")
					damage = damage*charm
					emit_signal("charming")
				else:
					movetype = KNOW
					$AnimatedSprite2D.play("know")
					damage = damage * know
					emit_signal("knowledge")
		elif health <= 100:
			if Input.is_action_just_pressed("Attack"):
				spamstop = false
				movechoice = randi() % 10
				if movechoice < 8:
					movetype = INTIM
					$AnimatedSprite2D.play("intim")
					damage = damage * intim
					emit_signal("intimidating")
				else:
					movetype = CHARM
					$AnimatedSprite2D.play("charm")
					damage = damage * charm
					emit_signal("charming")

And the damage calculator:

extends Node2D

# letting the damage be re-exported
class_name Damattack

# getting the player stats
@export var playerstats: Player
@export var enemystats: Enemy

# attack variables
var player_attack = KNOW
enum{KNOW, INTIM, CHARM}
@export var enemattack = 1
@export var playattack = 1

# attack signals
signal damage

# collecting enemy inputs
func _on_battlereynard_charming():
	if player_attack == CHARM:
		playattack = playerstats.playerdamage/2
		enemattack = enemystats.damage/2
		emit_signal("damage")
		print("draw")
		print(playattack)
	elif player_attack == KNOW:
		emit_signal("damage")
		print("win")
	elif player_attack == INTIM:
		emit_signal("damage")
		print("lose")

I haven’t put in all of the calculations yet, just working on the “charming/draw” calculation first and making sure that functions. The signals seem to work, as all the printing happens properly, but when I run this in the debug I get the error “Invalid acces to property or key “playerdamage” on a base object of type “Nil”.”.

I think this means that the player/enemy scripts aren’t exporting their variables properly for the damage calculator to “read”, but I’m not sure how to fix it. I’m fairly new to coding so any help would be useful!

Where are you setting the stats variables?

@export vars are initially defined in the editor. If you haven’t set the variables there it could result in the errors you are seeing. “Nil” is an indication that something is undefined.

I think that might have been the issue, I found that I hadn’t set things properly in the editor. Thank you!

Doesn’t seem like you are exporting resources, only nodes. Did you make sure to assign these export variables in the inspector panel or if you player/enemy are spawned mid-game then @export won’t help you much and you’ll have to assign the variable as usual in scripts.