• Godot Help
  • Error when an instance tries to change a variable when a collision happens.

Hi, first time using this forum, and don't have an idea on what the problem might be/how to show it to you guys.

A player spawns a bullet and when it collides with a body that is in the group 'player', it applies damage to the body.
It literally worked yesterday at noon, but at night it started giving me the "Invalid get index 'damage' (on base: 'EncodedObjectAsID')." error.
Bullet damage code:

remotesync func damage(attacker: Object,receiver: Object):
		print(str(attacker)+" damaged "+str(receiver))
		receiver.health -= damage
		print(str(attacker)+" damaged "+str(receiver))
		if receiver.health <= 0:
			attacker.kills += 1
			#attacker.health += 30
			if attacker.health > 100:
				attacker.health = 100
		queue_free()

Bullet spawn code:

remotesync func spawn_bullet(id,tot_recoil):
	var b = bullet.instance()
	b.name = "Bullet" + name + str(Net.network_object_name_index)
	b.flag = get_parent()
	b.damage = rifle_damage
	b.position = $Bulletpoint.get_global_position()
	b.rotation_degrees = rotation_degrees + tot_recoil
	b.apply_impulse(Vector2(0,0),Vector2(bullet_speed,0).rotated(rotation + tot_recoil))
	Bullets.add_child(b)
	b.set_network_master(id)
	Net.network_object_name_index += 1

"Net" is a singleton and "network_object_name_index" counts each intanciated object, adding +1 to the total count in an attempt to sync the names. I copied that from altough i didnt copy what i didnt understand, so my code isnt 100% like it.

I receive the error on this line
'receiver.health -= damage'

It's hard to give you specifics without seeing the rest of your project.

The error means that the value being passed as receiver is an EncodedObjectAsID. I can't see how that happened since you haven't posted the code where it occurs. You could stop the current error by checking for an id, but that might not fix the problem.

if receiver is EncodedObjectAsID:
	receiver = receiver.get_object_id()
if receiver is EncodedObjectAsID:
		print("success")
		receiver_Id = receiver.get_instance_id()
		receiver_inst = instance_from_id(receiver_Id)
		print(str(attacker)+" damaged "+str(receiver))
		receiver_inst.health -= damage
		if receiver_inst.health <= 0:
			attacker.kills += 1
		queue_free()

I tried it like this, it gets to the "success" print but stops in "receiverinst.health -= damage" with the same error: "Invalid get index 'health' (on base: 'EncodedObjectAsID')."

This is the output, the entities are "encoded", and i've read something about remote code execution if they aren't encoded, so I won't try to make the exception. Its weird how it used to work, but all of a sudden yesterday it started to give me this encoded object error, since i havent changed anything on the network code and even tested a previous version.

So ok got to this post
and tried it without calling and RPC, just remotesync to update the player health to all, so the code looks like this:

func _on_Bullet_body_entered(body):
	if flag != body && body.is_in_group("player"):
		damage(flag,body)
remotesync func damage(attacker: Object,receiver: Object):
	print("success")
	print(str(attacker)+" damaged "+str(receiver))
	receiver.health -= damage
	if receiver.health <= 0:
		attacker.kills += 1
		queue_free()

where the damage function was called via RPC before.

Thanks duane for the reply, it didnt give me answer right away but it set me to the right direction!