Is it possible to do a damage calculation on a global script? or is there a better way?

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

Hi, so i want to know if its possible to create a global script that handle all damage calculation from any attack.

like taking stats variable from the attacker and the target, then calculate the damage then send back the damage value.

I did have a working damage system by having every objects calculate their own stats and damage when attacking but its very very tedious and inefficient.

thank you!

:bust_in_silhouette: Reply From: Magso

You can call a function in an autoload script but it would still need all the variable values through the arguments and any signals would still have to be connected on each object in order to call the autoload function for example

var damage : float
func _body_entered_signal(body):
   damage = autoload_global_script.damage_function(damage)

The autoload global script.

func damage_function(damage_stat):
    return new_damage_stat

It would still be the same level of efficiency.

thank you!, im a bit stumped on how to prevent stats variable (on autoload script) to be overwritten when there are multiple object attacking at the same time.

for example, when A (atk = 5) and B (atk=8) attack the player at the same time the damages dealt are both 5 or 8 (instead of 5 and 8) , my autoload script can only hold stats for 1 attacker (collect attacker stats (as aatk, adef, etc.) and 1 defender (.as batk, bdef, etc.)

thatreshaad | 2020-03-13 02:49

There are 2 ways. Either disable the collider that registers the attack given that it’s not the main body’s collider, or create an attack variable on the autoload so instead of passing an argument, only call the function if the variable equals 0 and set the variable to the attack value. Once the attack is finished reset the variable to 0.

Magso | 2020-03-13 08:33

“only call the function if the variable equals 0”

duh, why i didn’t think of that, thanks!

thatreshaad | 2020-03-13 13:16