#on_area_exited makes on_area_entered stop working

1 messages · Page 1 of 1 (latest)

sharp nova
#

On area entered, my player script grabs area information and updates a variable named current_area_info. On area exited it sets the current_area_info back to default. For some reason after entering an area, my area info updates as expected, and upon leaving it changes back to "none" as expected... but if I go back into the area it doesnt update again, it continues being "none".

perhaps my script may make it more apparent what i mean.
Area script: ```swift
@export var Transporter_name : String
@export var Transporter_destination : Marker2D
var transporter_area_info : area_info

func _ready() -> void:
transporter_area_info = area_info.new()
transporter_area_info.area_name = Transporter_name
transporter_area_info.destination = Transporter_destination.global_position

func get_transporter_info():
return transporter_area_info```

Player script: ```swift
var current_area_info : area_info

func _on_sensor_area_entered(area: Area2D) -> void:
# Transporters
if area.get_parent().has_method("get_transporter_info"):
var area_node = area.get_parent()
current_area_info = area_node.get_transporter_info()

func _on_sensor_area_exited(area: Area2D) -> void:
current_area_info.area_name = "none"```

hexed phoenix
#

Where do you define area_info? The script above?

#

Worth noting, that the player would need to enter the other area AFTER existing the previous one.

Because then it enters the area and gets the new name, but if it exits the previous one, it gets set to "none" again. Even if it is inside the other.

sharp nova
sharp nova
#

if i remove the on exited function, theyre not one time use, theyll update each time you step back and forth between the areas?

hexed phoenix
#

Did you test it or are you asking if it would work that way?

sharp nova
#

ive tested it, its so strange

hexed phoenix
#

It may be what i suggested.
The exit part happens after entering the new one.
Which makes sense if they are close ot each other.

sharp nova
#

i genuinely dont get it...
if i step in and have it print the current_area_info.area_name it prints whatever is in the export variable of the area script
if i step out and have it print, it prints "none"
if i step back in and have it print? "none"

hexed phoenix
#

Imagine you're entering another room.
At one point you're standing in both. Then you finally make the next step and exit the previous one.

#

But you enter the next room BEFORE exiting the previous one

sharp nova
#

im testing it with only one area

#

i just want to have it so that if im in an area, the area_name is whatever the areas script says it is, and if i exit the area it goes back to a default

#

which ive got up to

#

but you cant re-enter the area

#

well you can but it wont update the area name

hexed phoenix
#

So even without the on_exit function it still doesn't work more than once, right?

#

Are your connections set to CONNECT_ONE_SHOT?

sharp nova
hexed phoenix
#

Wouldn't that solve your issue? Or do you absolutely need it to be set back to "none" once it exits the area?

#

Most games just have it update when you enter and make sure there's areas everywhere the player can walk in.

sharp nova
#

it needs to be something besides the areas name because im setting it up so that an input will only happen if youre standing in the area

hexed phoenix
#

Thing is, that by design. You can make it impossible for the player to exit an area and still be elegible.

You can cover every empty space with a different area, which would then mean the player is no longer in a valid one.

sharp nova
#

actually lemme edit

hexed phoenix
#

You can also just update every frame with get_overlapping_areas()

And if it overlaps with an area that has the information, update it to that.

sharp nova
#

so youre suggesting having a massive area in all the other space that just has the area_info set to defaults

#

based on tests it would work it just... seems like more than should be needed

hexed phoenix
#

Yeah.
with get_overlapping_areas() you don't have to.

If it overlaps with none of the areas, just leave it as "none" and there you go, automatic.

You can either handle this by design (my former idea) or entirely by code like this.

sharp nova
#
func _on_sensor_area_entered(area: Area2D) -> void:
    # Transporters
    if area.get_parent().has_method("get_transporter_info"):
        var area_node = area.get_parent()
        current_area_info = area_node.get_transporter_info()
        temp_toggle = true

func _on_sensor_area_exited(area: Area2D) -> void:
    temp_toggle = false```
#

this isnt one use, it updates as many times as you like

#

i dont understand WHY that works like that but the other one doesnt but i could just handle if my input goes through that way?

#

technically all i need is for my player to know if its at the base or the top of the stairs and do a move_toward function based on an input if its still within that area

#

i only needed it to know which its in because the destination marker 2d is different

sharp nova
#

ended up going with that and the solution looks like ```swift
func _on_sensor_area_entered(area: Area2D) -> void:
# Transporters
if area.get_parent().has_method("get_transporter_info"):
var area_node = area.get_parent()
current_area_info = area_node.get_transporter_info()
player_is_in_transporter_area = true

func _on_sensor_area_exited(area: Area2D) -> void:
player_is_in_transporter_area = false

func Transporters(delta: float):
if (player_is_in_transporter_area and Input.is_action_pressed("space")):
player_being_transported = true
does_player_have_controls = false

if player_being_transported:
    global_position = global_position.move_toward(current_area_info.destination,delta*Speed)

if player_being_transported:
    set_collision_layer_value(2, false)
    set_collision_mask_value(1, false)
    $Sensor.set_collision_layer_value(1, false)
    $Sensor.set_collision_mask_value(1, false)
    
if (global_position == current_area_info.destination):
    player_being_transported = false
    does_player_have_controls = true
    set_collision_layer_value(2, true)
    set_collision_mask_value(1, true)
    $Sensor.set_collision_layer_value(1, true)
    $Sensor.set_collision_mask_value(1, true)```
#

thats just a test, still gotta set up the animation player and a couple other things