Global Variable not being transferred

:pray: ## Godot Version
v 4.3

Question

This is my first time coding a project and I just learnt that global scripts were a thing, i need help because i’m not sure what’s going wrong.


I have set up a global script, the code attached to it looks like this:

extends Node

var goblin_attack = false

func test():
	if goblin_attack == true:
		print("global_signals works")

I am trying to use this code the transfer information across two scripts. From goblin.gd:

	if melee_attack == true and player != null:
		GlobalSignals.goblin_attack = true
		print("Attack")
		$AnimatedSprite2D.play("Attack")
		goblin_attack_cooldown = false
		$"Goblin Attack Cooldown".start()
		player_chase = false
		$"Run Animation Cooldown".start()
		melee_attack = false

To player.gd:

func enemy_attacked():  
	if GlobalSignals.goblin_attack == true:
		goblin_damage = true

func take_goblin_damage():
	if goblin_damage == true:
		$"Goblin Animation Wait".start()

func _on_player_hitbox_body_entered(body: Node2D) -> void:
	if body.has_method("goblin"):
		enemy_inattack_range = true


func _on_player_hitbox_body_exited(body: Node2D) -> void:
	if body.has_method("goblin"): 
		enemy_inattack_range = false


func _on_goblin_animation_wait_timeout() -> void:
	if enemy_inattack_range == true:
		health -= health - 5
		print(health)
		GlobalSignals.goblin_attack = false

So I know that the line of code on goblin.gd is being read adn my character is doign the attack animation, but for some reason the information isn't being transferred to the globalsignals script as "global_signals works" isn't being printed, nor is the health on player.gd script.

All 3 scripts are on the scene being laoded, so I don't know what's wrong, Any help will be appreciated :pray
![image|263x452](upload://qdgz6MPlhQVXjIxKJTFBDL9roP0.png)

There are a dozen ways this could fail, depending on variable contents. The problem is most likely somewhere in some combination of conditionals. Try something without any conditionals until you understand the underlying mechanism.

I believe the problem is happening because of

func _on_goblin_animation_wait_timeout() -> void:
	if enemy_inattack_range == true:
		health -= health - 5
		print(health)
		GlobalSignals.goblin_attack = false

more specifically, the line GlobalSignals.goblin_attack = false, I can reach a conclusion as I lack more context, but, if you are changing the goblin_attack value back to false before the methods that validate it to be called, your program will never pass the condition.

Anyway, a Global script isn’t the best solution to a problem like that, change it to use signals. Signals follows the Observer design pattern where each script that wants to know a value connects to the script that emitts the value, and when the value is emitted, each conected script will be triggered.
Here there’s some resource about signals:

1 Like

Thanks for the sources, i understrand how to use signals now and I figured out how to make my code work

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.