How do i reference a variable from another resource script in my calculation?

Okay. I think I understand your goal now.

Achieving your goal

You want the damage of a skill to be influenced by one or more of the three attributes: strength, dexterity, and intelligence. You want to be able to configure which attributes are taken into account.

One way of doing this is to create an enum that is based on the available attributes (see docs for description).

enum Attributes {
	STR,
	DEX,
	AGI,
	INT
} # The naming is shortened to make it faster to write but it's up to you.

Then you can expose a list in Godot’s inspector that allows you to pick which stat(s) influences the damage output.

class_name SkillStat

@export var stat_influences : Array[Attributes]

Now that you have a list of influential stats, you can iterate over the list of attributes, get the stat for each attribute from the class, and compute the correct damage.

func calculate_damage(class_stat: ClassStat):
	var total_stats = 0

	# Iterate over all the influences
	for i in stat_influences.size(): 
		if stat_influences[i] == Attributes.STR:
			totat_stats += class_stat.strength
		if stat_influences[i] == Attributes.DEX:
			total_stats += class_stat.dexterity
		if stat_influences[i] == Attributes.AGI:
			total_stats += class_stat.agility
		if stat_influences[i] == Attributes.INT:
			total_stats += class_stat.intelligence

	# Compute the damage
	var damage = base_damage * total_stats
	return damage

Additional comments

Yes, that is correct. You seem to already have an understanding of the reductionistic principles of programming; that each object is often comprised of smaller, more specialized objects. If you’re interested, I would recommend that you study inheritance and composition in programming. These concepts are vital to understand if you want to create big systems.


Also correct. Actually, exchanging your arrows for dot notation gets you pretty close to the actual code you would write.

var weapon: Weapon
var damage = weapon.skill.calculate_damage(self)

Well, this is the same as your previous arrow-based example just at a different scope, and a damage-dealing operation at the end.


You could indeed implement health in your ClassStat. However, if you plan to build a game that is more than small in size, you should not rely on such an approach. More often than not, health as a concept is used for a wide set of objects in games. To avoid writing repetitive code (the same code in multiple scripts), you would write a Health class. This class/node would then be added to objects via composition. Other nodes can then subscribe to Health’s signals (damage, heal, or death signal) to retrieve information from the Health instance.

You can find an example of a networked health node I created at the bottom of this post. The post in its entirety is concerned with composition and inheritance.


One last thing. You have to remove your habit of saying sorry for not being good enough. There’s nothing wrong with being a beginner. Be excited that you have any programming knowledge at all!

Reserve saying sorry for times when you realize that you haven’t given something or someone the correct amount of energy or attention.

I hope this answered most of your questions. Let me know if you have any more questions.

1 Like