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()
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?
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?
Well, the conditions says that it should be true if the body is the player andGlob.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?
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…
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
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
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.
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
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.