Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserializing single objects works for one frame and then it goes to the previous state #337

Open
Fransebas opened this issue Jan 25, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@Fransebas
Copy link

Fransebas commented Jan 25, 2025

Describe the bug

I'm trying to serialize and deserialize a single object, the only object that moves in the scene, when I do it goes for one frame to the positions it had when it was serialized and in the next frame it goes back as if nothing happen.

If I serialize and deserialize the whole space it works, but it's too much data, I only need the data from the moving object.

To Reproduce

Steps to reproduce the behavior:

Make the following scene:

  • RigidBody2D
    • CollisionShape2D (It's a circle)

Let the circle start falling and save the state (say at -100 y), after some time more time restore to the previous state (say the ball at this point is at - 800 y), the circle will flash for one frame to the position where you saved the state (-100 y) and one frame later it will continue falling like nothing happen (the circle will be at - 800 y) .

This is the code I use to serialize/deserialize:`

extends Node

@onready var _context : Context = get_tree().get_root().find_child("Context", true, false)



class SerializationData:
	var state : PackedByteArray 
	var rid : RID
	
	func _init(state: PackedByteArray, rid: RID):
		self.state = state
		self.rid = rid
		
var states : Array[SerializationData] = []
	
func save_state():
	get_tree().paused = true
	states.clear()
	states.append_array(get_state(_context.main_ball))
	
func get_state(node : BallSimulation) -> Array[SerializationData]:
	var states : Array[SerializationData] = []
	var shape_rid = node.get_rid()
	states.append(SerializationData.new(
		RapierPhysicsServer2D.export_binary(shape_rid), shape_rid
	))
	for owner_id in node.get_shape_owners():
		for owner_shape_id in node.shape_owner_get_shape_count(owner_id):
			var inner_shape_rid = node.shape_owner_get_shape(owner_id, owner_shape_id).get_rid()
			states.append(SerializationData.new(
				RapierPhysicsServer2D.export_binary(inner_shape_rid),
				inner_shape_rid
			))
	return states
	
func restore_state(states : Array[SerializationData]):
	get_tree().paused = true
	print ("states = ", states.size())
	for state in states:
		RapierPhysicsServer2D.import_binary(state.rid, state.state)

func _on_restore_pressed() -> void:
	restore_state(states)


func _on_save_pressed() -> void:
	save_state()

Expected behavior

The ball should start at - 100 y after restoring instead of continuing at - 800 y.

Environment:

  • OS: macOS
  • Version: Tried both 0.8.8 and 0.8.10
  • Godot Version: v4.x
  • Type: slow fully deterministic.

Example project(zip)

Let me know if I need to make an example project. Maybe I'm just missing something to deserialize.

Notes
Also, the documentation about deserialization doesn't match the code, in the documentation the code to deserialize is:

RapierPhysicsServer2D.space_import_json(space_rid, exported_json)
var body_json = RapierPhysicsServer2D.collision_object_import_json(body_rid, exported_json)

But the code only has the method:

RapierPhysicsServer2D.import_binary(state.rid, state.state)

There are other methods in the documentation that doesn't exists in the code.

@Fransebas Fransebas added the bug Something isn't working label Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant