Godot Version
4.6
Question
I have an Area2D, and when the area is entered, an item in the game does an action when enabled. But when the item is disabled and the area is entered, and then when you enable the item, it does nothing until you exit and re-enter the Area2D.
1 Like
Can you post your code? The area_entered signal should be sent again when disabled is set to false.
2 Likes
Well that is little easy .. in the script of that object for example you have a object name tomato, cabbage and cheeze and bread to mke sandwich..
So in scripts of Tomato, cabbage cheese and bread.. make function like on Tomato make function tomato
Func Tomato (): pass
And in script of cabbage. Add function
Func cabbage(): pass
In script cheese add function cheese..
Func cheese(): pass
In script of breads add function breads..
Func bread(): pass
These are the methods created…
And then assign them collision shapes.. and in Main script connect all the. “areas body enter signals “.. and like
Func on_area_body_entered(): if body.has_meathod(" tomato, cabbage, cheese, bread"): burger.visible = true
Well i am writing this on mobile so sorry for synthex confusion.. i don’t have my laptop right now for some reasons
..
Well if you want separately.. then you can connect separate signals..
Like :—
Func on_area_body_entered(): the area signals form bread.. if body.has_method("cabbage"): bread_with_cabbage_image.visible = true
Hope so it’s helpful… if you provide more information then might be we could find any other better way 
1 Like
tbh i don’t think this helps answer the original question at all
2 Likes
Well i just try to help..
1 Like
I’m pretty sure you might need the entire script to fully understand the mechanics.
The _ready() function runs the pick_room() function that randomly chooses which room the player needs to go to. Then we connect the room_entered() signal so that when the player enters the room, it will tell you if you’ve done it!
extends Node
@export var array: Array[Area2D]
@onready var rng = RandomNumberGenerator.new()
var room: Area2D
#Code for choosing a room
func _ready() -> void:
pick_room()
func pick_room():
var _room: int = rng.randi_range(0, array.size() - 1)
room = array[_room]
array.clear()
array.append(room)
room.connect("body_entered",room_entered)
room.connect("body_exited",room_exited)
#print(str(room))
#print(str(array))
#Entering and exiting the room
func room_entered(body):
if body.has_node("Sprite"):
var sprite = body.get_node("Sprite")
if Global.on == true:
sprite.play("2")
func room_exited(body):
if body.has_node("Sprite"):
var sprite = body.get_node("Sprite")
if Global.on == true:
sprite.play("on")
As I’ve stated before, when the player enters the room with the item disabled and then enables it, the item does nothing. The same when you disable it when you enter and then enable it without leaving the area.
Well, thank you
(To be clear, I’m being genuine thank you for trying).
1 Like
When is the value of Global.on changed?
It’s a boolean, and the default value is false until you enable the item (because of an issue I encountered).
Can you check if the method connected to the body entered signal is being called in the first place? I think the issue might be related to Global.on
Let me rephrase this: I have an area2d and a sprite that changes colour depending on where it is and if it’s on. When the sprite enters the area2d and it’s enabled the sprite changes colour. The only problem is if the sprite is disabled outside of the area and then the sprite is put into that area and enabled there it doesn’t change colour. How can I fix this?
The global variable is to make sure if the sprite is disabled it won’t change colour if the area is entered.
You are disabling the body node and not the sprite itself, right?
1 Like
It’s just a simple two-switch piece of logic.
You have two state switches: inside/outside and enabled/disabled. The thing is highlighted if both of those switches are turned on, otherwise it’s not highlighted.
Intercept all actions that can change the state of either switch. Whenever that happens - check the state of both switches and set the highlighted state accordingly.
Let me try to understand but, if the item is enabled while inside the area, will never trigger the event related to crossing into the area, that is why you trigger it once you leave and entered again.
What the global variable does is if it’s false when entering the area it won’t change the sprite’s colour. If the sprite is outside of the area and the global var is true when the sprite enters the area it changes colour. If the sprite is inside of the area and the global var is false it won’t change colour, now if the global var is set to true while the sprite is inside of the area it won’t change colour like I need it to.
I think the body_entered signal doesn’t allow checking if the sprite is still inside of the area but only if checking if it’s entered and exited.
The signal body_entered will be emitted if the body’s collision shape is set to not disabled while it is inside the area. I tested it. That is why I think the issue is with the Global.on variable
Is there a signal that checks if something is inside of the area?
No, that is not the purpose of signals. Signals are only emitted when an event happens. If you want to check if something is inside an area at any point, use the has_overlapping_bodies method
1 Like