• 2D
  • Area2D mouse enter/exit detection problem

So i have 2 Area2D nodes with square shaped collision2d shapes .I am trying to detect when my mouse enters and exits each area.Both these areas are right next to eachother and when i move my mouse from the first area to the second one,it detects my mouse entering the second area before it detects it exiting the first area . Is this normal behaviour ?and what can i do to detect them in the correct order?

PS: im New to Godot :)

I don't know if that is expected, but if the areas are overlapping, then it would be normal to get an enter before an exit. Can you explain what it is you're trying to accomplish (maybe upload a minimal sample project of the problem)? I can try to see if there is some workaround.

@cybereality said: I don't know if that is expected, but if the areas are overlapping, then it would be normal to get an enter before an exit. Can you explain what it is you're trying to accomplish (maybe upload a minimal sample project of the problem)? I can try to see if there is some workaround.

Nope they are not overlapping ,but they have a very small gap between them. its a little hard to see in the picture though because of the colors. And here i demonstrate the problem simply printing enter/exit for inputs on both areas

Heres some of the code for the areas : What im trying to accomplish is detect when my mouse is released outside of the areas , so i use a bool global variable to detect that(globals.mexit).But ofcourse this doesnt work because when i enter the other area globals.mexit becomes true , even though im still in the second area.The reason im using a global variable is because im eventually going to add like 100 clickable and interactive areas so it makes things simpler.

Okay. So I was able reproduce the problem you said. The mouse_entered signals do seem to happen before the mouse_exited. And I'm still not exactly sure what you are trying to do with that mexit variable. However, I put together an example project which shows mouse rollover/hover and also clicking and dragging outside the object and getting a response. It is using colors on squares to show the states. I hope this helps.

Thanks for the project ! your code is alot simpler than mine and it would definitely solve my enter/exit order issue .I have one more question ,if you dont mind :). I am planing on having like 100 clickable buttons and each time i click on a button its value would increase to a certain number displayed on a label over the button , now my only question is : is there any way to store these numbers to individual variables while sharing the same script?Would be great if you could show something like this in your example Thanks! Sorry for the late reply btw

Each of the buttons can be a scene (that's how I set it up) so they use the same scripts and assets, but can have independent values for the variables. You just need to set a variable at the top of the script (so it can be accessed from outside) and then increment it from the main script.

On the button script:

var click_count = 0

In the main script:

current_clicked.click_count += 1

See the update files.

And how could i access those variables from each box? I tried putting this in the main script : "var N1=get_node("Box 1").click_count" , and i get an error saying null instance.

EDIT: I dont know if this is the best way to do it but , i put this line of code in process (delta) and it works now! I think i have everything sorted out now. Appreciate the help :D

I actually have one more little problem.When i switch to another scene and switch back all the values on the buttons are reset to zero.I would like them to stay the same when toggling scenes. Im not quite sure how i'd go about doing this.Do you have any ideas?

You could use an singleton/autoload script and store the values there so they are preserved from scene to scene. The documentation has a page on singletons that gives an overview of how they work.

Ive experimented with autoloading but in my project i have a bunch of buttons in different scenes just like in the project from @cybereality and im not sure where i would add the autoload script to store each individual value.Nothing ive tried has worked so far and i keep getting errors.You can check @cybereality's project for reference or If anyone wants to take a closer look heres my project below any help appreciated :)

Cool, I will try to take a look at the project later today and see what I can do.

Okay, I got it working! It is probably a tad more complicated than is strictly necessary, but it works and should be decently expandable.

I added a few exported variables to Clickable.gd so it could track what instance ID it is for saving and I added some code to _ready that loads the data stored in the singleton. I used a bunch of dictionaries for convenience and easier readability (in my opinion). I also added a function called _on_scene_change, which is called by the singleton when the scene changes.

I inputted all of the exported variables in for all of the clickable buttons in Table.tscn, but I didn't setup the clickable objects in 2ndview. Nodes that have the same _save_data_instance_category and _save_data_instance_id export variables will override each other, but outside of that they should work across scenes with little issue.

The data is stored in the number.gd singleton, in a dictionary called game_instance_save_data. This dictionary contains categories, which are also dictionaries, that hold the save data for each instance (also a dictionary. I like dictionaries :lol:). I modified the code so the change_scene function in number.gd is called instead of get_tree.change_scene and that, combined with the modifications in Clickable.gd, makes it where the data is saved across scenes.

Hopefully that makes sense. I tried to document most of how the code works, but time was limited so the code (and comments) were a bit rushed. Hopefully this helps though! :smile:

Thank you so much for this! Its definitely more complicated than i expected , and im still pretty new to godot/programming so id never come up with this myself lol.It works prefectly fine now and the comments in the code are also helpful explaining how it works.I still dont fully understand , but im gettin there xD. One part i dont understand is : how do i set up the clickable objects in 2ndview? Ive attached the same script to them and the "Table" script to the root node is there anything else i need to do? Also do you have any tips on syncing the values of buttons between the two scenes? Something that could point me in the right direction? Thanks in advance!

EDIT: Okay so i got it to work with the second scene by creating a second singleton script i called "number2" to use with my second scene where i also created a new script for the clickable boxes that directs them to my number2 script. It seems to work well for now :) Now i just gotta figure out the syncing.

No problem, happy to help!

To answer the question regarding syncing: If the buttons between scenes both use the clickable script, then as long as they have the exact same _save_data_instance_category and _save_data_instance_id set in the Godot editor, they should save/load the synced values between scenes.

You can also access the dictionary containing the values directly by access game_instance_save_data, and then passing in the instance category and ID you want to use. For example, for if you want to access the number of clicks in box 1, and you know the category is Boxes and the ID is 1, then you could use code like this (untested):

# make sure it exists in the dictionary
if (number.game_instance_save_data.has("Boxes")):
	if (number.game_instance_save_data["Boxes"].has("1")):
		var box_one_clicks = number.game_instance_save_data["Boxes"]["1"]["click_num"]
		print (box_one_clicks)

Hopefully this helps explain. I'll keep an eye out for a explanation/tutorial on how to use singletons to share data across scenes in Godot, as it can be confusing.

Slight aside: You should be able to use the same data across scenes without needing another autoload script, as long as you know roughly what data you are looking for. You can use multiple autoload scripts if you want, but it might be a tad more difficult to sync the values.

Thanks for the explanation. I finally figured it out. I didnt even know that you could change save data instance and category in the editor. Im a noob lol. Thanks so much for the help!