0 votes

i made this spawner code:`

func _on_spawntime_timeout() -> void:
    var px = rand_range(1000 ,0)
    var py = rand_range(0,1000)
    var loc = Vector2(px,py)
    var e = enemy.instance()
    add_child(e)
    e.position = loc

i want the enemy instance same as enemy scene like if i change the damge from the main scene it will change for both enemy instance and the orginal enemy

func _on_hitbox_body_entered(body: Node) -> void:
    if "bullet" in body.name:
        $enemyhealthbar.value -= dmg

this is the code for the enemy dmg

var dmg = 25
in Engine by (12 points)

2 Answers

0 votes

Make a setter function for the variable.
When you will need to change the variable value, just call it and it will set dmg variable in all of your instances.

Create a list with all of your instances, then in the setter function for each of the enemies set dmg in each of them.

by (54 points)
0 votes

If i understanded your logic, the best way is put the enemies in a array or something and create a logic to set the value in the elements of array. Also you can script a foreach with scene children and identify the enemy by some method with has_method.

If you want a more automatic logic you can create a signal in the main scene that triggers every time that the damange is calculated and connect that signal in the enemies.

by (171 points)
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.