Object data members are affected each other after instantiate()

Godot Version

4.3

Question

I used PackedScene.instantiate() to create several mobs at different timestamps, and I noticed some bugs that some data members are affecting each other. So I added a start_time as a data member, inited at ready() and in process(), i logged the start_time value.

Apparently, the start_time (time in the log) is switching back and forth frame after frame.

zombieid2 time: 1334scale:(0.806259, 0.806259) height:338.62890625
zombieid2 time: 4212scale:(0.21041, 0.21041) height:88.3720626831055
zombieid2 time: 1334scale:(0.80959, 0.80959) height:340.027954101562
zombieid2 time: 4212scale:(0.213949, 0.213949) height:89.8585662841797

I didn’t declare start_time as static. Nor have i seen in any other languages that instances can affect each other. Can someone help me understand what’s the behaviour of this?

That is strange. Can you try setting the start_time in _enter_tree() rather than _ready(). Enter tree fires when the scene enters the tree (obviously) but the ready function fires when the parent is ready.

Also, if this is a shared resource then are you duplicating the resource?

var mob_data = preload("res://mob_data.tres").duplicate()

I think your shared resource is being changed by multiple nodes, although why it would flick back and forth like that is still very strange.

Oh you set the start time value in the process()? So the process() function is writing to the shared resource? That is why your instance appears to be affected by other instances, as they are both modifying the same resource.

I don’t know to be fair but my best guess is that you need to duplicate the shared resource before using it to store data for an individual mob.

Look forward to seeing what the actual answer is if this is not it. Hope that helps.

Thanks for replying. Now i am more fascinating about the value flickering. start_time is a readonly value after init. Apparently there is some memory management behind this. It’s not a global variable, but it’s also shared between different instances. i don’t know if this is something designed on purpose.

1 Like