+1 vote

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!

in Engine by (19 points)

1 Answer

+1 vote
Best answer

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):
    #code
    return new_damage_stat

It would still be the same level of efficiency.

by (3,201 points)
selected by

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.)

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.

"only call the function if the variable equals 0"

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.