Why do I get error "Index p_contact_idx out of size (body->contact_count)."?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kaukamieli
:warning: Old Version Published before Godot 3 was released.

So I get this error every frame:

Index p_contact_idx out of size (body->contact_count)

The engine says the culprit is the line

if state.get_contact_collider_object(i):

I do have multiple contacts reported. The amount doesn’t apparently even matter. Is this an engine bug?

My version is stable. I tried with 2.1.4 too, which funnily says “Invalid ID” as first line before it starts spouting that error.

The most fun thing, though? The code definitely works. It just gives errors as a bonus.

func _integrate_forces(state):
for i in range(get_max_contacts_reported()):
	if state.get_contact_collider_object(i):
		if state.get_contact_collider_object(i).is_in_group("sparkobject"):
			sparksposition = state.get_contact_local_pos(i)
:bust_in_silhouette: Reply From: volzhs

This is description of get_max_contacts_reported()

int get_max_contacts_reported() const

Return the maximum contacts that can be reported. See set_max_contacts_reported().

you may set it as like 10.
the number of contacted objects might be less than 10.
but you made loop to get collider object 0~9.

Yes, well I wanted to go through them, but I should of course be using get_colliding_bodies().size(), I need a new brain.

And that if was supposed to check for null, it did earlier.
But I still get the error. Though now it happens on the is_in_group() part and only once every time when bodies stop touching.

func _integrate_forces(state):
	for i in range(get_colliding_bodies().size()):
		if state.get_contact_collider_object(i) != null:
			if state.get_contact_collider_object(i).is_in_group("sparkobject"):
				sparksposition = state.get_contact_local_pos(i)

Kaukamieli | 2017-08-13 18:22

Well, I learned something about weakref and stuff. I moved lot of checks over to the signal. Now I still get the same error, but only when thing touches multiple things… The culprit is now sparksposition = state.get_contact_local_pos(sparkbody)

func _integrate_forces(state):
if bodylist != null && sparkbody != null && wr.get_ref() && state.get_contact_count() > 0:
	sparksposition = state.get_contact_local_pos(sparkbody)
	print(sparkbody)

func _on_Link1_body_enter( body ):
	if body.is_in_group("sparkobject"):
		wr = weakref(body)
		bodylist = get_colliding_bodies()
		for i in bodylist:
			if !wr.get_ref():
				print("missing")
			elif body.is_in_group("sparkobject"):
				sparkbody = bodylist.find(body)

Kaukamieli | 2017-08-13 19:40