Problem due to readung global variable

Godot Version

4.5.dev1

Question

I am lost to understand why am I getting following mistake:I defined a trap which get on when player moves trough it: when player go trough It works ($LosePlaying playes), but after short time (few seconds) It playes again…

extends Node2D
var electricON: bool

func _ready():
	$AnimationPlayer.play("electric column")
	print('_0_=')
	print(str(Glob.electricON))
	#print(Glob.is_blocked)
	#print(Glob.provavar)

func _on_area_2d_body_entered(body: Node) -> void:
	#print('body is: ',body)
	
	if body is PlayerController and !Glob.electricON:
		Glob.electricON= true
		$LosePlaying.play()

Hi,

Can you please be a bit more specific? What’s the exact error message, and on which line is it triggered?
Also, can you share the Glob script too?

Honestly I don’t get any error message; no line trigger any error.The Glob script is the following:


extends Node

var is_blocked = false 
var electricON = false

probably in future I will be going to add some others variable; for now it is quite simple…

Just saw you edited your initial message.

So, the issue is that, you enter the trap, the _on_area_2d_body_entered function is called (as it should), and after a few seconds, even though the player hasn’t leave the trap, the function is called again, right?

no, the player left the trap but It looks like _on_area_2d_body_entered(body: Node) get fired again…

Okay, thanks for the info. Looks like you already added a print print('body is: ',body): is the body triggering the trap again the player too?
Or is the trap triggered by another body?

this is the code i have:

if body is PlayerController and !Glob.electricON:
		Glob.electricON= true
		$LosePlaying.play()

This should assure me that It is run only one time…am i right ??but LosePlaying is played twice

Well, the conditions says that it should be true if the body is the player and Glob.electricON is false. From the code you’ve shared, it seems that Glob.electricON has not been reset to false, which is why I’m wondering if the body triggered the second collision is the player.
As asked just above, could you uncomment the print instruction to check what is colliding with the area the second time?

ok, so. this is the code:

if body is PlayerController and !Glob.electricON:
		print('body is: ',body)
		$LosePlaying.play()
		Glob.electricON=true

after playing the sound effect I set Glob.electricON=false again. What is printed is:
`

body is: Player:<CharacterBody2D#58552485218>

`
But It is possible listening the sound playing twice…

Did I answer your doubt ?

Not gonna lie, I’m a bit lost.

You have a code that’s triggered after a player collision and if Glob.electricON is set to false, which works fine at first.
The second time, according to what you’re saying, the variable Glob.electricON has been reset to false, and according to the print (body is: Player:<CharacterBody2D#58552485218>), the collision comes from the player. Since both conditions are met to play the sound, it doesn’t seem like a bug to me…

Could you record a video of the issue?

No,no. Maybe I just wrong to type.
I have this coded:

if body is PlayerController and !Glob.electricON:
		print('body is: ',body)
		$LosePlaying.play()
		Glob.electricON=true #This set the flag properly to avoid a 2nd call

There’s a contradiction here, you said:

and in the code you write:

Glob.electricON=true #This set the flag properly to avoid a 2nd call

Where are you resetting the flag to false, as you mentioned earlier?

surely I set to false previously; now i just set it to true:

func _on_area_2d_body_entered(body: Node) -> void:
	if body is PlayerController and !Glob.electricON:
		print('body is: ',body)
		$LosePlaying.play()
		Glob.electricON=true   # to avoid $LosePlaying is played again

Okay so, again, if the second collision comes from the player (according to the log body is: Player:<CharacterBody2D#58552485218>), that would mean that Glob.electricON is reset to false somewhere.

hi… I spent several time for looking where electricON 'd reset to false…but i found nothing about. the only doubt is about maybe the global script get executed again ?mmmm I don’t know…

If you have any line where electricON is set again to false, you should find out when/from where/why it is called. A good way to do that is to use breakpoints.

Put a breakpoint on the line(s) where electricON is set back to false to investigate.

In case you are not familiar with breakpoints and debugging (that’s a useful tool and skill to learn !), GDQuest made a good tutorial on Godot for that : Getting started with debugging in Godot · GDQuest

ok, the problem is I didnt find any line where electricON is set again to false.However thank you for the. debugging tutorial

Also, you already have an electicON variable in the first script you posted, why are you using the electicON variable from Glob instead of setting/accessing the variable from the same script ?

And if you could show your scene tree, explain which script is on what node, and how do you initialize Glob, that would help.

I removed the electricON variable defined within the script linked to the Node2D ElectrizityTrap:

So the only one I access to is variable from Glob .I choice to do so for own organization purposes.
The undesidered behaviour is still present.
I initialized Glob by going in progect settings, globals pannel and defining file glob.gd within which i defined following variables:

extends Node

var is_blocked = false 
var electricON = false

Also , I set name: Glob for res://glob.gd

You mentioned before that this is what is printed when you enter the trap.

But is it printed just once, or twice?
Because if it’s printed just once, as I would expect it to, then the problem is not with this piece of code, but somewhere else. Maybe your $LosePlaying node is looping the audio, or something along these lines.

1 Like