I am building a VR project that uses 2 area nodes with a sphere collider as children of the VR controllers to detect what object is grabbable by the user. Here's the code:

var colliding_with = null

func area_enter_rb(body: RigidBody):	#connected to signal
	print("body entered, colliding_with= ", colliding_with)
	if colliding_with == null:
		colliding_with = body
		body.get_node("mesh").material = body.material_selected  #hilight material
		print("entering finished, colliding_with= ", colliding_with)
	else:
		print("entering failed")

func area_exit_rb(body: RigidBody):	#connected to signal
	print("body left, colliding_with= ", colliding_with)
	if colliding_with != null:
		colliding_with = null
		body.get_node("mesh").material = body.material_normal  #default material
		print("exiting finished, colliding_with= ", colliding_with)
	else:
		print("exiting failed")

As far as I can tell by the print statements, everything seems to be running as it should; however (especially when multiple objects go in and out the area collider) it can happen that an object gets stuck on the hilighted material, even when the area is no longer touching it. What could be the issue? Maybe some enter/exit events do not register sometimes? Or are these functions running asyncrounously, leading to some shared resources issues?

(Btw the code formatting seems to be completely broken judging from the preview EDIT: solved by using ``` instead of `)

For multiple lines put three tildas above and below the code. From looking at your logic, it seems to me you looked over the fact that more than one object can be in the area. You are using just one variable (colliding_with ) to save the body that is supposed to be inside even though it can be overwritten by another object entering.

@pigna said: (Btw the code formatting seems to be completely broken judging from the preview)

Not really, you were just using inline code tagging. For code blocks add two more so is ``` in front and after the code block.

@Megalomaniak said:

@pigna said: (Btw the code formatting seems to be completely broken judging from the preview)

Not really, you were just using inline code tagging. For code blocks add two more so is ``` in front and after the code block.

Oh I am so sorry I didn't know that, thanks

@Erich_L said: For multiple lines put three tildas above and below the code. From looking at your logic, it seems to me you looked over the fact that more than one object can be in the area. You are using just one variable (colliding_with ) to save the body that is supposed to be inside even though it can be overwritten by another object entering.

Thanks for the help. Yeah, having only one collision variable is intentional, so that I only ever have 1 object that is grabbable at a time... but yeah, I think I start to see the issue here... When an object leaves the area it does not check if there are any other objects in it, so their collision doesn't get registered. That's probaby the cause

@pigna said:

@Megalomaniak said: Not really, you were just using inline code tagging. For code blocks add two more so is ``` in front and after the code block.

Oh I am so sorry I didn't know that, thanks

Yeah the forums software does have a tiny link to wikipedia article on markdown but doesn't really explain well how t use it or the forum software in general, which is why we made a topic on how to get started here that also includes a little bit of a cheat sheet for markdown as well(post 2).

@pigna said: Thanks for the help. Yeah, having only one collision variable is intentional, so that I only ever have 1 object that is grabbable at a time... but yeah, I think I start to see the issue here... When an object leaves the area it does not check if there are any other objects in it, so their collision doesn't get registered. That's probaby the cause

Did that help? Were you able to get it working? I didn't want to try changing your code directly because I was plastered.

6 days later

@Erich_L said:

@pigna said: Thanks for the help. Yeah, having only one collision variable is intentional, so that I only ever have 1 object that is grabbable at a time... but yeah, I think I start to see the issue here... When an object leaves the area it does not check if there are any other objects in it, so their collision doesn't get registered. That's probaby the cause

Did that help? Were you able to get it working? I didn't want to try changing your code directly because I was plastered.

Yes!

You might want to do it fully with the physics system and not areas. RigidBody has the signals "body_entered" and "body_exited" which are better suited for what you are doing. Also, having just one collision variable is not a good idea, you should find a way to make this more dynamic, for example using groups or checking the colliding body's name. Because those function can also happen like if the body touches to floor or wall or other things you might not be expecting and then your game will break.

a year later