Hello everyone,
I'm working on a turn-based tactical game in Godot and am currently trying to develop the AI logic. My approach to this is to score different outcomes based on possible actions the AI can take. To achieve this, I'm attempting to duplicate the main combat logic nodes to simulate and evaluate different scenarios without affecting the main game state.
However, I've run into an issue where the duplicate() method doesn't seem to copy the @onready variables, causing them to be lost in the duplicated nodes.
To address this, I've written a custom duplicate function, but it's cumbersome and doesn't handle grandchildren nodes or deeper hierarchies:
func duplicate_object(OBJ):
var OBJ2 = OBJ.duplicate()
var l=OBJ.get_property_list()
for property in l:
if property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE:
if is_instance_valid(OBJ.get(property.name)):
var prop=OBJ.get(property.name)
if prop is Node or prop is Resource:
OBJ2.set(property.name, prop.duplicate())
else :
OBJ2.set(property.name, prop)
return OBJ2
Are there better ways or best practices for duplicating nodes, especially when dealing with @onready variables?
How can I enhance this function to duplicate grandchildren nodes or deeper hierarchies?
Any insights, optimizations, or alternative approaches would be greatly appreciated. Thank you in advance!