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

dict2inst does not work on classes with _init parameters #30572

Open
Tracked by #33348
ducklin5 opened this issue Jul 14, 2019 · 6 comments · Fixed by #32534 or #38357
Open
Tracked by #33348

dict2inst does not work on classes with _init parameters #30572

ducklin5 opened this issue Jul 14, 2019 · 6 comments · Fixed by #32534 or #38357

Comments

@ducklin5
Copy link

Godot version:
Godot Engine v3.1.1.stable.official

Issue description:
dict2inst does not work on classes with _init parameters. A simple fix is to manually initialize the object after it has been created with .new()

Steps to reproduce:
Create a scene with an external(not built in script). Then run the scene with this in the script:

extends Node2D

class A:
	var value 
	func _init(val):
		self.value = val
		
class B:
	var value
	func init(val):
		self.value = val

func serializeA():
	var inst = A.new(1)
	var instDict = inst2dict(inst)
	var inst2 = dict2inst(instDict)
	print("A was serialized and deserialize succesfully")

func serializeB():
	var inst = B.new()
	B.init(1)
	var instDict = inst2dict(inst)
	var inst2 = dict2inst(instDict)
	print("B was serialized and deserialize succesfully")

func _ready():
	serializeA() # this will crash the program
	serializeB() # this runs fine
@ducklin5
Copy link
Author

Just found an alternative fix:

class A:
	var value 
	func _init(val=0):
		self.value = val

I'm guessing the reason for the crash is that dict2inst uses .new() without any parameters. I think the easiest solution to this is to make note of this in the docs

@Reneator
Copy link

Reneator commented Aug 6, 2019

There is a kind of similar problem with bytes2var where a node would not work properly if it has an _init() method with an argument defined.

If i remove the custom _init() method it works fine.

@I-Have-No-Idea-What-IAmDoing

seems to not be fixed in v3.4.2 as I just ran into this issue was able to go around it by making the parameter have default values but the error that is given is misleading.

@kleonc
Copy link
Member

kleonc commented Nov 5, 2022

Confirmed, still happens in 3.5.1.stable. Works fine in master.

Changes done in #32534 were never backported to 3.x.

@noverd

This comment was marked as off-topic.

@vnen
Copy link
Member

vnen commented Feb 24, 2023

That is sort of expected, dict2inst() won't be able to create an instance of a class that does not have a default constructor. We may find a way to bypass the constructor since the state is saved, but it isn't really a simple thing to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
10 participants