How to pass multiple objects for calculating damage on hit?

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


I don’t know the best practices for passing objects or accessing them in the case above.

I have a Player which has a ClassStat (Resource) attached to it.
The ClassStat provides the health, defense, attributes…
The player has a Weapon (Sprite, …) which has a WeaponsStat (Resource) attached to it. The Weapon can fire Bullet, which has a hitbox (Component).
The WeaponStat provides the damage…

An Enemy also has a ClassStat providing the defense. A hurtbox (Component) is attached to it and reacts to the hitbox.

I have static functions that calculate the damage taken passing an Attacker, a Weapon and a Defender.

My question is: how to calculate the damage taken when the hitbox collides with the hurtbox? I need the Player’s Class Stat, the Weapon’s WeaponStat and the Enemy’s ClassStat. How should I pass the different items without having to pass them to multiple objects?

I’m sorry if this is not clear or if it is missing information to help you understand.

:bust_in_silhouette: Reply From: crossbito


I would create functions like getAttack() and getDefense() , you would need the defense of the node receiving damage and the attack of the object dealing damage. Here’s an example implementation:

func calculateDamage(attacker, defender):
    var attack = attacker.getAttack()
    var defense = defender.getDefense()

    # Calculate the damage dealt and return it
    var damage = attack - defense
    return damage

For your bullet (attacker), you can implement a getAttack() function that references the weapon that created the bullet. The weapon, in turn, would have its own getAttack() function and a reference to the player, where you can get the player’s attack value. Here’s an example:

# Bullet
func getAttack():
    return self.attack + weapon.getAttack()

# Weapon
func getAttack():
    return self.attack + player.getAttack()

# Player
func getAttack():
    return self.attack

In this system, the attack value of the bullet is calculated by summing the attack value of the bullet itself and the attack value obtained from the weapon. The weapon’s attack value is determined by adding its own attack value and the attack value of the player. The player, in turn, simply returns its own attack value.

Now, you can apply the same concept to the enemy. If the enemy has armor, you can calculate the defense by combining the defense from the body with the defense from the armor.

With this approach, you can consistently calculate damage by considering all the effects of bullets, armor, weapons, and other factors. By traversing from one node to another and accumulating the effects, you can accurately calculate the final damage or defense.

This is one approach, but you may be able to find something even better for your specific system.

Thank you for your answer.

In your solution, does it mean that the bullet should carry a reference to the Player (and Player’s ClassStat) and a reference to the Weapon (and Weapon’s WeaponStat)?

Something like, Weapon)?

denisgames | 2023-06-10 10:29

No, in my example, the bullet only needs a reference to the weapon, and the weapon has a reference to the player. The idea is to only have a reference to the node that calls us, and from there, you can obtain data. Bullet → weapon → player. This is useful because it allows you to avoid creating references for everything inside everything.

crossbito | 2023-06-10 12:49