Hi, glad your trying to learn godot.
first of all please mark your code as such like this:
if test == true:
return "sucess
```gdscript
if test == true:
return "sucess
```
Now regarding your actuall problem. I would suggest you do some debugging.
Modify your functions as such
func _on_detection_area_body_entered(body: Node2D) → void:
print("Detected Player") #DEBUG
print("Players currentConfuse is: ", global.player_current_confuse) #DEBUG
player = body
if global.player_current_confuse == false:
evade = true
if global.player_current_confuse:
confused = true
func _on_detection_area_body_exited(body: Node2D) → void:
print("Not detecting player anymore") #DEBUG
player = null
evade = false
confused = false
The print messages will show up in the console so you can see what exactly the problem is. Delete them if you fixed it.
If you run your game like this you will see:
- Is your player correctly being detected
- is your confuse value correctly set
Something I just noticed:
Do you really want the Enemy to be able to be in both evade and confused mode at the same time? because as of current this is possible. This will cause really weird movement behaviour becaue both the confusedDirection and the direction will be applied at the same time.
If you do not want this you have to do some minor or major changes to your code.
Do you want to have more that just the current 2 movement modes later?
if yes you can make an elif hirachy like this:
if confused:
var confused_direction = (position + player.position).normalized()
position += confused_direction * SPEED * delta
elif evade:
var direction = (position - player.position).normalized()
position += direction * SPEED * delta
current_speed = lerp(current_speed, 0.0, acceleration * delta)
Now the Enemy will only evade if they are not confused
if you only have 2 movement modes: Dont use 2 booleans use 1.
For example remove confused and only make it evade then do:
if !evade:
var confused_direction = (position + player.position).normalized()
position += confused_direction * SPEED * delta
else:
var direction = (position - player.position).normalized()
position += direction * SPEED * delta
current_speed = lerp(current_speed, 0.0, acceleration * delta)
And replace all statements where you said confused = true
with evade = false
This will spare you with additional confusion.
Something else to consider. Using a global for this usecase is rather unortodox. It means that if your Player presses the confuse button all enemys with your NPC script will be confused. It also means that all scripts and methods you ever use will be able to acess the player_current_confuse variable.
If you dont mind that, your approach should be fine.
I personally would probably prefer using godots signal system for things such as this. I dont know your game idea so I cant exactly tell you what to do there but you could for example define a
signal changedConfuseState(confused)
in your Player or in your confuse area.
If the enemys have a reverence to that area or to the player (depending on where you defined the signal) you can subscribe to it as such:
var _amIconfused: bool
@onready confusedDetector : Area2D = $DRAG_IN_YOUR_NODE_FROM_SCENE_VIEW
func _ready() -> void:
confusedDetector.changedConfuseState.connect(onConfuseStateChanged())
func onConfuseStateChanged(confused: bool)
_amIconfused = confused
This would have several advantages:
- you can easily make enemys start or stop to ignore or not ignore confusion by subscribing or unsubscribing form the signal with connect() and disconnect()
- You dont add more and more clutter to 1 central global class
- You can easily give the Confusion multiple parameters for exaple like a confusion time after which the enemys will automatically unconfuse themselfes.
- Probably some others that I am forgetting
But it is a little more complicated so if you are just looking for the simpelest possible solution you might be good with the global class. Although going for the simplest solution can mean that it will get more complex later on depending on your type of game. There is also a chance that the global class is simply the best alternative you have I just wanted to tell you that the signal option exists.
I wish you good look with your game and hope you find the error 