• Godot HelpProgramming
  • How can I replace an object with one that isn't in the level but exists as tscn in the file system?

How do I use replace_by? It seems to not be able to replace anything when done as $object.replace_by($"NewObject", false) or even $"object".replace_by($"NewObject", false). Is there another way to swap one object for another? I tried with replace_by_instance, but that doesn't work and produces errors. I cannot find any tutorials on swapping objects.

I want to swap one rigid body with a static body, while said object is a body that entered an area and was detected by it.

I've never used replace_by, and I'd be wary of using it during signal handling or physics processing.

Instead there are a couple of options that you can try:

  1. Have both the static and rigid bodies in your scene, and control the swapping by toggling the visibility as the rigid body enters your area.
  2. When the body enters the area instantiate your static body and move it into position, and then free the rigid body.

@bitshift-r said: I've never used replace_by, and I'd be wary of using it during signal handling or physics processing.

Instead there are a couple of options that you can try:

  1. Have both the static and rigid bodies in your scene, and control the swapping by toggling the visibility as the rigid body enters your area.
  2. When the body enters the area instantiate your static body and move it into position, and then free the rigid body.

Thanks. So if I try option number 2, do I take the rotation and position of the rigid, spawn static body and apply rigid body's translation+rotation, then free the rigid body via body.queue_free()? I'll have to make the rigid body move toward the center of the area and rotate to match area's rotation first, then pass this info to the static body. Also for some reason the area doesn't cover the full collision area, it only activates if I bring the object to its center. I don't know why.

@Detonatress said: Thanks. So if I try option number 2, do I take the rotation and position of the rigid, spawn static body and apply rigid body's translation+rotation, then free the rigid body via body.queue_free()?

That's correct, although as mentioned in the other thread I think it would be easier to manage by taking over for the RigidBody's movement via _integrate_forces and custom_integrator.

@Detonatress said: I'll have to make the rigid body move toward the center of the area and rotate to match area's rotation first, then pass this info to the static body. Also for some reason the area doesn't cover the full collision area, it only activates if I bring the object to its center. I don't know why.

Not sure what's happening here. The body_entered signal should fire as soon as it enters the Area's collision shape.

2 years later