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

Reference properties aren't serialized properly #170

Open
TheWalruzz opened this issue Feb 14, 2024 · 1 comment
Open

Reference properties aren't serialized properly #170

TheWalruzz opened this issue Feb 14, 2024 · 1 comment
Labels
🐛 bug Something isn't working
Milestone

Comments

@TheWalruzz
Copy link

Godot version: 4.2.1

Describe the bug
While trying to get equipment system working, I've noticed an issue with serialization of Reference properties.
Apparently, using set_reference() on an entity instance with a reference property and then calling Pandora.serialize(...) results in that property being serialized as an object, instead of a Dictionary, which causes problems when trying to process serialized data further.

To Reproduce
Consider an example project with this setup in Pandora as an example (ignore weird styling on dropdowns - I was messing with editor theme):

Zrzut ekranu 2024-02-14 174526

Initial reference in Helmet property is left empty.

Now, we run this code:

extends Node


@export var character: Character # extends PandoraEntity - Player entity is assigned
@export var item: HelmetItem # extends Item extends PandoraEntity - Simple Helmet entity is assigned


func _ready() -> void:
	var instance := character.instantiate()
	instance.set_reference("Helmet", item)
	var serialized := Pandora.serialize(instance)
	print(serialized)
	# prints: { "_id": "8", "_instanced_from_id": "8", "_instance_properties": [{ "_value": <PandoraEntity 'Simple Helmet'>, "_property_id": "7" }], "_index": 0 }
	var serialized_json := JSON.stringify(serialized)
	print(serialized_json)
	# prints: {"_id":"8","_index":0,"_instance_properties":[{"_property_id":"7","_value":"<PandoraEntity 'Simple Helmet'>"}],"_instanced_from_id":"8"}
	var deserialized_instance := Pandora.deserialize(JSON.parse_string(serialized_json))
	deserialized_instance.get_reference("Helmet")
	# Error: "Trying to assign a non-object value to a variable of type 'entity.gd'."

As you can see from the serialized Dictionary, Helmet property is serialized as { "_value": <PandoraEntity 'Simple Helmet'>, "_property_id": "7" } and when stringified to JSON it becomes a string, which is completely incorrect and causes an error in get_reference() method when trying to access that property after deserialization.

Expected behavior
Reference property is properly serialized.

Desktop (please complete the following information):

  • OS: Windows
  • Version 11

Additional context
Minimal reproduction project:

PandoraReferenceSerializationBug.zip

@TheWalruzz TheWalruzz added the 🐛 bug Something isn't working label Feb 14, 2024
@russmatney
Copy link
Contributor

I hit this a week or two ago, definitely not the expected behavior! A non-ideal workaround I went with was to use a string entity_id and fetching 'relations' with that id in the code. I didn't like it, but it doesn't leak too far if you're already writing a custom entity class:

@tool
extends Event
class_name PuzzleCompleted

func get_puzzle_set() -> PuzzleSet:
	return Pandora.get_entity(get_string("puzzle_set_id")) as PuzzleSet

Maybe a wrapper like this is how Pandora could handle this at serialize/deserialize time?

@bitbrain bitbrain added this to the 1.0.0 milestone Jul 14, 2024
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

3 participants